【发布时间】:2022-01-12 06:54:03
【问题描述】:
我正在尝试在我的游戏中添加“射击”功能,玩家(无人机)会在按下键盘上的空格键时发射子弹。
我已经成功地做到了,但我也希望函数在执行一次后延迟。例如,玩家可以按一次空格键,然后必须等待 2-3 秒才能再次执行该功能。因此我的问题是,我怎样才能使它起作用?
这是当前的功能代码。
main.py
def shoot(self):
self.get_drone_coordinates()
x = self.drone_coordinates[0][0]
y = self.drone_coordinates[0][1] - 20
self.drone_bullet = Image(source="images/drone_bullet.png",
pos=(x, y))
self.add_widget(self.drone_bullet)
self.bullets.append(self.drone_bullet)
controls.py
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
elif keycode[1] == 'spacebar':
self.shoot()
我尝试导入时间库并在controls.py中的函数中添加一个time.sleep()函数,但这冻结了整个程序。
import time
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
elif keycode[1] == 'spacebar':
self.shoot()
time.sleep(3)
任何帮助将不胜感激!提前致谢!
【问题讨论】:
-
使用
Clock.schedule_once有帮助吗?
标签: python python-3.x kivy