I'm creating a little space shooter game. I need to limit the amount of times a player can fire, I'm setting a 2 second limit. But whenever I try to implement that it just won't work. The bullets keep firing multiple times a second.
Here's my code, I'd be very grateful if something could help:
def main():
pygame.init()
screen = pygame.display.set_mode((1000,750))
pygame.display.set_caption("Alien Invasion")
pygame.mouse.set_visible(False)
#load the bitmaps
background = pygame.image.load("background.jpg").convert_alpha()
space_ship = pygame.image.load("space_ship.tga").convert_alpha(background)
#create the objects
player = Space_Ship("space_ship.tga", screen)
asteroid_small = Asteroid(screen, "small")
asteroid_large = Asteroid(screen, "large")
asteroid_medium = Asteroid(screen, "medium")
#load the groups
asteroid_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
asteroid_group.add(asteroid_small, asteroid_large, asteroid_medium)
clock_start = 0
#start the main game loop
while True:
clock_start = time.clock()
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_SPACE]:
if clock_start > 2:
bullet = Space_Ship_Ammo(screen, player)
bullet_group.add(bullet)
clock_start = 0
#draw the background
screen.blit(background, (0,0))
#update the sprites
player.update()
asteroid_small.update()
asteroid_large.update()
asteroid_medium.update()
bullet_group.update()
pygame.display.update()
main()