gpt4 book ai didi

python - Pygame 运行几秒钟后变得无响应

转载 作者:行者123 更新时间:2023-12-01 00:07:30 26 4
gpt4 key购买 nike

我正在尝试编写一个 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会有用的。本节描述了一个标准的游戏循环,它应该类似于:

事件处理→更新状态→绘制新状态→更新显示

关于python - Pygame 运行几秒钟后变得无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59866440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com