Pygame窗口真的很滞后



当我尝试运行pygame窗口时,它变得非常滞后。到目前为止,我有 132 行代码,我还有很多东西要添加。在测试运行时,我注意到当我添加以下代码时,游戏滞后了

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

我想通了。 这是因为我的pygame.time.Clock().tick(60)在游戏循环中。不知道为什么这会导致滞后,但对于可能遇到相同问题的任何人,请不要将其放在主循环中。

我没有看到任何明显的东西,所以我的猜测是由于你的延时机制。 你能发布你是如何延迟的吗? 延迟时间不得干扰渲染/绘制。 下面是一个示例。

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 不会并且仍然允许您执行延迟。 也不要将这种增量用作延迟机制,因为它会因不同的系统速度而异,请改用时基。