Search code examples
pythonpygameframe-rate

Pygame: display doesn't update


I am having an issue where the display doesn't update for some reason. I have confirmed that x goes up but the display doesn't show it for some reason. Code:

import pygame


SIZE = (500, 500)
CENTER = (SIZE[0] / 2, SIZE[1] / 2)
FPS = 60
COLORS = {
    "white": (255, 255, 255),
    "red": (255, 0, 0),
    "green": (0, 255, 0),
    "blue": (0, 0, 255),
    "black": (0, 0, 0)
}
WIN = pygame.display.set_mode(SIZE)

pygame.display.set_caption("what da dog doing?")


def main():
    clock = pygame.time.Clock()
    run = True
    player = pygame.draw.rect(WIN, COLORS.get(
        "white"), (CENTER[0] - 400 / 2, CENTER[1] - 70 / 2, 400, 70),  border_radius=10)

    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        player.x += 1
        pygame.display.update()

    pygame.quit()


if __name__ == "__main__":
    main()

Any help is appreciated.


Solution

  • you need to place stuff that is supposed to be updated in the main loop, also you need to fill the screen with some color (or blit an image that is large enough to cover the area you need) to draw over the previously drawn stuff

    def main():
        clock = pygame.time.Clock()
        run = True
        player = pygame.draw.rect(WIN, COLORS.get(
            "white"), (CENTER[0] - 400 / 2, CENTER[1] - 70 / 2, 400, 70),  border_radius=10)
    
        while run:
            clock.tick(FPS)
    
            WIN.fill((0, 0, 0))
    
            pygame.draw.rect(WIN, COLORS.get('white'), player, border_radius=10)
            player.x += 1
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
    
            pygame.display.update()
    
        pygame.quit()