gpt4 book ai didi

python - 为什么无法在 pygame 中输入任何内容?

转载 作者:行者123 更新时间:2023-12-01 01:08:57 25 4
gpt4 key购买 nike

我正在尝试创建一个井字游戏。
函数“board_print”显示游戏板,但由于某种原因我无法单击右上角的“退出”按钮或单击板上的任意位置...为什么?

pygame.init()
LEFT = 1
SCROLL = 2
RIGHT = 3
backgrouמd = [255, 174, 201]
black = (0,0,0)
line_color = (0, 0, 255)
w = 800
h = 800
X_img = r'C:\Users\aviro\Desktop\coollogo_com-20139270.png'
O_img = r'C:\Users\aviro\Desktop\coollogo_com-1453599.png'
o_pose_list = []
x_pose_list = []
finish = False
size = (w, h)
screen = pygame.display.set_mode(size)
game_end_count = 0
slash = 0
backslash = 0
row0 = 0
row1 = 0
row2 = 0
line0 = 0
line1 = 0
line2 = 0
board = ([[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']])
ans_list = []
x_key = ('0', '1', '2')
user_ans = ""
o_image = pygame.image.load(O_img).convert_alpha()
x_image = pygame.image.load(X_img)
x_turn = False
# game engine
while game_end_count <= 9 or not finish:
for pose in x_pose_list:
screen.blit(x_image, pose)
for pose in o_pose_list:
screen.blit(o_image, pose)
board_print()
x_turn = not x_turn
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
elif x_turn:
while True:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == LEFT:
x_pose_list.append(pygame.mouse.get_pos())
break
elif not x_turn:
while True:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == LEFT:
o_pose_list.append(pygame.mouse.get_pos())
break

enter image description here

最佳答案

删除事件循环内的进程循环,该循环会阻塞应用程序。单个事件通过 event in pygame.event.get() 获取。如果不调用它,您将不会收到任何新事件。

有 1 个主循环就完全足够了:

while game_end_count <= 9 or not finish:

主循环内有 1 个事件循环:

for event in pygame.event.get():

主循环甚至事件循环内的每个进程循环都是无用的并且是糟糕的设计。

while game_end_count <= 9 or not finish:

for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == LEFT:
if x_turn:
x_pose_list.append(pygame.mouse.get_pos())
else:
o_pose_list.append(pygame.mouse.get_pos())

for pose in x_pose_list:
screen.blit(x_image, pose)
for pose in o_pose_list:
screen.blit(o_image, pose)
board_print()

x_turn = not x_turn

关于python - 为什么无法在 pygame 中输入任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55042267/

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