我正在尝试制作一个平台游戏,其关卡在上一个版本完成后出现

我正在使用pygame并尝试制作跑酷/平台游戏,我希望玩家能够通过达到一定的坐标而进入关卡。在我的代码中,我可以使用三个级别,但是如果我死了,它将自动将我移到下一个级别。一旦真正完成游戏,我该如何让玩家进入下一个级别而不是死亡?

class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((width, height))
        pg.display.set_caption("Final")
        self.clock = pg.time.Clock()
        self.running = True

    def new_game(self):
        self.all_characters = pg.sprite.Group()
        self.obstacles = pg.sprite.Group()
        self.player = Player()
        self.grounds = pg.sprite.Group()
        self.all_characters.add(self.player)
        gr = Ground(0, height - 200, width, 40)
        self.all_characters.add(gr)
        self.obstacles.add(gr)
        ob1 = Obstacle(200, 370, 40, 30)
        self.all_characters.add(ob1)
        self.obstacles.add(ob1)
        ob2 = Obstacle(350, 370, 40, 30)
        self.all_characters.add(ob2)
        self.obstacles.add(ob2)
        ob3 = Obstacle(500, 399, 170, 20)
        self.all_characters.add(ob3)
        self.obstacles.add(ob3)
        ob4 = Obstacle(800, 370, 40, 30)
        self.all_characters.add(ob4)
        self.obstacles.add(ob4)
        ob5 = Obstacle(950, 370, 40, 30)
        self.all_characters.add(ob5)
        self.obstacles.add(ob5)
        ob6 = Obstacle(1100, 399, 170, 20)
        self.all_characters.add(ob6)
        self.obstacles.add(ob6)
        self.run()
        if self.player.rect.x >= 1300:

            '''
            self.playing = True
            self.screen.fill(red)
            '''

    def new_lvl(self):
        self.all_characters = pg.sprite.Group()
        self.obstacles = pg.sprite.Group()
        self.player = Player()
        self.grounds = pg.sprite.Group()
        self.all_characters.add(self.player)
        gr = Ground(0, height - 200, width, 40)
        self.all_characters.add(gr)
        self.obstacles.add(gr)
        ob1 = Obstacle(300, 370, 40, 30)
        self.all_characters.add(ob1)
        self.obstacles.add(ob1)
        ob2 = Obstacle(350, 370, 40, 30)
        self.all_characters.add(ob2)
        self.obstacles.add(ob2)
        ob3 = Obstacle(500, 399, 170, 20)
        self.all_characters.add(ob3)
        self.obstacles.add(ob3)
        ob4 = Obstacle(800, 370, 40, 30)
        self.all_characters.add(ob4)
        self.obstacles.add(ob4)
        ob5 = Obstacle(950, 370, 40, 30)
        self.all_characters.add(ob5)
        self.obstacles.add(ob5)
        ob6 = Obstacle(1100, 399, 170, 20)
        self.all_characters.add(ob6)
        self.obstacles.add(ob6)
        self.run()

    def lvl_3(self):
        self.all_characters = pg.sprite.Group()
        self.obstacles = pg.sprite.Group()
        self.player = Player()
        self.grounds = pg.sprite.Group()
        self.all_characters.add(self.player)
        gr = Ground(0, height - 200, width, 40)
        self.all_characters.add(gr)
        self.obstacles.add(gr)
        ob1 = Obstacle(300, 370, 40, 30)
        self.all_characters.add(ob1)
        self.obstacles.add(ob1)
        ob2 = Obstacle(350, 370, 40, 30)
        self.all_characters.add(ob2)
        self.obstacles.add(ob2)
        ob3 = Obstacle(500, 399, 170, 20)
        self.all_characters.add(ob3)
        self.obstacles.add(ob3)
        ob4 = Obstacle(900, 370, 40, 30)
        self.all_characters.add(ob4)
        self.obstacles.add(ob4)
        ob5 = Obstacle(950, 370, 40, 30)
        self.all_characters.add(ob5)
        self.obstacles.add(ob5)
        ob6 = Obstacle(1100, 399, 170, 20)
        self.all_characters.add(ob6)
        self.obstacles.add(ob6)
        self.run()

    def run(self):
        self.playing = True
        while self.playing:
            self.clock.tick(fps)
            self.events()
            self.update_game()
            self.draw()

    def update_game(self):
        self.all_characters.update() 
        collide = pg.sprite.spritecollide(self.player, self.obstacles, False)
        if collide:
            self.player.center = collide[0].rect.top
            print("You died")
            self.playing = False
        '''
        if self.player.rect.x >= 1300:
            self.playing = False
            self.screen.fill(red)
        '''

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False


    def draw(self):
        self.screen.fill(black)
        self.all_characters.draw(self.screen)
        pg.display.flip()

g = Game()
while g.running:
    g.new_game()
    g.new_lvl()
    g.lvl_3()






class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((width, height))
        pg.display.set_caption("Final")
        self.clock = pg.time.Clock()
        self.running = True

    def new_game(self):
        self.all_characters = pg.sprite.Group()
        self.obstacles = pg.sprite.Group()
        self.player = Player()
        self.grounds = pg.sprite.Group()
        self.all_characters.add(self.player)
        gr = Ground(0, height - 200, width, 40)
        self.all_characters.add(gr)
        self.obstacles.add(gr)
        ob1 = Obstacle(200, 370, 40, 30)
        self.all_characters.add(ob1)
        self.obstacles.add(ob1)
        ob2 = Obstacle(350, 370, 40, 30)
        self.all_characters.add(ob2)
        self.obstacles.add(ob2)
        ob3 = Obstacle(500, 399, 170, 20)
        self.all_characters.add(ob3)
        self.obstacles.add(ob3)
        ob4 = Obstacle(800, 370, 40, 30)
        self.all_characters.add(ob4)
        self.obstacles.add(ob4)
        ob5 = Obstacle(950, 370, 40, 30)
        self.all_characters.add(ob5)
        self.obstacles.add(ob5)
        ob6 = Obstacle(1100, 399, 170, 20)
        self.all_characters.add(ob6)
        self.obstacles.add(ob6)
        self.run()
        if self.player.rect.x >= 1300:

            '''
            self.playing = True
            self.screen.fill(red)
            '''

    def new_lvl(self):
        self.all_characters = pg.sprite.Group()
        self.obstacles = pg.sprite.Group()
        self.player = Player()
        self.grounds = pg.sprite.Group()
        self.all_characters.add(self.player)
        gr = Ground(0, height - 200, width, 40)
        self.all_characters.add(gr)
        self.obstacles.add(gr)
        ob1 = Obstacle(300, 370, 40, 30)
        self.all_characters.add(ob1)
        self.obstacles.add(ob1)
        ob2 = Obstacle(350, 370, 40, 30)
        self.all_characters.add(ob2)
        self.obstacles.add(ob2)
        ob3 = Obstacle(500, 399, 170, 20)
        self.all_characters.add(ob3)
        self.obstacles.add(ob3)
        ob4 = Obstacle(800, 370, 40, 30)
        self.all_characters.add(ob4)
        self.obstacles.add(ob4)
        ob5 = Obstacle(950, 370, 40, 30)
        self.all_characters.add(ob5)
        self.obstacles.add(ob5)
        ob6 = Obstacle(1100, 399, 170, 20)
        self.all_characters.add(ob6)
        self.obstacles.add(ob6)
        self.run()

    def lvl_3(self):
        self.all_characters = pg.sprite.Group()
        self.obstacles = pg.sprite.Group()
        self.player = Player()
        self.grounds = pg.sprite.Group()
        self.all_characters.add(self.player)
        gr = Ground(0, height - 200, width, 40)
        self.all_characters.add(gr)
        self.obstacles.add(gr)
        ob1 = Obstacle(300, 370, 40, 30)
        self.all_characters.add(ob1)
        self.obstacles.add(ob1)
        ob2 = Obstacle(350, 370, 40, 30)
        self.all_characters.add(ob2)
        self.obstacles.add(ob2)
        ob3 = Obstacle(500, 399, 170, 20)
        self.all_characters.add(ob3)
        self.obstacles.add(ob3)
        ob4 = Obstacle(900, 370, 40, 30)
        self.all_characters.add(ob4)
        self.obstacles.add(ob4)
        ob5 = Obstacle(950, 370, 40, 30)
        self.all_characters.add(ob5)
        self.obstacles.add(ob5)
        ob6 = Obstacle(1100, 399, 170, 20)
        self.all_characters.add(ob6)
        self.obstacles.add(ob6)
        self.run()

    def run(self):
        self.playing = True
        while self.playing:
            self.clock.tick(fps)
            self.events()
            self.update_game()
            self.draw()

    def update_game(self):
        self.all_characters.update() 
        collide = pg.sprite.spritecollide(self.player, self.obstacles, False)
        if collide:
            self.player.center = collide[0].rect.top
            print("You died")
            self.playing = False
        '''
        if self.player.rect.x >= 1300:
            self.playing = False
            self.screen.fill(red)
        '''

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False


    def draw(self):
        self.screen.fill(black)
        self.all_characters.draw(self.screen)
        pg.display.flip()

g = Game()
while g.running:
    g.new_game()
    g.new_lvl()
    g.lvl_3()