【问题标题】:Creating pygame menu using mouse.get_pressed()使用 mouse.get_pressed() 创建 pygame 菜单
【发布时间】:2018-03-25 23:56:46
【问题描述】:

我想在pygame中制作一个类似于DopeWars的游戏,它只是使用基本的界面来制作一个相当有趣的游戏。我想在单击按钮时出现一个菜单框。使用我当前的代码,它会在单击鼠标时出现,但一旦我释放鼠标按钮就会消失。有什么方法可以让菜单(目前只是一个矩形)保持打开状态,直到我点击一个后退按钮(尚未实现)。

import pygame

pygame.init()

display_width = 400
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
background = (72,76,81)
button_inactive = (99,105,114)
button_active = (84,89,96)

game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Ay lmao')


clock = pygame.time.Clock()

def button(msg,button_x,button_y,button_w,button_h,button_a,button_in,action = None):

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if button_x + button_w > mouse[0] > button_x and button_y + button_h > mouse[1] > button_y:
        pygame.draw.rect(game_display,button_a,(button_x,button_y,button_w,button_h))
        if click[0] == 1 and action != None:            
            action()             
    else:
        pygame.draw.rect(game_display,button_in,(button_x,button_y,button_w,button_h))

    small_text = pygame.font.Font("freesansbold.ttf",20)
    text_surf, text_rect = text_objects(msg, small_text)
    text_rect.center = ((button_x+(button_w/2)), (button_y + 50/2))
    game_display.blit(text_surf,text_rect)


def move():
    pygame.draw.rect(game_display,button_inactive,[100,100,200,300])


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

def game_loop():
    gameExit = False
    while not gameExit:

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

        game_display.fill(background)

        mouse = pygame.mouse.get_pos()

        button("Move",15,490,370,50,button_active,button_inactive,move)
        button("Action",15,545,370,50,button_active,button_inactive)


        pygame.display.update()

        clock.tick(90)

    game_loop()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    基本上,您只需要一种方法来保持菜单不变。我会这样做的一种方法是将状态(菜单关闭或打开)简单地存储在布尔变量中。例如:

    menu_opened = False #The variable that sees if the menu should be open.
    while True
        click = pygame.mouse.get_pressed()
            if click[0]: #No need to add == 1
                menu_opened = True
        if menu_opened:
            menu.draw() #Draw menu
    

    您需要为此添加更多内容(例如鼠标位置必须在哪里等等),但总体思路是将菜单是否打开的状态存储在变量中。通常,如果它是一种开关类型的菜单,则使用布尔值,但如果您有多个项目,则可以使用整数。 告诉我这个解决方案是否适合你。

    【讨论】:

    • 嗨!感谢您的建议,它工作得很好。我还使用现有的按钮功能来显示更多按钮,包括“退出菜单”,它只是将 menu_opened 更改为 False。 TYSM
    【解决方案2】:

    我推荐你使用pygame-menu (pip install pygame-menu)

    演示图片:https://github.com/ppizarror/pygame-menu/tree/master/docs/_static

    它还提供了一些基本示例。请参考此链接:https://github.com/ppizarror/pygame-menu/tree/master/pygame_menu/examples

    文档:https://pygame-menu.readthedocs.io/en/latest/

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多