pygame.event.pump()

本文介绍了Pygame中事件泵(event pump)的功能及其使用方法。通过调用`pygame.event.pump()`来确保即使在事件循环之外,也能正常处理窗口和输入事件。这对于保持游戏或应用的稳定运行至关重要。
import time import pygame from ElfinRobotMovementHTIC import * class Cmd(object): def __init__(self): self.reset() def reset(self): self.axis0 = 0 self.axis1 = 0 self.axis2 = 0 self.axis3 = 0 self.axis4 = 0 self.axis5 = 0 self.btn0 = 0 self.btn1 = 0 self.btn2 = 0 self.btn3 = 0 self.btn4 = 0 self.btn5 = 0 self.btn6 = 0 self.btn7 = 0 self.btn8 = 0 self.btn9 = 0 self.hat0 = 0 class Service(object): def __init__(self, robot): self.joystick = None self.robot = robot self.cmd = Cmd() def init_joystick(self): pygame.init() self.joystick = pygame.joystick.Joystick(0) self.joystick.init() print("44444444") def test(self,wet): print(wet) def loop(self): air = False while True: self.cmd.reset() pygame.event.pump() for i in range(0, self.joystick.get_numaxes()): val = self.joystick.get_axis(i) if abs(val) < 0.2: val = 0 tmp = "self.cmd.axis" + str(i) + " = " + str(val) if val != 0: exec(tmp) for i in range(0, self.joystick.get_numhats()): val = self.joystick.get_hat(i) tmp = "self.cmd.hat" + str(i) + " = " + str(val) exec(tmp) for i in range(0, self.joystick.get_numbuttons()): if self.joystick.get_button(i) != 0: tmp = "self.cmd.btn" + str(i) + " = 1" exec(tmp) if self.cmd.btn1: #toggle IO air = not air stoprobot(self.robot) #(0:X,1:Y,2:Z,3:A,4:B,5:C) #0=negative;1=positive if self.cmd.btn0 and self.cmd.hat0 == (0, 1): print("value from the hat is,self.cmd.btn0",self.cmd.hat0,self.cmd.btn0) LongJogL(self.robot,2,1) elif self.cmd.btn0 and self.cmd.hat0 == (0, -1): print("value from the hat is,self.cmd.hat0",self.cmd.hat0,self.cmd.btn0) LongJogL(self.robot,2,0) elif self.cmd.hat0 == (0, 1): print("value from the hat is",self.cmd.hat0) self.test("test") LongJogL(self.robot,1,0) elif self.cmd.hat0 == (0, -1): print("value from the hat is",self.cmd.hat0) LongJogL(self.robot,1,1) elif self.cmd.hat0 == (1, 0): print("value from the hat is",self.cmd.hat0) LongJogL(self.robot,0,0) elif self.cmd.hat0 == (-1, 0): print("value from the hat is",self.cmd.hat0) LongJogL(self.robot,0,1) elif self.cmd.axis0 > 0.5 : LongJogL(self.robot,3,1) print("movement in axis0") elif self.cmd.axis0 < -0.5: LongJogL(self.robot,3,0) print("movement in axis0") elif self.cmd.axis1 > 0.5: LongJogL(self.robot,4,1) elif self.cmd.axis1 < -0.5: LongJogL(self.robot,4,0) elif self.cmd.axis2 > 0.5: LongJogL(self.robot,5,1) elif self.cmd.axis2 < -0.5: LongJogL(self.robot,5,0) else: stoprobot(self.robot) def close(self): if self.joystick: self.joystick.quit() if __name__ == "__main__": s = connect('192.168.2.1') # s = connect('172.16.101.34') service = Service(s) service.init_joystick() try: service.loop() finally: service.close() 手柄控制代码如上
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值