Joeyboy
Joeyboy

Reputation: 472

Creating pygame menu using mouse.get_pressed()

I want to make a game similar to DopeWars in pygame, which just uses basic interface to make a fairly entertaining game. I want to make a menu box appear when I click a button. With my current code it appears while my mouse is clicked but dissapears once I release mouse button. Is there any way to make the menu (currently just a rect) stay open until I click a back button (not implemented yet).

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()

Upvotes: 1

Views: 2265

Answers (2)

Carson
Carson

Reputation: 8058

I recommend you use pygame-menu (pip install pygame-menu)

pictures of the demo: https://github.com/ppizarror/pygame-menu/tree/master/docs/_static

It also provides some basic examples. Please refer to this link: https://github.com/ppizarror/pygame-menu/tree/master/pygame_menu/examples

document: https://pygame-menu.readthedocs.io/en/latest/

demo

Upvotes: 1

Ethanol
Ethanol

Reputation: 370

Basically, you simply need a way to make it so that the menu stays. One way that I would do it is simply store the state (menu off or on) in a boolean variable. For example:

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

You'll need to add some more to this (such as where the mouse location has to be and more), but the general idea is to store the state of if the menu is opened or not in a variable. Usually if it is an on-off kind of menu, booleans are used, but integers can be used if you have multiple items. Tell me if this solution works for you.

Upvotes: 4

Related Questions