首页 > 解决方案 > Pygame.MOUSEBUTTONDOWN 的问题

问题描述

我是 python 和 Pygame(以及所有编码!)的初学者。我定义了一个交互式按钮

def button(msg, x, y, w, h, ic, ac, action=None):

    mouse = pygame.mouse.get_pos()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:

        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
        smallText = pygame.font.SysFont("comicsans", 25)
        textSurf = smallText.render(msg, True, black)
        textRect = textSurf.get_rect()
        textRect.center = ( (x + (w/2)), (y + ( h/2)))
        gameDisplay.blit(textSurf, textRect)

        if event.type == pygame.MOUSEBUTTONDOWN and action != None:

            action()


    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

        smallText = pygame.font.SysFont("comicsans", 25)
        textSurf = smallText.render(msg, True, black)
        textRect = textSurf.get_rect()
        textRect.center = ( (x + (w/2)), (y + ( h/2)))
        gameDisplay.blit(textSurf, textRect)

当我从介绍屏幕调用按钮时,一切正常

global gameDisplay
intro = True

while intro:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    gameDisplay = pygame.display.set_mode((1060,800))
    pygame.display.set_caption ("Maze")

    gameDisplay.fill(white)
    largeText = pygame.font.SysFont("comicsans", 115)
    smallText = pygame.font.SysFont("comicsans", 50)

    TextSurf = largeText.render("Monster Mazes", True, black)
    TextRect = TextSurf.get_rect()
    TextRect.center = ((1060/2), (800/3))

    TextSurf1 = smallText.render("Collect the food & drink before boarding the spaceship", True, black)
    TextRect1 = TextSurf1.get_rect()
    TextRect1.center = ((1060/2), (800/3*2))


    TextSurf2 = smallText.render("Don't hit the walls!", True, black)
    TextRect2 = TextSurf2.get_rect()
    TextRect2.center = ((1060/2), ((800/3*2)+100))

    gameDisplay.blit(TextSurf, TextRect)
    gameDisplay.blit(TextSurf1, TextRect1)
    gameDisplay.blit(TextSurf2, TextRect2)

    button("Go!", 200, 400, 100, 50, green, bright_green, set_maze)
    button("Quit", 760, 400, 100, 50, red, bright_red, quitgame)

    pygame.display.update()
    clock.tick(15)

但是,当我从屏幕上的游戏中调用它时,它的行为就像总是按下鼠标按钮一样,所以只要光标悬停在按钮上,即使未单击鼠标按钮,也会调用该操作

def game_over():

    global score
    global lives
    global width
    global height
    global maze
    global gameDisplay

    while True:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        bonus_score = lives*50
        total_score = score + bonus_score

        gameDisplay.fill(white)
        largeText = pygame.font.SysFont("comicsans", 115)
        smallText = pygame.font.SysFont("comicsans", 50)
        TextSurf = largeText.render("Game Over!", True, black)
        TextRect = TextSurf.get_rect()
        TextRect.center = ((width/2), (height/3))

        TextSurfScore = smallText.render("Score : " + str(score), True, black)
        TextRectScore = TextSurfScore.get_rect()
        TextRectScore.center = ((width/2), (height/3*2))

        TextSurfScoreBonus = smallText.render("Bonus : " + str(bonus_score), True, black)
        TextRectScoreBonus = TextSurfScoreBonus.get_rect()
        TextRectScoreBonus.center = ((width/2), (height/3*2)+50)

        TextSurfScoreTotal = smallText.render("Total : " + str(total_score), True, black)
        TextRectScoreTotal = TextSurfScoreTotal.get_rect()
        TextRectScoreTotal.center = ((width/2), (height/3*2)+100)

        gameDisplay.blit(TextSurf, TextRect)
        gameDisplay.blit(TextSurfScore, TextRectScore)
        gameDisplay.blit(TextSurfScoreBonus, TextRectScoreBonus)
        gameDisplay.blit(TextSurfScoreTotal, TextRectScoreTotal)

        button("Play again", 200, 400, 100, 50, green, bright_green, play_again)
        button("Quit", 760, 400, 100, 50, red, bright_red, quitgame)

        pygame.display.update()
        clock.tick(30)  

我看不出我做错了什么/不同!我真的很感激任何帮助!

谢谢

标签: pygame

解决方案


我找到了解决方案!游戏结束是一个功能,但介绍不是。我只需要参加全球活动就可以了。


推荐阅读