python - Pygame 运行几秒钟后变得无响应
问题描述
我正在尝试编写一个 GUI 来显示 Knights Tour 回溯算法。我有算法工作,但运行 GUI 时,几秒钟后它变得无响应。
import pygame
import time
# Init Gui
pygame.init()
# Window Size
WIN_DIMENSION = 800
win = pygame.display.set_mode((WIN_DIMENSION, WIN_DIMENSION))
pygame.display.set_caption("Knights Tour")
# Board Size
N = 8
TARGETMOVES = N**2
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (180, 0, 0)
def DisplayGui(board, final=False):
win.fill(black)
for i in range(N):
ydraw = i * (WIN_DIMENSION/N)
for n in range(N):
xdraw = n * (WIN_DIMENSION / N)
pygame.draw.rect(win, red, (xdraw, ydraw, WIN_DIMENSION/N, WIN_DIMENSION/N), 3)
displayText(board[i][n], xdraw, ydraw)
while final:
for event in pygame.event.get():
if event.type == pygame.QUIT:
final = False
print("CLOSING")
pygame.display.update()
#time.sleep(1)
def text_objects(text, font):
textSurface = font.render(str(text), True, white)
return textSurface, textSurface.get_rect()
def displayText(text, xdraw, ydraw):
font = pygame.font.Font('freesansbold.ttf', 7 * N)
TextSurf, TextRect = text_objects(text, font)
TextRect.center = ((xdraw + (WIN_DIMENSION / N) / 2), (ydraw + (WIN_DIMENSION / N) / 2))
win.blit(TextSurf, TextRect)
def checkValid(board, movx, movy):
'''
params:
movx => next x move
movy => next y move
checks if move is valid
'''
if (movx >= 0 and movy >= 0 and movx < N and movy < N and board[movx][movy] == " "):
return True
return False
def printBoard(board):
'''
Prints Board
'''
for i in range(len(board)):
print(board[i])
def KnightsTour():
currx = 0
curry = 0
# Init board
board = [[" " for i in range(N)] for i in range(N)]
xmoves = [-2, -2, -1, -1, 1, 1, 2, 2]
ymoves = [1, -1, 2, -2, 2, -2, 1, -1]
totalmoves = 1
board[0][0] = 0
DisplayGui(board)
if generateMove(board, currx, curry, totalmoves, xmoves, ymoves):
printBoard(board)
DisplayGui(board, True)
else: print("Invalid")
def generateMove(board, currx, curry, totalmoves, xmoves, ymoves):
if totalmoves == TARGETMOVES:
return True
print("X: {} <> Y: {}".format(currx, curry)) # draw board here
DisplayGui(board)
for i in range(8):
nextx = currx + xmoves[i]
nexty = curry + ymoves[i]
if checkValid(board, nextx, nexty):
board[nextx][nexty] = totalmoves
if generateMove(board, nextx, nexty, totalmoves+1, xmoves, ymoves):
return True
# backtracking
board[nextx][nexty] = " "
return False
if __name__ == "__main__":
KnightsTour()
我尝试使用 sys.sleep() 减慢它的速度,但这并没有帮助。我不知道是什么导致 GUI 冻结。算法仍然在后台运行。
解决方案
如果你想在不显着改变代码的情况下克服这个问题,你可以在你的函数中调用pygame.event.pump( ) 。DisplayGui()
这将使您的操作系统/窗口管理器相信您的程序没有冻结。
def DisplayGui(board, final=False):
win.fill(black)
if not final:
pygame.event.pump()
for i in range(N):
...
实际处理 QUIT 事件会更有帮助,这样您就可以在不结束 python 进程的情况下提前结束程序。
如果您正在考虑重新组织您的代码,那么这本openbook可能会很有用。本节描述了一个标准的游戏循环,看起来应该是这样的:
事件处理→更新状态→绘制新状态→更新显示
推荐阅读
- sql - 如何根据外部函数的结果更新表中的列?
- sql-server - SQL Server:查找 ReportServer 目录报告参数和参数的数据集
- dll - KeyDown 程序抛出 2 个编译错误
- java - 从firebase存储中一张一张下载多张图片
- amazon-web-services - 将 SSM 会话管理器的会话输出保存到另一个 AWS 账户中的 S3 存储桶
- telerik - Telerik 报表设计器 - 如何从数据源值中删除空行?
- javascript - 不会呈现异步加载的数据
- bash - 如何打印比赛后面的所有内容?
- windows - 收到消息“psql 未被识别为内部或外部命令”
- r - 将函数应用于数据框时出现错误消息