Programmer
Programmer

Reputation: 314

How to completely clear pygame window with button

I made this program:

import pygame
pygame.init()

WIDTH = 600
HEIGHT = 700

RED = (255, 0, 0)
BLACK = (0, 0, 0)
GREY = (211, 211, 211)
GREEN = (0, 255, 0)

win = pygame.display.set_mode((WIDTH, HEIGHT))
win.fill(GREY)

def buttons(buttonx, buttony, buttonw, buttonh, color, msg, size): # left, top, width, height, color, message, font size
    pos = pygame.mouse.get_pos()
    fontb = pygame.font.SysFont("arial", size)
    text = fontb.render(msg, True, BLACK)

    outline = pygame.Rect(buttonx - 2, buttony - 2, buttonw + 4, buttonh + 4)
    win.fill(BLACK, outline)
    button = pygame.Rect(buttonx, buttony, buttonw, buttonh)
    win.fill(color, button)

    textplace = text.get_rect(center=(buttonx + buttonw/2, buttony + buttonh/2))
    win.blit(text, textplace)

    if button.collidepoint(pos):
        win.fill(GREEN, button)
        win.blit(text, textplace)
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                pass
                # this block of code should make window grey

def main_menu():
    font1 = pygame.font.SysFont("arial", 45)
    welcoming = font1.render("Welcome!", True, BLACK, GREY)
    wRect = welcoming.get_rect(center=(WIDTH / 2, 75))
    win.blit(welcoming, wRect)

    buttons(100, 150, 400, 100, RED, "Play", 60)
    buttons(100, 300, 175, 100, RED, "Options", 40)
    buttons(325, 300, 175, 100, RED, "Instructions", 30)
    buttons(100, 450, 175, 100, RED, "Leaderboard", 30)# left, top, width, height, color, message
    buttons(325, 450, 175, 100, RED, "Quit", 60)

main = True
while main:
    main_menu()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False

    pygame.display.update()

If any of the buttons is pressed, the window should turn completely grey (all buttons and texts should disappear). I have tried to put

win.fill(GREY)
pygame.display.update()

in place of

pass
# this block of code should make window grey

in lines 34 and 35, but it does not work. I have also tried to make some kind of functions that return grey window which is same size as the original window and then blit it on the main menu, but it doesn't work either. Can anyone help?

Upvotes: 2

Views: 661

Answers (1)

Michael Noguera
Michael Noguera

Reputation: 411

The code you are trying succeeds in filling the window with grey. However, the blank grey screen then has the menu drawn on top of it immediately by your main loop.

Using an additional global variable, current_screen, it is possible to prevent this from happening. The main loop now checks current_screen to see which screen it should draw before doing so, ensuring that the menu will not be drawn when it is not supposed to be shown.

This following code worked for me, but let me know if you have any problems with it.

import pygame
pygame.init()

# declare final variables
WIDTH = 600
HEIGHT = 700

RED = (255, 0, 0)
BLACK = (0, 0, 0)
GREY = (211, 211, 211)
GREEN = (0, 255, 0)

# create window
win = pygame.display.set_mode((WIDTH, HEIGHT))
win.fill(GREY)

# left, top, width, height, color, message, font size


def buttons(buttonx, buttony, buttonw, buttonh, color, msg, size):
    global win, current_screen

    pos = pygame.mouse.get_pos()
    fontb = pygame.font.SysFont("arial", size)
    text = fontb.render(msg, True, BLACK)

    # draw button outline and fill
    outline = pygame.Rect(buttonx - 2, buttony - 2, buttonw + 4, buttonh + 4)
    win.fill(BLACK, outline)
    button = pygame.Rect(buttonx, buttony, buttonw, buttonh)
    win.fill(color, button)

    # draw button text
    textplace = text.get_rect(
        center=(buttonx + buttonw/2, buttony + buttonh/2))
    win.blit(text, textplace)

    if button.collidepoint(pos):  # button mouse-hover
        win.fill(GREEN, button)
        win.blit(text, textplace)
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:  # button pressed
                current_screen = "blank"


def main_menu():
    # draw welcome message
    font1 = pygame.font.SysFont("arial", 45)
    welcoming = font1.render("Welcome!", True, BLACK, GREY)
    wRect = welcoming.get_rect(center=(WIDTH / 2, 75))
    win.blit(welcoming, wRect)

    # draw buttons
    buttons(100, 150, 400, 100, RED, "Play", 60)
    buttons(100, 300, 175, 100, RED, "Options", 40)
    buttons(325, 300, 175, 100, RED, "Instructions", 30)
    # left, top, width, height, color, message
    buttons(100, 450, 175, 100, RED, "Leaderboard", 30)
    buttons(325, 450, 175, 100, RED, "Quit", 60)


main = True
current_screen = "main menu"

while main:
    # draw the current screen
    if current_screen == "main menu":
        main_menu()
    elif current_screen == "blank":
        win.fill(GREY)
    else:
        raise NotImplementedError("There is no behavior defined for the current screen, "+current_screen+", in the main loop!")

    # end main loop on window close
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False

    pygame.display.update()

Upvotes: 2

Related Questions