[英]Pygame window is really laggy
當我嘗試運行它時,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.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 不會並且仍然允許您執行延遲。 也不要將此增量用作延遲機制,因為它會因不同的系統速度而異,請改用時基。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.