0

I am having a problem adding a booster to my game, for now I have it set to a toggle but I want it to be a time based e.g. press shift get boost for 5 seconds cant press for 30 seconds. But when I tried this the player would have a continuous boost as the loop would restart, for now I have made the boost a toggle without a timer, .sleep does not work as well because it pauses the program for the time.

    while run:
     clock.tick(FPS)

     if len(enemies) == 0:
            level += 1
            wave_length += 5
            for i in range(wave_length):
                    enemy = Em(random.randrange(50, WIDTH-10), random.randrange(-1500*level/5, -100), random.choice(["red","green","purple","blue"]))
                    enemies.append(enemy)
     boost = False
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
            run = False
     keys = pygame.key.get_pressed()
   
     if keys[pygame.K_LEFT] and player.x - player_vel > 0:
        player.x -= player_vel
     if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH:
        player.x += player_vel
     if keys[pygame.K_UP] and player.y - player_vel > 0:
        player.y -= player_vel
     if keys[pygame.K_DOWN] and player.y + player_vel + player.get_height() < HEIGHT:
        player.y += player_vel

     if keys[pygame.K_LSHIFT]:
        boost = True
        start = time.time()
        if boost == True:
            player_vel = boost_speed
zacfifi
  • 3
  • 3

1 Answers1

0

There are two options I can think of to achieve what you want. Option number one would be to create a USEREVENT (https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame#:~:text=In%20pygame%20exists%20a%20timer%20event.%20Use%20pygame.time.set_timer,Note%2C%20in%20pygame%20customer%20events%20can%20be%20defined.).

Or you could calculate the time yourself and it is actually fairly easy. You simply divide the increase rate (1 in this instance) by the FPS like so:

import pygame
from sys import exit as sys_exit

pygame.init()

WINDOW_SIZE = (800, 600)
WINDOW = pygame.display.set_mode(WINDOW_SIZE)
FPS = 60
CLOCK = pygame.time.Clock()

finish_time = 5
current_time = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys_exit()
    
    current_time += 1 / FPS # This simple.

    if current_time >= finish_time:
        print("FINISHED!")
    
    pygame.display.update()
    CLOCK.tick(FPS)
CozyCode
  • 484
  • 4
  • 13