我的 pygame 代码 运行 非常慢,我该如何解决?
My pygame code is running horribly slow, how can I fix it?
所以我试图在 pygame 中制作俄罗斯方块,但 运行 真的很慢。我隔离了绘制所有内容的函数,甚至 运行 真的很慢,我该如何解决这个问题? (运行 我的意思是我出现黑屏,所需的图像在几分钟内从底部缓慢加载。)
P.S。有一些未使用的变量,因为我懒得把它们注释掉,但我不认为几个变量会产生很大的不同
import random
import time
import pygame
pygame.init()
win=pygame.display.set_mode((420, 840))
clock= pygame.time.Clock()
score= 0
level= 0
lines= 0
field= [[1]*10 for _ in range(22)]
run= True
play= True
setState= False
t= 48
frameCounter= 0
font= pygame.font.SysFont('arial', 30)
def drawWin():
tempY= 40
for y in range(2 ,22):
tempX= 10
for x in field[y]:
if(x==1):
win.fill((255, 255, 255), (tempX, tempY, 40, 40))
pygame.draw.rect(win, (0, 0, 255), (tempX, tempY, 40, 40), 5)
tempX+=40
tempY+=40
pygame.draw.rect(win, (255, 255, 255), (10, 40, 400, 800), 2)
text= font.render('Score: '+ str(score)+ '\nLevel: '+ str(level), 1, (255, 255, 255))
while True:
drawWin()
for event in pygame.event.get():
if event.type== pygame.QUIT:
break
time.sleep(1)
clock.tick(t)
pygame.quit()
简单修复,忘记了 pygame 的更新显示命令。
尝试将 pygame.display.update()
添加到 while 循环的末尾
我之前遇到过类似的问题 运行 Pygame :))
所以我试图在 pygame 中制作俄罗斯方块,但 运行 真的很慢。我隔离了绘制所有内容的函数,甚至 运行 真的很慢,我该如何解决这个问题? (运行 我的意思是我出现黑屏,所需的图像在几分钟内从底部缓慢加载。)
P.S。有一些未使用的变量,因为我懒得把它们注释掉,但我不认为几个变量会产生很大的不同
import random
import time
import pygame
pygame.init()
win=pygame.display.set_mode((420, 840))
clock= pygame.time.Clock()
score= 0
level= 0
lines= 0
field= [[1]*10 for _ in range(22)]
run= True
play= True
setState= False
t= 48
frameCounter= 0
font= pygame.font.SysFont('arial', 30)
def drawWin():
tempY= 40
for y in range(2 ,22):
tempX= 10
for x in field[y]:
if(x==1):
win.fill((255, 255, 255), (tempX, tempY, 40, 40))
pygame.draw.rect(win, (0, 0, 255), (tempX, tempY, 40, 40), 5)
tempX+=40
tempY+=40
pygame.draw.rect(win, (255, 255, 255), (10, 40, 400, 800), 2)
text= font.render('Score: '+ str(score)+ '\nLevel: '+ str(level), 1, (255, 255, 255))
while True:
drawWin()
for event in pygame.event.get():
if event.type== pygame.QUIT:
break
time.sleep(1)
clock.tick(t)
pygame.quit()
简单修复,忘记了 pygame 的更新显示命令。
尝试将 pygame.display.update()
添加到 while 循环的末尾
我之前遇到过类似的问题 运行 Pygame :))