Python pygame中的菜单切换

Python pygame中的菜单切换,python,pygame,Python,Pygame,我改变了代码的结构,但当我崩溃时仍然遇到问题,我想重新启动游戏。我的想法是建立3个“屏幕”:主菜单,游戏,重启。当我运行程序并单击“开始”按钮或“退出”按钮时,进入主菜单没有问题。但是,当我进入游戏时,我崩溃了,我点击了“重启”按钮,但是出现了一个错误 代码如下: import pygame import sys pygame.init() screen = pygame.display.set_mode((1200, 600)) clock = pygame.time.Clock() BL

我改变了代码的结构,但当我崩溃时仍然遇到问题,我想重新启动游戏。我的想法是建立3个“屏幕”:主菜单,游戏,重启。当我运行程序并单击“开始”按钮或“退出”按钮时,进入主菜单没有问题。但是,当我进入游戏时,我崩溃了,我点击了“重启”按钮,但是出现了一个错误

代码如下:

import pygame
import sys


pygame.init()
screen = pygame.display.set_mode((1200, 600))
clock = pygame.time.Clock()
BLUE = pygame.Color('dodgerblue3')
ORANGE = pygame.Color('sienna3')
BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)
GREEN = (13, 255, 0)
YELLOW = (0, 255, 20)
BRIGHT_YELLOW = (255, 255, 20)
vel = 4
vel_left = 5
vel_right = -5

player = pygame.Rect(40, 45, 30, 30)

rotatingrects = [
    pygame.Rect(630, 300, 250, 20),
    pygame.Rect(920, 300, 250, 20)
    ]

walls = [
    pygame.Rect(0, 0, 1200, 20), pygame.Rect(0, 0, 20, 600),
    pygame.Rect(0, 580, 1200, 20), pygame.Rect(1180, 0, 20, 600),
    pygame.Rect(300, 0, 20, 530), pygame.Rect(20, 100, 230, 20),
    pygame.Rect(70, 200, 230, 20), pygame.Rect(20, 300, 230, 20),
    pygame.Rect(70, 400, 230, 20), pygame.Rect(600, 100, 20, 500)
    ]

class MovingRect(pygame.Rect):

    def __init__(self, x, y, w, h, vel):
        # Calling the __init__ method of the parent class
        super().__init__(x, y, w, h)
        self.vel = vel

    def update(self):
        self.x += self.vel  # Moving
        if self.right > 600 or self.left < 320:  # If it's not in this area
            self.vel = -self.vel  # Inverting the direction

movingrects = [
    MovingRect(320, 120, 30, 30, vel_left),
    MovingRect(320, 240, 30, 30, vel_left),
    MovingRect(320, 360, 30, 30, vel_left),
    MovingRect(570, 180, 30, 30, vel_right),
    MovingRect(570, 300, 30, 30, vel_right),
    MovingRect(570, 420, 30, 30, vel_right)
    ]

def quit_game():
    pygame.quit()
    sys.exit()

def text_objects(text, font):
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()

def button(msg, x, y, w, h, ic, ac, action = None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()


    if x + w > mouse[0] > x and y + h  > mouse[1] > y:
        pygame.draw.rect(screen, ac, (x, y, w, h))
        if click[0] == 1 and action is not None:
            action()
    else:
        pygame.draw.rect(screen, ic, (x, y, w, h))

    smallText = pygame.font.Font("freesansbold.ttf",50)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x+(w/2)), (y+(h/2)))
    screen.blit(textSurf, textRect)

def restart():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()

        screen.fill(BLUE)

        button("Restart", 525, 250, 150, 60, BRIGHT_YELLOW, YELLOW, menu)

        pygame.display.update()
        pygame.display.flip()
        clock.tick(60)

def front_page():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()

        screen.fill(WHITE)

        button("Start", 525, 250, 150, 60, BRIGHT_YELLOW, YELLOW, menu)
        button("Quit", 525, 350, 150, 60, BRIGHT_YELLOW, YELLOW, quit_game)

        pygame.display.update()
        pygame.display.flip()
        clock.tick(60)


def menu():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()

        keys = pygame.key.get_pressed()

        # Player coordinates
        if keys[pygame.K_LEFT] and player.x > 0:
            player.x -= vel
        if keys[pygame.K_RIGHT] and player.x < 1200 - player.width:
            player.x += vel
        if keys[pygame.K_UP] and player.y > 0:
            player.y -= vel
        if keys[pygame.K_DOWN] and player.y < 600 - player.height:
            player.y += vel

        for wall in walls:
            # Check if the player rectangle collides with a wall rectangle
            if player.colliderect(wall):
                print("Game over")
                restart()

        for movingrect in movingrects:
            movingrect.update()  # Movement and bounds checking
            if player.colliderect(movingrect):
                print("Game over")

        # TO DO #
        for rotatingrect in rotatingrects:
            if player.colliderect(rotatingrect):
                print("Game over")

        # Drawing everything
        screen.fill(WHITE)
        pygame.draw.rect(screen, RED, player)
        # Drawing walls and moving objects
        for rotatingrect in rotatingrects:
            pygame.draw.rect(screen, BLACK, rotatingrect)

        for wall in walls:
            pygame.draw.rect(screen, BLACK, wall)

        for movingrect in movingrects:
            pygame.draw.rect(screen, GREEN, movingrect)

        pygame.draw.rect(screen, RED, player)
        pygame.display.update()

        pygame.display.flip()
        clock.tick(60)


def main():
    scene = front_page
    while scene is not None:
        scene = scene()


main()
pygame.quit()
导入pygame
导入系统
pygame.init()
screen=pygame.display.set_模式((1200600))
clock=pygame.time.clock()
蓝色=pygame.Color('dodgerblue3')
橙色=pygame.Color('sienna3'))
黑色=(0,0,0)
白色=(255255)
红色=(255,0,0)
绿色=(13255,0)
黄色=(0,255,20)
亮黄色=(255、255、20)
水平=4
左电平=5
右电平=-5
player=pygame.Rect(40,45,30,30)
旋转矩形=[
pygame.Rect(630300250,20),
pygame.Rect(92030025020)
]
墙=[
pygame.Rect(0,0,1200,20),pygame.Rect(0,0,20600),
pygame.Rect(0580120020),pygame.Rect(118060600),
pygame.Rect(300,0,20530),pygame.Rect(20100230,20),
pygame.Rect(70200230,20),pygame.Rect(2030230,20),
pygame.Rect(7040023020),pygame.Rect(60010020500)
]
类MovingRect(pygame.Rect):
定义初始值(self,x,y,w,h,vel):
#调用父类的uuu init_uuuu方法
super().\uuuu init\uuuu(x,y,w,h)
self.vel=vel
def更新(自我):
self.x+=self.vel#移动
如果self.right>600或self.left<320:#如果不在该区域
self.vel=-self.vel#反转方向
movingrects=[
MovingRect(320、120、30、30、左水平),
MovingRect(320、240、30、30、左水平),
MovingRect(320、360、30、30、左水平),
MovingRect(570、180、30、30、右水平),
MovingRect(570,300,30,30,右水平),
MovingRect(570420,30,30,右水平)
]
def退出游戏():
pygame.quit()
sys.exit()
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(屏幕,ac,(x,y,w,h))
如果单击[0]==1且操作不是无:
行动()
其他:
pygame.draw.rect(屏幕,ic,(x,y,w,h))
smallText=pygame.font.font(“freesansbold.ttf”,50)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
screen.blit(textSurf,textRect)
def restart():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
退出游戏()
屏幕填充(蓝色)
按钮(“重启”,525250,150,60,亮黄,黄色,菜单)
pygame.display.update()
pygame.display.flip()
时钟滴答(60)
def首页()
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
退出游戏()
屏幕填充(白色)
按钮(“开始”,525250,150,60,亮黄,黄色,菜单)
按钮(“退出”,525350,150,60,亮黄色,黄色,退出游戏)
pygame.display.update()
pygame.display.flip()
时钟滴答(60)
def菜单():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
退出游戏()
keys=pygame.key.get_pressed()
#玩家坐标
如果键[pygame.K_LEFT]和player.x>0:
player.x-=vel
如果键[pygame.K_RIGHT]和player.x<1200-player.width:
player.x+=vel
如果键[pygame.K_UP]和player.y>0:
player.y-=vel
如果键[pygame.K_DOWN]和player.y<600-player.height:
player.y+=vel
对于墙中墙:
#检查播放器矩形是否与墙矩形冲突
如果player.collide rect(墙):
打印(“游戏结束”)
重新启动()
对于movingrect中的movingrect:
movingrect.update()#移动和边界检查
如果player.collide rect(movingrect):
打印(“游戏结束”)
#做#
对于rotatingrect中的rotatingrect:
如果player.collide rect(旋转rect):
打印(“游戏结束”)
#画一切
屏幕填充(白色)
pygame.draw.rect(屏幕,红色,玩家)
#绘制墙和移动对象
对于rotatingrect中的rotatingrect:
pygame.draw.rect(屏幕、黑色、旋转矩形)
对于墙中墙:
pygame.draw.rect(屏幕、黑色、墙)
对于movingrect中的movingrect:
pygame.draw.rect(屏幕、绿色、移动rect)
pygame.draw.rect(屏幕,红色,玩家)
pygame.display.update()
pygame.display.flip()
时钟滴答(60)
def main():
场景=首页
虽然场景不是“无”:
场景=场景()
main()
pygame.quit()

之所以会发生这种情况,是因为您的
玩家
rect是一个全局变量,当您重新启动游戏时,它仍然处于碰撞位置

将游戏相关变量移动到
菜单
功能(游戏功能),以便在再次调用
菜单
时将其重置为原始值

def menu():
    vel = 4
    vel_left = 5
    vel_right = -5

    player = pygame.Rect(40, 45, 30, 30)

    rotatingrects = [
        pygame.Rect(630, 300, 250, 20),
        pygame.Rect(920, 300, 250, 20)
    ]

    walls = [
        pygame.Rect(0, 0, 1200, 20), pygame.Rect(0, 0, 20, 600),
        pygame.Rect(0, 580, 1200, 20), pygame.Rect(1180, 0, 20, 600),
        pygame.Rect(300, 0, 20, 530), pygame.Rect(20, 100, 230, 20),
        pygame.Rect(70, 200, 230, 20), pygame.Rect(20, 300, 230, 20),
        pygame.Rect(70, 400, 230, 20), pygame.Rect(600, 100, 20, 500)
    ]
    movingrects = [
        MovingRect(320, 120, 30, 30, vel_left),
        MovingRect(320, 240, 30, 30, vel_left),
        MovingRect(320, 360, 30, 30, vel_left),
        MovingRect(570, 180, 30, 30, vel_right),
        MovingRect(570, 300, 30, 30, vel_right),
        MovingRect(570, 420, 30, 30, vel_right)
    ]

谢谢你,这很有效,你让我不再放弃一切,哈哈。不客气!我还想消除游戏中的递归(这意味着一个函数本身调用,或者两个函数相互重复调用(在1000次递归之后,Python将产生一个错误))。实际上,场景函数应该将下一个场景返回到
main
函数,在
while
循环中调用该函数。您可以将
下一个\u场景
变量添加到场景中