为什么我在 pygame 中得到恒定的 0 fps
Why do I get a constant 0 fps in pygame
即使我将时钟节拍设置为 60 并且它在右边 space,我也没有得到任何帧。我也知道它不是我的电脑,因为我有 运行 更大的游戏,帧数更好。它也不能是缩进,因为我的意图是正确的。
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()
任何帮助将不胜感激,我已经为这个问题苦苦挣扎了一段时间。
关于我应该如何组织代码,还有其他提示吗?
您需要在应用程序循环中调用 get_fps()
:
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()))
# [...]
键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN
事件在每次按下一个键时发生一次。 KEYUP
每松开一个键就会出现一次。使用键盘事件进行单个操作或逐步移动。
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()
评估按钮的当前状态并获得连续移动:
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
# [...]
即使我将时钟节拍设置为 60 并且它在右边 space,我也没有得到任何帧。我也知道它不是我的电脑,因为我有 运行 更大的游戏,帧数更好。它也不能是缩进,因为我的意图是正确的。
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()
任何帮助将不胜感激,我已经为这个问题苦苦挣扎了一段时间。 关于我应该如何组织代码,还有其他提示吗?
您需要在应用程序循环中调用 get_fps()
:
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()))
# [...]
键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN
事件在每次按下一个键时发生一次。 KEYUP
每松开一个键就会出现一次。使用键盘事件进行单个操作或逐步移动。
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()
评估按钮的当前状态并获得连续移动:
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
# [...]