Do something every x (milli)seconds in pygame

后端 未结 2 992
刺人心
刺人心 2020-11-29 12:43

I\'m learing Python and Pygame, and my first thing I\'m making is a simple Snake game. I\'m trying to make it so that the snake moves once every 0.25 seconds. Here is the pa

相关标签:
2条回答
  • 2020-11-29 13:21

    There are several approaches, like keeping track of the system time or using a Clock and counting ticks.

    But the simplest way is to use the event queue and creating an event every x ms, using pygame.time.set_timer():

    pygame.time.set_timer()

    repeatedly create an event on the event queue

    set_timer(eventid, milliseconds) -> None

    Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed.

    Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.

    To disable the timer for an event, set the milliseconds argument to 0.

    Here's a small, running example where the snake moves every 250 ms:

    import pygame
    pygame.init()
    screen = pygame.display.set_mode((300, 300))
    player, dir, size = pygame.Rect(100,100,20,20), (0, 0), 20
    MOVEEVENT, t, trail = pygame.USEREVENT+1, 250, []
    pygame.time.set_timer(MOVEEVENT, t)
    while True:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]: dir = 0, -1
        if keys[pygame.K_a]: dir = -1, 0
        if keys[pygame.K_s]: dir = 0, 1
        if keys[pygame.K_d]: dir = 1, 0
    
        if pygame.event.get(pygame.QUIT): break
        for e in pygame.event.get():
            if e.type == MOVEEVENT: # is called every 't' milliseconds
                trail.append(player.inflate((-10, -10)))
                trail = trail[-5:]
                player.move_ip(*[v*size for v in dir])
    
        screen.fill((0,120,0))
        for t in trail:
            pygame.draw.rect(screen, (255,0,0), t)
        pygame.draw.rect(screen, (255,0,0), player)
        pygame.display.flip()
    

    enter image description here

    0 讨论(0)
  • 2020-11-29 13:21

    Use the Clock module of Pygame to keep track of time. Specifically the method tick of the Clock class will report to you the number of milliseconds since the last time you called tick. Therefore you can call tick once at the beginning (or at the end) of every iteration in your game loop and store its return value in a variable called dt. Then use dt to update your time-dependent game state variables.

    time_elapsed_since_last_action = 0
    clock = pygame.time.Clock()
    
    while True: # game loop
        # the following method returns the time since its last call in milliseconds
        # it is good practice to store it in a variable called 'dt'
        dt = clock.tick() 
    
        time_elapsed_since_last_action += dt
        # dt is measured in milliseconds, therefore 250 ms = 0.25 seconds
        if time_elapsed_since_last_action > 250:
            snake.action() # move the snake here
            time_elapsed_since_last_action = 0 # reset it to 0 so you can count again
    
    0 讨论(0)
提交回复
热议问题