简体   繁体   中英

What is the best way to make a player move at every interval in pygame?

Is there a library or a simple way to only loop something every 0.5 seconds without interrupting the rest of the program?

I have just started using pygame and have made a simple platformer and a Pong replica so far. I decided to try and make a Snake replica (I only currently have the head) and I need the snake to only move every 0.5 seconds while inputs can be registered at the 30 fps which I have the rest of the game running at. This is my current workaround:

while running: #this is tabbed back in my code

# keep loop running at the right speed
clock.tick(FPS)

# get time at each iteration
currentTime = str(time.time()).split(".")[0]
gameTime = int (currentTime) - int (startTime)
# this is used to check for something every 0.5 second (500 ms)
currentTimeMs = str(time.time()).split(".")[1]

# snake will move evry 0.5 second in a direction
if currentTimeMs[0] in ["5","0"] and moveDone == False:
    moveDone = True
    player1.move(direction)
elif currentTimeMs[0] not in ["5","0"]:
    moveDone = False

There is more code within the while running: loop to get the direction and display the sprites but its not necessary for this. My current code works fine and will repeat the move function for my player1 every time that x in mm:ss:x is 0 or 5 (0.5 seconds apart) and will not repeat if it is that multiple times over a few frames.

This code needs to work within the running loop and not stop the program so time.sleep() doesn't work. I have also tried using the schedule library but it will not work as it cannot seem to allow the direction variable to change when passing it into the function.

My question therefore is; Is there a library or a shorter way to accomplish what I need?

Thanks in advance and I can message you the whole code if you need.

You can store the moment of last move and then compare to actual time. To do it you can use perf_counter from time . Here is an example

last_move_time = perf_counter()

while running:
    if perf_counter() - last_move_time > 0.5:
        # move player
        last_move_time = perf_counter()

I suggest using pygames event mechanics and pygame.time.set_timer() (seehere for docs).

You would do something like this:

pygame.time.set_timer(pygame.USEREVENT, 500)

and in the event loop look for the event type.

if event.type == pygame.USEREVENT:

If this is the only user defined event that you are using in your program you can just use USEREVENT .

When you detect the event the timer has expired and you move your snake or whatever. A new timer can be set for another 1/2 second. If you need more accuracy you can keep tabs on the time and set the timer for the right amount of time, but for you situation just setting it for 1/2 sec each time is okay.

If you need multiple timers going and need to tell them apart, you can create an event with an attribute that you can set to different values to track them. Something like this (though I have not run this particular code snippet, so there could be a typo):

my_event = pygame.event.Event(pygame.USEREVENT, {"tracker": something})
pygame.time.set_timer(my_event , 500)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM