Почему моя кнопка Pygame не всегда работает? - PullRequest
0 голосов
/ 26 апреля 2020

Мои первые несколько кнопок работают нормально, но моя CSTWindow работает только иногда. Я неохотно пишу, потому что я самоучка, и код не будет безупречным, любой совет будет высоко цениться о том, как заставить эту кнопку работать все время, а не только каждые 10i sh клики. Части моего кода ниже. Я опять самоучка не идеальна!

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)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...