我正在尝试创建井字游戏。
函数“ 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
答案 0 :(得分:1)
删除事件循环内的过程循环,此循环会阻塞应用程序。 event in pygame.event.get()
引发了一次单笔事件。如果不这样做,您将不会获得任何新事件。
有一个主循环就足够了:
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