0

I'm trying to create a 2D game in ursina and I have a class FirstLevel in which I create the player 2D entity, enemies, cubes etc and I also use this class' update method for player action etc. In my main.py, first I create a menu like interface with Start & Exit buttons etc. and then if the start button is clicked I will run the first level.

My question is: can I create second class, say, SecondLevel (inheriting from the first some info like the player speed, enemies etc.) and destroy the FirstLevel class (after the first level boss is destroyed)? If not, does someone know how I can switch between different class Entities (which are levels in my case)?

The source code is here: https://github.com/VulpeanuAdrian/LordMuffinGame

def start_level():
    global m
    destroy(play)
    destroy(help)
    destroy(help_text)
    destroy(exit_button)
    destroy(cat_screen)
    m = True
    m = FirstLevel()

def help_text_func():
    play.visible = True
    exit_button.visible = True
    destroy(help_text_bt)

def help_menu():
    global help_text_bt
    help_text_bt = Button(
        text=help_text_string,
        on_click=help_text_func,
        scale=(1, 0.6),
        x=-0.4, 
        y=0.35, 
        color=color.orange, 
        text_color=color.violet
    )

if __name__ == '__main__':
    app = Ursina()
    window.fullscreen = True
    cat_screen = Animation('cat_gif', fps=30, loop=True, autoplay=True, scale=(13, 13))
    cat_screen.start()
    play = Button(' Play ', on_click=start_level, scale=(0.095, 0.095), x=0, y=0, color=color.blue)
    help = Button('Help', on_click=help_menu, scale=(0.095, 0.095), x=0, y=-0.1)
    help_text = Text(text=help_text_string, x=-0.3, y=0.3, visible=False, color=color.random_color())
    exit_button = Button(' Quit ', on_click=application.quit, x=0, y=-0.2, scale=(0.095, 0.095), color=color.red)

Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23

1 Answers1

1

There's two ways of achieving what you want, a first one which is pretty straight-forward and a second one which is more object oriented :

  • One way of deleting specific entities (here from your level) is simply by storing the entities in a list, iterating through them and using destroy(entity_name) on each of them and then initializing the class containing the next level.
  • If you prefer more OOP programming, you should make each level an Entity and parent to itself every Entity you define, making it so that you can just destroy the parent class (the level), and ursina will also destroy its children. If you want to parent an Entity to something else, you can just manually set a custom destroy method for the level that deletes all entities you put in it. See the example below :
from ursina import *


class FirstLevel(Entity):
   def __init__(self):
       super().__init__()
       self.cube = Entity(parent=self, model='cube', texture='brick')
       self.other_cube = Entity(parent=camera.ui, model='cube', color=color.pink, scale=.1)

   def input(self, key):
       if key == 'a':
           self.unloadLevel()

   def unloadLevel(self):
       destroy(self.other_cube)
       destroy(self)



if __name__ == '__main__':
   app = Ursina()

   a = FirstLevel()

   app.run()
  • In my code i did go exactly for the second variant , the problem is basically after i will use the destroy(self) ( in the FirstLevel class i dont know how to properly load the second level in the if__name__ == main , thanks a lot for your anwear: ) – Adrian Vulpeanu Feb 26 '22 at 14:01
  • Can you be more precise about what you can't figure out ? – DiamondsBattle Mar 01 '22 at 17:35
  • Sure, thanks for the reply , basically i did try your second suggestion but after i will destroy(self) -> destroy the first level class i dont know how to start the second level class.. – Adrian Vulpeanu Mar 06 '22 at 17:05