【问题标题】:How Can I Make This Start Button Load My Game?如何让这个开始按钮加载我的游戏?
【发布时间】:2020-06-09 21:06:06
【问题描述】:

我做了一个菜单开始游戏和退出游戏我真的很困惑如何将我的主循环放在一个函数中并在我单击按钮时调用该函数请帮助!我试图将我的主循环放在一个 def 函数中,但我的屏幕会显示为黑色,这里什么也没有显示是我的开始菜单的图像 Video 我的完整代码script

这是我的游戏介绍


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def game_intro():
    red = (200,0,0)
    green = (0,200,0)
    bright_red = (255,0,0)
    bright_green = (0,255,0)

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

        window.fill((255,255,255))
        largeText = pygame.font.Font('BLOODY.ttf',115)
        TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
        TextRect.center = ((800/2), (800/2))
        window.blit(TextSurf, TextRect)

# make the square brighter if collideded with the buttons
        mouse = pygame.mouse.get_pos()
        if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
            pygame.draw.rect(window, bright_green,(150,450,120,50))
        else:
            pygame.draw.rect(window, green,(150,450,120,50))
        if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
            pygame.draw.rect(window, bright_red,(550,450,110,50))
        else:
            pygame.draw.rect(window, red,(550,450,110,50))
# ---------------------------------------------------------------------

        smallText = pygame.font.Font("freesansbold.ttf",20)
        textSurf, textRect = text_objects("Start Game", smallText)
        textRect.center = ( (150+(120/2)), (450+(50/2)) )
        window.blit(textSurf, textRect)


        smallText = pygame.font.Font("freesansbold.ttf",20)
        textSurf, textRect = text_objects("Quit Game", smallText)
        textRect.center = ( (150+(910/2)), (450+(50/2)) )
        window.blit(textSurf, textRect)


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




【问题讨论】:

    标签: python pygame


    【解决方案1】:

    创建一个名为 main_hover 的布尔值,并将其设置为 false。当鼠标悬停在播放按钮上时,将其设置为 true。如果不是,请将其设置为 false。 在您的事件 for 循环中,您需要添加以下内容:

    if event.type == pygame.MOUSEBUTTONUP:
        if main_hover == True:
            main_loop()
    

    做同样的事情,但要退出按钮。 您的代码应如下所示:

    def text_objects(text, font):
        textSurface = font.render(text, True, black)
        return textSurface, textSurface.get_rect()
    
    def game_intro():
        red = (200,0,0)
        green = (0,200,0)
        bright_red = (255,0,0)
        bright_green = (0,255,0)
    
        intro = True
        main_hover = False
        quit_hover = False
        while intro:
            for event in pygame.event.get():
                #print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.MOUSEBUTTONUP:
                    if main_hover == True:
                       main_loop()
                    if quit_hover == True:
                       quit()
    
            window.fill((255,255,255))
            largeText = pygame.font.Font('BLOODY.ttf',115)
            TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
            TextRect.center = ((800/2), (800/2))
            window.blit(TextSurf, TextRect)
    
    # make the square brighter if collideded with the buttons
            mouse = pygame.mouse.get_pos()
            if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
                pygame.draw.rect(window, bright_green,(150,450,120,50))
                main_hover = True
            else:
                pygame.draw.rect(window, green,(150,450,120,50))
                main_hover = False
            if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
                pygame.draw.rect(window, bright_red,(550,450,110,50))
                quit_hover = True 
            else:
                pygame.draw.rect(window, red,(550,450,110,50))
                quit_hover = False
    # ---------------------------------------------------------------------
    
            smallText = pygame.font.Font("freesansbold.ttf",20)
            textSurf, textRect = text_objects("Start Game", smallText)
            textRect.center = ( (150+(120/2)), (450+(50/2)) )
            window.blit(textSurf, textRect)
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2023-01-10
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      相关资源
      最近更新 更多