pythonpython-3.xpygamepygame-clockpygame-tick

Why do I get a constant 0 fps in pygame


I am not getting any frames even though I have set my clock tick to 60 and it is in the right space. I also know its not my computer because I have run larger games with better frames. Also it cant be the indentation because I have it intended correctly.

import pygame


pygame.init()

screen = pygame.display.set_mode((500, 500))

gameclock = pygame.time.Clock()
fps = str(int(gameclock.get_fps()))

playerX = 250
playerY = 250
playerSpeed = 3

playerFrontImg = pygame.image.load("Images/PlayerImages/playerFront.png")
playerRightImg = pygame.image.load("Images/PlayerImages/playerRight.png")
playerBackImg = pygame.image.load("Images/PlayerImages/playerBack.png")
playerLeftImg = pygame.transform.flip(playerRightImg, True, False)


playercurrentImg = playerFrontImg


running = True
while running:
    gameclock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                playerY -= playerSpeed
                playercurrentImg = playerBackImg

            if event.key == pygame.K_DOWN:
                playerY += playerSpeed
                playercurrentImg = playerFrontImg

            if event.key == pygame.K_RIGHT:
                playerX += playerSpeed
                playercurrentImg = playerRightImg

            if event.key == pygame.K_LEFT:
                playerX -= playerSpeed
                playercurrentImg = playerLeftImg

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                playerY += 0

            if event.key == pygame.K_DOWN:
                playerY += 0

            if event.key == pygame.K_RIGHT:
                playerX += 0

            if event.key == pygame.K_LEFT:
                playerX += 0

    playerRect = playercurrentImg.get_rect(topleft = (playerX, playerY))
    screen.fill((255, 255, 100))
    print(fps)
    screen.blit(playercurrentImg, (playerX, playerY))
    pygame.display.update()
pygame.quit()
quit()

Any help would be greatly appreciated, I have been struggling with this issue for awhile now. Is there any other tips on how I should organize my code.


Solution

  • You need to invoke get_fps() in the application loop:

    Compute your game's framerate (in frames per second). It is computed by averaging the last ten calls to Clock.tick().

    gameclock = pygame.time.Clock()
    
    # [...]
    
    running = True
    while running:
        gameclock.tick(60)
        fps = str(int(gameclock.get_fps()))
        
        # [...]
    

    The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

    pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:

    running = True
    while running:
        gameclock.tick(60)
        fps = str(int(gameclock.get_fps()))
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            playerY -= playerSpeed
            playercurrentImg = playerBackImg
    
        if keys[pygame.K_DOWN]:
            playerY += playerSpeed
            playercurrentImg = playerFrontImg
    
        if keys[pygame.K_RIGHT]:
            playerX += playerSpeed
            playercurrentImg = playerRightImg
    
        if keys[pygame.K_LEFT]:
            playerX -= playerSpeed
            playercurrentImg = playerLeftImg
    
        # [...]