Search code examples
pythonpygame

How can I slow down the refresh rate in pygame?


I'm new to python and I'm trying to make a simple platformer game using pygame. My issue is that when I use a while loop to make a block fall until it hits the bottom of the screen, it travels there all at once and I can't see it happening. However when I move the block side to side using if statements, I can see that happening. How can I slow down the falling block down so it's visible?

I was following a tutorial for the most part, but wanted to add my own thing.

clock = pygame.time.Clock()
fps = 60
run = True
while run:
    clock.tick(fps)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a] and x > 0:
        x = x - 5
    if keys[pygame.K_d] and x < (500 - width):
        x = x + 5
    if keys[pygame.K_s]: #this is the portion that is too fast. 
        while y < (500 - height):
            y = y + 5    
    player = pygame.draw.rect(screen, (player_color), (x,y,width,height))
    pygame.display.update()

I tried putting the entire while ... y = y + 5 code into an if as well; that slowed it down, but it only moved when I held down the s key.


Solution

  • If you want it to fully 'animate' down, you should add the code that keeps the pygame screen/player updating in your while loop, otherwise you're just changing the y without changing the screen. So your code would look somewhat like this:

    clock = pygame.time.Clock()
    fps = 60
    run = True
    while run:
        clock.tick(fps)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] and x > 0:
            x = x - 5
        if keys[pygame.K_d] and x < (500 - width):
            x = x + 5
        if keys[pygame.K_s]: #this is the portion that is too fast. 
            while y < (500 - height):
                y = y + 5 
                player = pygame.draw.rect(screen, (player_color), (x,y,width,height)) # Make sure to update the player
                pygame.display.update() # Make sure to update the display
        player = pygame.draw.rect(screen, (player_color), (x,y,width,height))
        pygame.display.update()
    

    Changing the FPS:
    But, if you do want to change the speed of the game loop/essentially the frames per second, you can simply change the fps variable/the clock.tick() argument. So for example:

    clock = pygame.time.Clock()
    fps = 30 # This value is the amount of frames per second
    run = True
    while run:
        clock.tick(fps) # The argument (currently fps) passed into this method will change the frames per second
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] and x > 0:
            x = x - 5
        if keys[pygame.K_d] and x < (500 - width):
            x = x + 5
        if keys[pygame.K_s]: #this is the portion that is too fast. 
            while y < (500 - height):
                y = y + 5    
        player = pygame.draw.rect(screen, (player_color), (x,y,width,height))
        pygame.display.update()
    

    You can read more about the clock.tick() method here