【发布时间】:2018-10-05 20:40:17
【问题描述】:
我的代码包含一个操纵杆 PyGame 模块,当左操纵杆的垂直轴移动时,它会更新变量 self.mmcount。我的问题是它太频繁地更新 mmcount 变量,很难得到一个特定的整数。我认为解决方案是在 IF 语句中添加延迟。例如,每两秒检查一次左摇杆是否朝上。
这些是更新 self.mmcount 变量的 IF 语句:
if joy1.get_button(3) == 1:
self.mmcount -= 1
if joy1.get_button(2) == 1:
self.mmcount += 1
完整代码:
class Menu:
def run(self):
self.intro = True
self.clock = clock
self.mmcount = 1
while self.intro:
self.get_joys()
def get_joys(self):
if joy1.get_button(3) == 1:
self.mmcount -= 1
elif joy1.get_button(2) == 1:
self.mmcount += 1
if self.mmcount > 3:
self.mmcount = 3
elif self.mmcount < 1:
self.mmcount = 1
m = Menu()
while True:
m.run()
【问题讨论】:
-
将最后3个
if替换为elif
标签: python python-3.x pygame joystick