Python 3.x 如何使用按钮完全清除pygame窗口

Python 3.x 如何使用按钮完全清除pygame窗口,python-3.x,pygame,Python 3.x,Pygame,我制作了这个节目: 导入pygame pygame.init() 宽度=600 高度=700 红色=(255,0,0) 黑色=(0,0,0) 灰色=(211211211211) 绿色=(0,255,0) win=pygame.display.set_模式((宽度、高度)) win.fill(灰色) def按钮(buttonx、buttony、buttonw、buttonh、颜色、消息、大小):#左侧、顶部、宽度、高度、颜色、消息、字体大小 pos=pygame.mouse.get_pos() f

我制作了这个节目:

导入pygame
pygame.init()
宽度=600
高度=700
红色=(255,0,0)
黑色=(0,0,0)
灰色=(211211211211)
绿色=(0,255,0)
win=pygame.display.set_模式((宽度、高度))
win.fill(灰色)
def按钮(buttonx、buttony、buttonw、buttonh、颜色、消息、大小):#左侧、顶部、宽度、高度、颜色、消息、字体大小
pos=pygame.mouse.get_pos()
fontb=pygame.font.SysFont(“arial”,大小)
text=fontb.render(消息、真、黑)
outline=pygame.Rect(buttonx-2,buttony-2,buttonnw+4,buttonh+4)
win.fill(黑色,轮廓)
button=pygame.Rect(buttonx、buttony、buttonw、buttonh)
win.fill(颜色、按钮)
textplace=text.get_rect(中间=(buttonx+buttonnw/2,buttony+buttonh/2))
win.blit(text,textplace)
if按钮碰撞点(位置):
win.fill(绿色,按钮)
win.blit(text,textplace)
对于pygame.event.get()中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
通过
#这段代码应该使窗口变成灰色
def主菜单():
font1=pygame.font.SysFont(“arial”,45)
欢迎=font1.render(“欢迎!”,True,黑色,灰色)
wRect=welcoming.get_rect(中心=(宽度/2,75))
win.blit(欢迎,wRect)
按钮(100150400100,红色,“播放”,60)
按钮(100300175100,红色,“选项”,40)
按钮(325300175100,红色,“说明”,30)
按钮(100450175100,红色,“排行榜”,30)#左侧,顶部,宽度,高度,颜色,信息
按钮(325450175100,红色“退出”,60)
main=True
虽然主要:
主菜单()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
main=False
pygame.display.update()
如果按下任何按钮,窗口应完全变为灰色(所有按钮和文本应消失)。我试着把

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

pass
# this block of code should make window grey

在第34行和第35行中,但它不起作用。我还尝试了一些函数,返回与原始窗口大小相同的灰色窗口,然后在主菜单上进行blit,但也不起作用。有人能帮忙吗?

您正在尝试的代码成功地将窗口填充为灰色。但是,灰色的灰色屏幕,然后有菜单绘制在它的顶部由你的主循环。< /P> 使用另一个全局变量,
current\u screen
,可以防止这种情况发生。主循环现在检查
当前屏幕
,查看在执行此操作之前应绘制哪个屏幕,确保在不应显示菜单时不会绘制该菜单

下面的代码对我有用,但是如果您有任何问题,请告诉我

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