是什么导致游戏延迟?
What causes lag in the game?
我在 python 中用 pygame 制作了一个非常简单的游戏,我不知道为什么它会滞后,有人可以帮助我吗?
我试图自己修复它,但我做不到,也找不到在线修复它的方法。
这不是因为我的 PC,它有 16 GB 的内存,SSD 512 GB 的存储空间,处理器:Intel Core i7-7600U 2.90 GHz。
这是代码:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Platformer")
colour = (255, 0, 0) # Colour red
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x = x - vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x = x + vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y = y - vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y = y + vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y = y - (jumpCount ** 2) * 0.5 * neg
jumpCount = jumpCount - 1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win, colour, (x, y, width, height))
pygame.display.update()
pygame.quit()
应用程序滞后,因为应用程序循环中的延迟很大。去除延迟:
pygame.time.delay(100)
使用pygame.time.Clock
控制每秒帧数,从而控制游戏速度。
方法tick()
of a pygame.time.Clock
object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick()
:
This method should be called once per frame.
这意味着循环:
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
每秒运行 60 次。
完整示例:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Platformer")
colour = (255, 0, 0) # Colour red
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x = x - vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x = x + vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y = y - vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y = y + vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y = y - (jumpCount ** 2) * 0.5 * neg
jumpCount = jumpCount - 1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win, colour, (x, y, width, height))
pygame.display.update()
pygame.quit()
我在 python 中用 pygame 制作了一个非常简单的游戏,我不知道为什么它会滞后,有人可以帮助我吗?
我试图自己修复它,但我做不到,也找不到在线修复它的方法。
这不是因为我的 PC,它有 16 GB 的内存,SSD 512 GB 的存储空间,处理器:Intel Core i7-7600U 2.90 GHz。
这是代码:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Platformer")
colour = (255, 0, 0) # Colour red
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x = x - vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x = x + vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y = y - vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y = y + vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y = y - (jumpCount ** 2) * 0.5 * neg
jumpCount = jumpCount - 1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win, colour, (x, y, width, height))
pygame.display.update()
pygame.quit()
应用程序滞后,因为应用程序循环中的延迟很大。去除延迟:
pygame.time.delay(100)
使用pygame.time.Clock
控制每秒帧数,从而控制游戏速度。
方法tick()
of a pygame.time.Clock
object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick()
:
This method should be called once per frame.
这意味着循环:
clock = pygame.time.Clock() run = True while run: clock.tick(60)
每秒运行 60 次。
完整示例:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Platformer")
colour = (255, 0, 0) # Colour red
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x = x - vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x = x + vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y = y - vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y = y + vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y = y - (jumpCount ** 2) * 0.5 * neg
jumpCount = jumpCount - 1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win, colour, (x, y, width, height))
pygame.display.update()
pygame.quit()