如何防止程序错误解释pygame中的输入?

2024-10-01 04:54:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个简单的井字游戏在pygame为我的学校项目。我设法整理了一些代码(抱歉,如果它有点混乱,它可能是我试图用来修复我的当前问题的代码的剩余部分),在某种程度上工作如预期。然而,我确实遇到了一个奇怪的错误,我不知道为什么,但似乎我的程序不能处理快速用户输入(如果用户点击相当快),而不是把一个圆圈然后一个十字架它可能会放2个圆圈或2个十字架。如果用户走得很慢,它的工作很好,但我正在寻找一种方法来摆脱这个问题,如果有人可以提示我对一些事情,我会非常感谢。以下是源代码:

import pygame
import sys
from pygame.locals import *

DISPLAY_WIDTH = 400
DISPLAY_HEIGHT = 400
BOX_SIZE = 50
BOARD_WIDTH = 3
BOARD_HEIGHT = 3


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255,   0,   0)
BACKGROUND_COLOR = (198, 187, 133)
indexList = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]

gameFinished = False


def game_loop():
    global DISPLAY_WIDTH, DISPLAY_HEIGHT, displaySurface, gameFinished

    pygame.init()
    FPSCLOCK = pygame.time.Clock()

    displaySurface = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
    pygame.display.set_caption("Tic Tac Toe")
    displaySurface.fill(BACKGROUND_COLOR)
    pygame.draw.line(displaySurface, BLACK, (140, 20), (140, 380), 10)
    pygame.draw.line(displaySurface, BLACK, (260, 20), (260, 380), 10)
    pygame.draw.line(displaySurface, BLACK, (20, 140), (380, 140), 10)
    pygame.draw.line(displaySurface, BLACK, (20, 260), (380, 260), 10)
    box1 = pygame.Rect(20, 20, 120, 120)
    box2 = pygame.Rect(140, 20, 120,120)
    box3 = pygame.Rect(260, 20, 120, 120)
    box4 = pygame.Rect(20, 140, 120, 120)
    box5 = pygame.Rect(140, 140, 120, 120)
    box6 = pygame.Rect(260, 140, 120, 120)
    box7 = pygame.Rect(20, 260, 120, 120)
    box8 = pygame.Rect(140, 260, 120, 120)
    box9 = pygame.Rect(260, 260, 120, 120)
    circle = pygame.image.load("circle.png")
    cross = pygame.image.load("cross.png")
    boardBoxes = [box1, box2, box3,  box4, box5, box6, box7, box8, box9]
    mouseY = 0
    mouseX = 0
    placesTaken = {0: False, 1: False, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False}

    turn = 1
    board = generate_boxes(0)
    print(repr(board))

    while not gameFinished:
        mouseClick = False
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                mouseX, mouseY = event.pos
            elif event.type == MOUSEBUTTONUP:
                mouseClick = True
                mouseX, mouseY = event.pos
            if mouseClick is True and turn == 1:
                get_mouse_overbox(mouseX, mouseY, boardBoxes, turn, board)
                print("Player")
                turn += 1
            elif mouseClick is True and turn == 2:
                get_mouse_overbox(mouseX, mouseY, boardBoxes, turn, board)
                turn -= 1

        if board[0] == 1 and placesTaken[0] is False:
            put_image(circle, 20, 20)
            placesTaken[0] = True
        elif board[0] == 2 and placesTaken[0] is False:
            put_image(cross, 20, 20)
            placesTaken[0] = True
        if board[1] == 1 and placesTaken[1] is False:
            put_image(circle, 140, 20)
            placesTaken[1] = True
        elif board[1] == 2 and placesTaken[1] is False:
            put_image(cross, 140, 20)
            placesTaken[1] = True
        if board[2] == 1 and placesTaken[2] is False:
            put_image(circle, 260, 20)
            placesTaken[2] = True
        elif board[2] == 2 and placesTaken[2] is False:
            put_image(cross, 260, 20)
            placesTaken[2] = True
        if board[3] == 1 and placesTaken[3] is False:
            put_image(circle, 20, 140)
            placesTaken[3] = True
        elif board[3] == 2 and placesTaken[3] is False:
            put_image(cross, 20, 140)
            placesTaken[3] = True
        if board[4] == 1 and placesTaken[4] is False:
            put_image(circle, 140, 140)
            placesTaken[4] = True
        elif board[4] == 2 and placesTaken[4] is False:
            put_image(cross, 140, 140)
            placesTaken[4] = True
        if board[5] == 1 and placesTaken[5] is False:
            put_image(circle, 260, 140)
            placesTaken[5] = True
        elif board[5] == 2 and placesTaken[5] is False:
            put_image(cross, 260, 140)
            placesTaken[5] = True
        if board[6] == 1 and placesTaken[6] is False:
            put_image(circle, 20, 260)
            placesTaken[6] = True
        elif board[6] == 2 and placesTaken[6] is False:
            put_image(cross, 20, 260)
            placesTaken[6] = True
        if board[7] == 1 and placesTaken[7] is False:
            put_image(circle, 140, 260)
            placesTaken[7] = True
        elif board[7] == 2 and placesTaken[7] is False:
            put_image(cross, 140, 260)
            placesTaken[7] = True
        if board[8] == 1 and placesTaken[8] is False:
            put_image(circle, 260, 260)
            placesTaken[8] = True
        elif board[8] == 2 and placesTaken[8] is False:
            put_image(cross, 260, 260)
            placesTaken[8] = True

        FPSCLOCK.tick(60)
        pygame.display.update()


def put_image(image_name, x_cord, y_cord):
    global displaySurface
    displaySurface.blit(image_name, (x_cord, y_cord))


def generate_boxes(value):
    boxesFilled = []
    for i in range(9):
        boxesFilled.append(value)
    return boxesFilled


def board_getcircles(board):
    onlyCircleBoard = [0 if i == 2 else i for i in board]
    return onlyCircleBoard


def board_getcrosses(board):
    onlyCrossBoard = [0 if i == 2 else i for i in board]
    return onlyCrossBoard


def get_mouse_overbox(mouse_x, mouse_y, box_list, which_turn, boardlist):
    if which_turn == 1:
        for counter in range(len(box_list)):
            if box_list[counter].collidepoint(mouse_x, mouse_y):
                boardlist[counter] = 1
    elif which_turn == 2:
        for caunter in range(len(box_list)):
            if box_list[caunter].collidepoint(mouse_x, mouse_y):
                boardlist[caunter] = 2

game_loop()

Tags: andimageboardfalsetrueifputis
2条回答

如果有人想找到答案或只是有一个类似的问题,这里是代码后,我改变了它(它现在的工作,感谢答案shuttle87)

import pygame
import sys
from pygame.locals import *

DISPLAY_WIDTH = 400
DISPLAY_HEIGHT = 400
BOX_SIZE = 50
BOARD_WIDTH = 3
BOARD_HEIGHT = 3


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255,   0,   0)
BACKGROUND_COLOR = (198, 187, 133)
indexList = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]

gameFinished = False


def game_loop():
    global DISPLAY_WIDTH, DISPLAY_HEIGHT, displaySurface, gameFinished

    pygame.init()
    FPSCLOCK = pygame.time.Clock()

    displaySurface = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
    pygame.display.set_caption("Tic Tac Toe")
    displaySurface.fill(BACKGROUND_COLOR)
    pygame.draw.line(displaySurface, BLACK, (140, 20), (140, 380), 10)
    pygame.draw.line(displaySurface, BLACK, (260, 20), (260, 380), 10)
    pygame.draw.line(displaySurface, BLACK, (20, 140), (380, 140), 10)
    pygame.draw.line(displaySurface, BLACK, (20, 260), (380, 260), 10)
    box1 = pygame.Rect(20, 20, 120, 120)
    box2 = pygame.Rect(140, 20, 120,120)
    box3 = pygame.Rect(260, 20, 120, 120)
    box4 = pygame.Rect(20, 140, 120, 120)
    box5 = pygame.Rect(140, 140, 120, 120)
    box6 = pygame.Rect(260, 140, 120, 120)
    box7 = pygame.Rect(20, 260, 120, 120)
    box8 = pygame.Rect(140, 260, 120, 120)
    box9 = pygame.Rect(260, 260, 120, 120)
    circle = pygame.image.load("circle.png")
    cross = pygame.image.load("cross.png")
    boardBoxes = [box1, box2, box3,  box4, box5, box6, box7, box8, box9]
    mouseY = 0
    mouseX = 0
    placesTaken = {0: False, 1: False, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False}

    turn = 1
    board = generate_boxes(0)
    print(repr(board))

    while not gameFinished:
        moveDone = False
        mouseClick = False






        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONUP:
                mouseClick = True
                mouseX, mouseY = event.pos
            if mouseClick is True and turn == 1 and moveDone == False:
                get_mouse_overbox(mouseX, mouseY, boardBoxes, turn, board)
                print("Player1")
                moveDone = True
            elif mouseClick is True and turn == 2 and moveDone == False:
                get_mouse_overbox(mouseX, mouseY, boardBoxes, turn, board)
                print("Player2")
                moveDone = True

            if board[0] == 1 and placesTaken[0] is False:
                put_image(circle, 20, 20)
                placesTaken[0] = True
                turn += 1
            elif board[0] == 2 and placesTaken[0] is False:
                put_image(cross, 20, 20)
                placesTaken[0] = True
                turn -= 1
            if board[1] == 1 and placesTaken[1] is False:
                put_image(circle, 140, 20)
                placesTaken[1] = True
                turn += 1
            elif board[1] == 2 and placesTaken[1] is False:
                put_image(cross, 140, 20)
                placesTaken[1] = True
                turn -= 1
            if board[2] == 1 and placesTaken[2] is False:
                put_image(circle, 260, 20)
                placesTaken[2] = True
                turn += 1
            elif board[2] == 2 and placesTaken[2] is False:
                put_image(cross, 260, 20)
                placesTaken[2] = True
                turn -= 1
            if board[3] == 1 and placesTaken[3] is False:
                put_image(circle, 20, 140)
                placesTaken[3] = True
                turn += 1
            elif board[3] == 2 and placesTaken[3] is False:
                put_image(cross, 20, 140)
                placesTaken[3] = True
                turn -= 1
            if board[4] == 1 and placesTaken[4] is False:
                put_image(circle, 140, 140)
                placesTaken[4] = True
                turn += 1
            elif board[4] == 2 and placesTaken[4] is False:
                put_image(cross, 140, 140)
                placesTaken[4] = True
                turn -= 1
            if board[5] == 1 and placesTaken[5] is False:
                put_image(circle, 260, 140)
                placesTaken[5] = True
                turn += 1
            elif board[5] == 2 and placesTaken[5] is False:
                put_image(cross, 260, 140)
                placesTaken[5] = True
                turn -= 1
            if board[6] == 1 and placesTaken[6] is False:
                put_image(circle, 20, 260)
                placesTaken[6] = True
                turn += 1
            elif board[6] == 2 and placesTaken[6] is False:
                put_image(cross, 20, 260)
                placesTaken[6] = True
                turn -= 1
            if board[7] == 1 and placesTaken[7] is False:
                put_image(circle, 140, 260)
                placesTaken[7] = True
                turn += 1
            elif board[7] == 2 and placesTaken[7] is False:
                put_image(cross, 140, 260)
                placesTaken[7] = True
                turn -= 1
            if board[8] == 1 and placesTaken[8] is False:
                put_image(circle, 260, 260)
                placesTaken[8] = True
                turn += 1
            elif board[8] == 2 and placesTaken[8] is False:
                put_image(cross, 260, 260)
                placesTaken[8] = True
                turn -= 1


        FPSCLOCK.tick(60)
        pygame.display.update()


def get_mouse_overbox(mouse_x, mouse_y, box_list, which_turn, boardlist):
    if which_turn == 1:
        for counter in range(len(box_list)):
            if box_list[counter].collidepoint(mouse_x, mouse_y) and which_turn == 1:
                boardlist[counter] = 1
    elif which_turn == 2:
        for caunter in range(len(box_list)):
            if box_list[caunter].collidepoint(mouse_x, mouse_y) and which_turn == 2:
                boardlist[caunter] = 2


def put_image(image_name, x_cord, y_cord):
    global displaySurface
    displaySurface.blit(image_name, (x_cord, y_cord))


def generate_boxes(value):
    boxesFilled = []
    for i in range(9):
        boxesFilled.append(value)
    return boxesFilled


def board_getcircles(board):
    onlyCircleBoard = [0 if i == 2 else i for i in board]
    return onlyCircleBoard


def board_getcrosses(board):
    onlyCrossBoard = [0 if i == 2 else i for i in board]
    return onlyCrossBoard




game_loop()

作为对我所做工作的简短总结,我将大量的if语句移动到for循环中的事件中,并使其在图片实际放在桌子上时,而不是在玩家单击时,使turn增加或减少1。我做的另一件事是添加了一个moveDone变量,它跟踪改变线路板状态的函数(get\u mouse\u overbox)是否已经在当前循环中启动。希望这能帮助别人,再次感谢你的帮助

pygame.event.get将检索队列中的所有事件,以便您可以一次呈现多个更新。因为在填充图像时不检查轮到谁,所以一次可以填充多个十字或圆。你知道吗

为什么会发生这种情况:

        elif event.type == MOUSEBUTTONUP:
            mouseClick = True
            mouseX, mouseY = event.pos

在循环中mouseClick第一次被设置为True,但是因为队列中有多个事件事件.get()有另一个元素要处理,这次它再次运行循环,mouseclick为真。但这一次我们仍然处于循环中,因为event.get()是一个多列表,所以:

        elif event.type == MOUSEMOTION:
            mouseX, mouseY = event.pos

现在它执行了,但是mouseClick在这里错误地True处理它,所以我们再次陷入另一个语句,这不是您想要的。你知道吗

相关问题 更多 >