【问题标题】:Why can't input anything in a pygame?为什么不能在pygame中输入任何内容?
【发布时间】:2019-03-07 11:03:01
【问题描述】:

我正在尝试创建井字游戏。
函数“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

【问题讨论】:

  • 很久以前就解决了

标签: python pygame


【解决方案1】:

删除事件循环内的进程循环,此循环会阻塞应用程序。单个事件由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

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多