Pygame window 真的很慢
Pygame window is really laggy
当我尝试 运行 时,pygame window 变得非常迟钝。到目前为止,我有 132 行代码,而且我还有更多的东西要添加。在测试-运行ning 时,我注意到当我在
中添加以下代码时,游戏出现了延迟
class Game:
def __init__(self):
self.win = pygame.display
self.d = self.win.set_mode((1200, 600))
self.win.set_caption(" JUMP ")
self.timee = 0
def write(self, size, writing, color, x, y):
font = pygame.font.SysFont("nirmalauisemilight", size)
text = font.render(writing, True, color)
self.d.blit(text, (x, y))
def game_over(self):
if b.y >= ba.y - b.side and self.timee > 5:
while True:
self.write(200, "GAME OVER", (255, 0, 0), 20, 50)
self.write(50, "Press enter to satart a new game", (0, 255, 0), 100, 400)
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
main()
g.win.flip()
和
def draw(self):
if g.timee < 5:
pygame.draw.rect(g.d, (0, 255, 0), (self.x, self.y, self.width, self.height))
else:
pygame.draw.rect(g.d, (255, 0, 0), (self.x, self.y, self.width, self.height))
g.timee
是我创建的一个变量,因此我可以在进入游戏一段时间后执行一些功能。 g.timee
在主循环中递增 0.01。我也尝试使用时间模块的 time.time()
功能,但它更落后于游戏。任何有关如何减少滞后的建议都将不胜感激。
编辑
在查看其他问题时,我遇到了 cPrifile 并使用它获得了这些数据
ncalls tottime percall cumtime percall filename:lineno(function)
0.000 0.000 0.204 0.204 testing.py:10(__init__)
1 0.000 0.000 0.000 0.000 testing.py:102(platforms)
401 0.001 0.000 0.001 0.000 testing.py:21(game_over)
1 0.020 0.020 9.688 9.688 testing.py:3(main)
1 0.000 0.000 0.000 0.000 testing.py:42(Block)
1 0.000 0.000 0.000 0.000 testing.py:43(__init__)
401 0.003 0.000 0.016 0.000 testing.py:53(draw)
401 0.002 0.000 0.006 0.000 testing.py:56(move)
401 0.001 0.000 0.001 0.000 testing.py:63(jump_logic)
401 0.002 0.000 0.002 0.000 testing.py:72(fall)
1 0.000 0.000 0.000 0.000 testing.py:84(Base)
1 0.000 0.000 0.000 0.000 testing.py:85(__init__)
1 0.000 0.000 0.000 0.000 testing.py:9(Game)
401 0.001 0.000 0.009 0.000 testing.py:91(draw)
401 0.001 0.000 0.001 0.000 testing.py:97(on_base)
虽然我不完全确定这是什么意思。
编辑 2
这是我目前的代码供参考
import pygame, os, random
def main():
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
acc_gravity = 0.009
terminal_vel = 4
class Game:
def __init__(self):
self.win = pygame.display
self.d = self.win.set_mode((1200, 600), pygame.RESIZABLE)
self.win.set_caption(" JUMP ")
self.timee = 0
self.platlist = []
def write(self, size, writing, color, x, y):
font = pygame.font.SysFont("nirmalauisemilight", size)
text = font.render(writing, True, color)
self.d.blit(text, (x, y))
def game_over(self):
if b.y >= ba.y - b.side and self.timee > 5:
while True:
self.write(200, "GAME OVER", (255, 0, 0), 20, 50)
self.write(50, "Press enter to start a new game", (0, 255, 0), 100, 400)
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
main()
g.win.flip()
def control_counter(self):
if self.timee > 100:
self.timee = 100
class Block:
def __init__(self):
self.x = 600
self.y = 0
self.side = 30
self.speed = 2
self.is_jumping = False
self.gravity = 0.005
self.jump_force = 10
self.jump_strength = 0.05
def draw(self):
pygame.draw.rect(g.d, (0, 99, 99), (self.x, self.y, self.side, self.side))
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.x += self.speed
if keys[pygame.K_LEFT]:
self.x -= self.speed
def jump_logic(self):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if self.is_jumping == False:
b.jump_force = 10
self.is_jumping = True
def fall(self):
self.y += self.gravity
self.gravity += acc_gravity
if self.gravity >= terminal_vel:
self.gravity = terminal_vel
def jump(self):
self.y -= self.jump_force
self.jump_force -= self.jump_strength
if self.jump_force <= 0:
self.jump_force = 0
class Base:
def __init__(self):
self.x = 0
self.y = 550
self.width = 1200
self.height = 20
def draw(self):
if g.timee < 5:
pygame.draw.rect(g.d, (0, 255, 0), (self.x, self.y, self.width, self.height))
else:
pygame.draw.rect(g.d, (255, 0, 0), (self.x, self.y, self.width, self.height))
def on_base(self):
if b.y >= self.y - b.side:
b.y = self.y - b.side
b.is_jumping = False
class Platforms:
def __init__(self):
self.x = random.randint(1, 1100)
self.y = random.randint(-300, 0)
self.width = 100
self.height = 20
self.speed = random.randint(1, 4)
def draw(self):
pygame.draw.rect(g.d, (0, 0, 0), (self.x, self.y, self.width, self.height))
def move(self):
self.y += self.speed
ba = Base()
b = Block()
g = Game()
for i in range(10):
g.platlist.append(Platforms())
game_loop = True
while game_loop:
g.timee += 0.01
events = pygame.event.get()
g.d.fill((98, 98, 98))
b.draw()
b.move()
b.jump_logic()
b.fall()
if b.is_jumping:
b.jump()
ba.draw()
ba.on_base()
g.game_over()
for plats in g.platlist:
plats.draw()
plats.move()
g.win.flip()
pygame.time.Clock().tick(60)
main()
我没有看到任何明显的东西,所以我猜是由于你的时间延迟机制。你能 post 说说你是怎么拖延的吗?您的延迟时间不得干扰 rendering/drawing。这是一个例子。
def gameloop1(): # delay interfering with drawing
# delay by some long loop
i = 0
while i < 10000:
i = i + 1
if i == 10000:
# execute some function
# draw stuff
i = 0
def gameloop2():
i = i + 1
if i == 10000:
# execute some function
# draw stuff
在示例中,gameloop1 在每次抽奖之前都会有一些延迟,而 gameloop2 不会并且仍然允许您执行延迟。也不要将此增量用作延迟机制,因为它会根据不同的系统速度而变化,请改用时基。
我明白了。这是因为我的 pygame.time.Clock().tick(60)
在游戏循环中。不确定为什么这会导致延迟,但对于可能遇到相同问题的任何人,请不要将其放在主循环中。
当我尝试 运行 时,pygame window 变得非常迟钝。到目前为止,我有 132 行代码,而且我还有更多的东西要添加。在测试-运行ning 时,我注意到当我在
中添加以下代码时,游戏出现了延迟class Game:
def __init__(self):
self.win = pygame.display
self.d = self.win.set_mode((1200, 600))
self.win.set_caption(" JUMP ")
self.timee = 0
def write(self, size, writing, color, x, y):
font = pygame.font.SysFont("nirmalauisemilight", size)
text = font.render(writing, True, color)
self.d.blit(text, (x, y))
def game_over(self):
if b.y >= ba.y - b.side and self.timee > 5:
while True:
self.write(200, "GAME OVER", (255, 0, 0), 20, 50)
self.write(50, "Press enter to satart a new game", (0, 255, 0), 100, 400)
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
main()
g.win.flip()
和
def draw(self):
if g.timee < 5:
pygame.draw.rect(g.d, (0, 255, 0), (self.x, self.y, self.width, self.height))
else:
pygame.draw.rect(g.d, (255, 0, 0), (self.x, self.y, self.width, self.height))
g.timee
是我创建的一个变量,因此我可以在进入游戏一段时间后执行一些功能。 g.timee
在主循环中递增 0.01。我也尝试使用时间模块的 time.time()
功能,但它更落后于游戏。任何有关如何减少滞后的建议都将不胜感激。
编辑
在查看其他问题时,我遇到了 cPrifile 并使用它获得了这些数据
ncalls tottime percall cumtime percall filename:lineno(function)
0.000 0.000 0.204 0.204 testing.py:10(__init__)
1 0.000 0.000 0.000 0.000 testing.py:102(platforms)
401 0.001 0.000 0.001 0.000 testing.py:21(game_over)
1 0.020 0.020 9.688 9.688 testing.py:3(main)
1 0.000 0.000 0.000 0.000 testing.py:42(Block)
1 0.000 0.000 0.000 0.000 testing.py:43(__init__)
401 0.003 0.000 0.016 0.000 testing.py:53(draw)
401 0.002 0.000 0.006 0.000 testing.py:56(move)
401 0.001 0.000 0.001 0.000 testing.py:63(jump_logic)
401 0.002 0.000 0.002 0.000 testing.py:72(fall)
1 0.000 0.000 0.000 0.000 testing.py:84(Base)
1 0.000 0.000 0.000 0.000 testing.py:85(__init__)
1 0.000 0.000 0.000 0.000 testing.py:9(Game)
401 0.001 0.000 0.009 0.000 testing.py:91(draw)
401 0.001 0.000 0.001 0.000 testing.py:97(on_base)
虽然我不完全确定这是什么意思。
编辑 2
这是我目前的代码供参考
import pygame, os, random
def main():
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
acc_gravity = 0.009
terminal_vel = 4
class Game:
def __init__(self):
self.win = pygame.display
self.d = self.win.set_mode((1200, 600), pygame.RESIZABLE)
self.win.set_caption(" JUMP ")
self.timee = 0
self.platlist = []
def write(self, size, writing, color, x, y):
font = pygame.font.SysFont("nirmalauisemilight", size)
text = font.render(writing, True, color)
self.d.blit(text, (x, y))
def game_over(self):
if b.y >= ba.y - b.side and self.timee > 5:
while True:
self.write(200, "GAME OVER", (255, 0, 0), 20, 50)
self.write(50, "Press enter to start a new game", (0, 255, 0), 100, 400)
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
main()
g.win.flip()
def control_counter(self):
if self.timee > 100:
self.timee = 100
class Block:
def __init__(self):
self.x = 600
self.y = 0
self.side = 30
self.speed = 2
self.is_jumping = False
self.gravity = 0.005
self.jump_force = 10
self.jump_strength = 0.05
def draw(self):
pygame.draw.rect(g.d, (0, 99, 99), (self.x, self.y, self.side, self.side))
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.x += self.speed
if keys[pygame.K_LEFT]:
self.x -= self.speed
def jump_logic(self):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if self.is_jumping == False:
b.jump_force = 10
self.is_jumping = True
def fall(self):
self.y += self.gravity
self.gravity += acc_gravity
if self.gravity >= terminal_vel:
self.gravity = terminal_vel
def jump(self):
self.y -= self.jump_force
self.jump_force -= self.jump_strength
if self.jump_force <= 0:
self.jump_force = 0
class Base:
def __init__(self):
self.x = 0
self.y = 550
self.width = 1200
self.height = 20
def draw(self):
if g.timee < 5:
pygame.draw.rect(g.d, (0, 255, 0), (self.x, self.y, self.width, self.height))
else:
pygame.draw.rect(g.d, (255, 0, 0), (self.x, self.y, self.width, self.height))
def on_base(self):
if b.y >= self.y - b.side:
b.y = self.y - b.side
b.is_jumping = False
class Platforms:
def __init__(self):
self.x = random.randint(1, 1100)
self.y = random.randint(-300, 0)
self.width = 100
self.height = 20
self.speed = random.randint(1, 4)
def draw(self):
pygame.draw.rect(g.d, (0, 0, 0), (self.x, self.y, self.width, self.height))
def move(self):
self.y += self.speed
ba = Base()
b = Block()
g = Game()
for i in range(10):
g.platlist.append(Platforms())
game_loop = True
while game_loop:
g.timee += 0.01
events = pygame.event.get()
g.d.fill((98, 98, 98))
b.draw()
b.move()
b.jump_logic()
b.fall()
if b.is_jumping:
b.jump()
ba.draw()
ba.on_base()
g.game_over()
for plats in g.platlist:
plats.draw()
plats.move()
g.win.flip()
pygame.time.Clock().tick(60)
main()
我没有看到任何明显的东西,所以我猜是由于你的时间延迟机制。你能 post 说说你是怎么拖延的吗?您的延迟时间不得干扰 rendering/drawing。这是一个例子。
def gameloop1(): # delay interfering with drawing
# delay by some long loop
i = 0
while i < 10000:
i = i + 1
if i == 10000:
# execute some function
# draw stuff
i = 0
def gameloop2():
i = i + 1
if i == 10000:
# execute some function
# draw stuff
在示例中,gameloop1 在每次抽奖之前都会有一些延迟,而 gameloop2 不会并且仍然允许您执行延迟。也不要将此增量用作延迟机制,因为它会根据不同的系统速度而变化,请改用时基。
我明白了。这是因为我的 pygame.time.Clock().tick(60)
在游戏循环中。不确定为什么这会导致延迟,但对于可能遇到相同问题的任何人,请不要将其放在主循环中。