pygame.display.update在pygame.time.wait(1000)之后运行

2024-09-24 10:28:04 发布

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

我正在完成一个教程。 教程可以在这里找到https://inventwithpython.com/makinggames.pdf

我指的主要脚本可以在这里找到 https://inventwithpython.com/memorypuzzle.py

这个程序中有一个bug,我已经尝试解决了好几天了。 在主游戏循环中

`

while True: # main game loop
    mouseClicked = False

    DISPLAYSURF.fill(BGCOLOR) # drawing the window
    drawBoard(mainBoard, revealedBoxes)

    for event in pygame.event.get(): # event handling loop
        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:
            mousex, mousey = event.pos
            mouseClicked = True

    boxx, boxy = getBoxAtPixel(mousex, mousey)
    if boxx != None and boxy != None:
        # The mouse is currently over a box.
        if not revealedBoxes[boxx][boxy]:
            drawHighlightBox(boxx, boxy)
        if not revealedBoxes[boxx][boxy] and mouseClicked:
            revealBoxesAnimation(mainBoard, [(boxx, boxy)])
            revealedBoxes[boxx][boxy] = True # set the box as "revealed"
            if firstSelection == None: # the current box was the first box clicked
                firstSelection = (boxx, boxy)
            else: # the current box was the second box clicked
                # Check if there is a match between the two icons.
                icon1shape, icon1color = getShapeAndColor(mainBoard, firstSelection[0], firstSelection[1])
                icon2shape, icon2color = getShapeAndColor(mainBoard, boxx, boxy)

                if icon1shape != icon2shape or icon1color != icon2color:

                    # Icons don't match. Re-cover up both selections.
                    # I think the problem is here
                    # I think the problem is here

                    pygame.time.wait(1000) # 1000 milliseconds = 1 sec
                    coverBoxesAnimation(mainBoard, [(firstSelection[0], firstSelection[1]), (boxx, boxy)])
                    revealedBoxes[firstSelection[0]][firstSelection[1]] = False
                    revealedBoxes[boxx][boxy] = False
                elif hasWon(revealedBoxes): # check if all pairs found
                    gameWonAnimation(mainBoard)
                    pygame.time.wait(2000)

                    # Reset the board
                    mainBoard = getRandomizedBoard()
                    revealedBoxes = generateRevealedBoxesData(False)

                    # Show the fully unrevealed board for a second.
                    drawBoard(mainBoard, revealedBoxes)
                    pygame.display.update()
                    pygame.time.wait(1000)

                    # Replay the start game animation.
                    startGameAnimation(mainBoard)
                firstSelection = None # reset firstSelection variable

    # Redraw the screen and wait a clock tick.
    pygame.display.update()
    FPSCLOCK.tick(FPS)

` 发生了什么事

pygame.display.update()将显示所选的第二张牌,但只显示一小部分秒

我试图实现的行为=>; 我试图实现的行为是,当用户单击它显示的第一张卡时,然后当用户单击它显示的第二张卡时。然后,游戏使用pygame.time.wait(1000)等待一秒钟。这是为了在循环继续执行之前的设定时间内显示两张卡,如果它们不相等,则再次覆盖两张卡

我想告诉作者,这样就不会有其他人陷入困境


Tags: andtheboxnoneeventfalseiftype