【问题标题】:Why does my pygame button not always work?为什么我的 pygame 按钮并不总是有效?
【发布时间】:2020-08-10 03:39:06
【问题描述】:

我的前几个按钮工作正常,但我的 CSTWindow 一个有时只能工作。我不愿意发帖,因为我是自学成才的,代码不会完美无缺,任何关于如何让这个按钮一直工作而不是每 10 次点击的建议都将不胜感激。我的部分代码如下。我再次自学它并不完美!

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
    smallText = pygame.font.SysFont("Ariel", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        gameDisplay.blit(background, (x,y))
        largeText = pygame.font.SysFont("Arial",45)
        TextSurf, TextRect = text_objects("Crime Scene and Evidence Examination Game", largeText)
        TextRect.center = ((display_width/2),(display_height/1.2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Scene Selection",150,540,100,50,blue,bright_blue,game_loop)
        button("Quit",550,540,100,50,red,bright_red,quitgame)

        pygame.display.update()

def CSTWindow():
    CSTWindow = True

    while CSTWindow:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        print("Crime Scene")

def TutorialWindow():
    tutorial = True
    while tutorial:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        screen = pygame.display.set_mode((display_width,display_height))
        screen.fill(white)
        screen.blit(TutorialBG, (x,y))
        button("Crime Scene",150,540,100,50,blue,bright_blue,CSTWindow)
        pygame.display.update()

def game_loop():
    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(blue)

        button("Tutorial",(370), (100),100, 50, lightBlue, bright_blue,TutorialWindow)
        pygame.display.update()
        clock.tick(60)

【问题讨论】:

    标签: python events button pygame


    【解决方案1】:

    也许它会更好地处理图像,这是我写的:

    def button(x, y, w, h, inactive, active, action=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
    
        if x + w > mouse[0] > x and y + h > mouse[1] > y:
            gameDisplay.blit(active, (x, y))
            if click[0] == 1 and action is not None:
                action()
        else:
            gameDisplay.blit(inactive, (x, y))
    

    顺便说一句,我看的教程和你一样,但我用的是图片。

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 2019-08-02
      • 2016-11-11
      • 1970-01-01
      • 2018-12-09
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多