I'm trying to add a "shoot" function to my game, where the player -a drone- would shoot a bullet upon pressing the space bar on the keyboard.
I've done that successfully but I also want the function to be on delay after executing it once. For example the player could press spacebar once then have to wait 2-3 seconds before being able to execute the function again. Hence my question, how could I make that work?
This is the current functional code.
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()
I tried importing the time library and adding a time.sleep() function in the function in controls.py, but that froze the entire program.
import time
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
elif keycode[1] == 'spacebar':
self.shoot()
time.sleep(3)
Any help would be appreciated! Thanks in advance!