0

即使我将时钟刻度设置为 60 并且它在正确的空间中,我也没有得到任何帧。我也知道它不是我的电脑,因为我运行过帧数更好的大型游戏。它也不能是缩进,因为我的意图是正确的。

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()

任何帮助将不胜感激,我一直在努力解决这个问题。关于我应该如何组织我的代码还有其他提示吗?

4

1 回答 1

3

您需要get_fps()在应用程序循环中调用:

计算游戏的帧速率(以每秒帧数为单位)。它是通过平均最后十次调用来计算的Clock.tick()

gameclock = pygame.time.Clock()

# [...]

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

键盘事件(参见pygame.event模块)仅在按键状态更改时发生一次。KEYDOWN每次按下某个键时,该事件发生一次。KEYUP每次释放键时发生一次。将键盘事件用于单个操作或逐步移动。

pygame.key.get_pressed()返回一个包含每个键状态的列表。如果按住某个键,则该键的状态为True,否则为False。用于pygame.key.get_pressed()评估按钮的当前状态并获得连续移动:

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

    # [...]
于 2020-12-17T18:17:08.603 回答