0

I'm building a project like a player trys to dodge the bullet, so my game calls create_bullet() each time in my while True loop.

I don't know how to call that function with an x time but without using time.sleep() because when I use it, it stops my entire game, even the player movement.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    Time to learn pygame. – stark Jan 13 '22 at 00:23
  • See https://gameprogrammingpatterns.com/game-loop.html, https://eng.libretexts.org/Bookshelves/Computer_Science/Programming_Languages/Book%3A_Making_Games_with_Python_and_Pygame_(Sweigart)/03%3A_Pygame_Basics/3.04%3A_Game_Loops_and_Game_States, https://stackoverflow.com/questions/54981456/how-to-implement-a-pygame-timed-game-loop and https://stackoverflow.com/questions/16301193/whats-the-proper-way-to-write-a-game-loop-in-python – Martheen Jan 13 '22 at 00:27
  • You could create a [`threading.Timer`](https://docs.python.org/3/library/threading.html#timer-objects) object that will call a function after a certain amount of time has passed without blocking the rest of your game. If you want this to happen more than once, see [threading.Timer - repeat function every 'n' seconds](https://stackoverflow.com/questions/12435211/threading-timer-repeat-function-every-n-seconds). – martineau Jan 13 '22 at 01:11
  • You could generate a custom event on a set time interval and use that to create your bullets, see [this answer](https://stackoverflow.com/a/60880122/2280890) for an example. – import random Jan 13 '22 at 01:47

2 Answers2

0

try taking time deltas and check if they are within certain range (5 seconds in this case) you can call your function again. some pseudo code off the top of my head:

tic = some initialized value
while true: # your game's loop

  if (time now - tic) >= 5 seconds:
    tic = time now # update tic
  else:
    let the game loop run
  player.display()
  bullet.display()
  # some game logic happens
  # loop ends and goes back

time.sleep() is not a good way to do it because, as you may already realized, will stop all of the processes running

lealvcon
  • 178
  • 8
0

Use a timer event pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. The time has to be set in milliseconds.
For a timer event you need to define a unique user events id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.

interval_in_seconds = 2 # 2 seconds

timer_interval = interval_in_seconds * 1000 # 1000 milliseconds = 1 second
timer_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event_id, timer_interval)

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

         elif event.type == timer_event_id:
             
             # call function
             my_funktion()

The timer event can be stopped by passing 0 to the time argument of pygame.time.set_timer.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174