当我点击一个按钮移动到pygame, python中的另一个屏幕时,我需要帮助清除菜单



我正在尝试制作一款游戏。我想以一种方式编写游戏代码,它有一个主菜单,点击开始按钮后,它将打开游戏。每当我点击开始按钮时,菜单仍然保持不变,游戏在它后面加载。请帮助!我不知道该怎么办!下面是我的代码:

def main_menu():
while True:
draw_text('START', font, white, (screen_width // 2 - 250), (screen_height // 2 - 0))
draw_text('EXIT', font, white, (screen_width // 2 + 50), (screen_height // 2 - 0))
draw_text('main menu', font, (255, 255, 255), 20, 20)

start_btn = pygame.Rect((screen_width // 2 - 250), (screen_height // 2 + 70), 200, 50)
exit_btn = pygame.Rect((screen_width // 2 + 50), (screen_height // 2 + 70), 200, 50)
mx, my = pygame.mouse.get_pos()
if start_btn.collidepoint((mx, my)):
if click:
world.draw()
if exit_btn.collidepoint((mx, my)):
if click:
exit()

click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True

pygame.draw.rect(screen, (255, 0, 0), start_btn)
pygame.draw.rect(screen, (255, 0, 0), exit_btn)

pygame.display.update()
clock.tick(60)
class World():
def __init__(self, data):
self.tile_list = []
#load images
dirt_img = pygame.image.load('pics/dirt.png')
grass_img = pygame.image.load('pics/grass.png')
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 2:
img = pygame.transform.scale(grass_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
col_count += 1
row_count += 1
def draw(self):
screen.blit(bg_img, (0, 0))
for tile in self.tile_list:
screen.blit(tile[0], tile[1])
world_data = [
[1, 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1, 1, 1],
[1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
world = World(world_data)

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

pygame.display.update()

如果您需要完整的程序,请在下面找到:

import pygame, sys
from pygame.locals import *
pygame.init()
screen_width = 600
screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Computer Science IA')
clock = pygame.time.Clock()
fps = 60
#define game variables
tile_size = 30
click = False
run = True

#define font
font = pygame.font.SysFont('Bauhaus 93', 70)
font2 = pygame.font.SysFont('Calibri 9', 20)
#define colors
white = (255, 255, 255)
blue = (0, 0, 255)
#load images
bg_img = pygame.image.load('pics/sky.png')
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit( img, (x,y))

def main_menu():
while True:
draw_text('START', font, white, (screen_width // 2 - 250), (screen_height // 2 - 0))
draw_text('EXIT', font, white, (screen_width // 2 + 50), (screen_height // 2 - 0))
draw_text('main menu', font, (255, 255, 255), 20, 20)

start_btn = pygame.Rect((screen_width // 2 - 250), (screen_height // 2 + 70), 200, 50)
exit_btn = pygame.Rect((screen_width // 2 + 50), (screen_height // 2 + 70), 200, 50)
mx, my = pygame.mouse.get_pos()
if start_btn.collidepoint((mx, my)):
if click:
world.draw()
if exit_btn.collidepoint((mx, my)):
if click:
exit()

click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True

pygame.draw.rect(screen, (255, 0, 0), start_btn)
pygame.draw.rect(screen, (255, 0, 0), exit_btn)

pygame.display.update()
clock.tick(60)
class World():
def __init__(self, data):
self.tile_list = []
#load images
dirt_img = pygame.image.load('pics/dirt.png')
grass_img = pygame.image.load('pics/grass.png')
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 2:
img = pygame.transform.scale(grass_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
col_count += 1
row_count += 1
def draw(self):
screen.blit(bg_img, (0, 0))
for tile in self.tile_list:
screen.blit(tile[0], tile[1])
world_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
[1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 7, 0, 5, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 1],
[1, 7, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 7, 0, 0, 0, 0, 1],
[1, 0, 2, 0, 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 2, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
world = World(world_data)

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

pygame.display.update()
pygame.quit()

它看起来是这样的:主菜单失败

创建一个变量来跟踪要显示的内容。

from pygame.locals import *
pygame.init()
screen_width = 600
screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Computer Science IA")
clock = pygame.time.Clock()
fps = 60
# define game variables
tile_size = 30
click = False
run = True

# define font
font = pygame.font.SysFont("Bauhaus 93", 70)
font2 = pygame.font.SysFont("Calibri 9", 20)
# define colors
white = (255, 255, 255)
blue = (0, 0, 255)
# load images
bg_img = pygame.image.load("pics/sky.png")

def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))

def draw(curr_display, start_btn, exit_btn, world):
if curr_display == "main_menu":
draw_text(
"START", font, white, (screen_width // 2 - 250), (screen_height // 2 - 0)
)
draw_text(
"EXIT", font, white, (screen_width // 2 + 50), (screen_height // 2 - 0)
)
draw_text("main menu", font, (255, 255, 255), 20, 20)
pygame.draw.rect(screen, (255, 0, 0), start_btn)
pygame.draw.rect(screen, (255, 0, 0), exit_btn)
elif curr_display == "world":
world.draw()

def main_menu(world):
curr_display = "main_menu"
start_btn = pygame.Rect(
(screen_width // 2 - 250), (screen_height // 2 + 70), 200, 50
)
exit_btn = pygame.Rect((screen_width // 2 + 50), (screen_height // 2 + 70), 200, 50)
while True:
mx, my = pygame.mouse.get_pos()
if curr_display == "main_menu":
if start_btn.collidepoint((mx, my)):
if click:
curr_display = "world"
if exit_btn.collidepoint((mx, my)):
if click:
exit()
draw(curr_display, start_btn, exit_btn, world)
click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
clock.tick(60)

class World:
def __init__(self, data):
self.tile_list = []
# load images
dirt_img = pygame.image.load("pics/dirt.png")
grass_img = pygame.image.load("pics/grass.png")
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 2:
img = pygame.transform.scale(grass_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
col_count += 1
row_count += 1
def draw(self):
screen.blit(bg_img, (0, 0))
for tile in self.tile_list:
screen.blit(tile[0], tile[1])

world_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 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