Reputation: 73
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)
Upvotes: 0
Views: 465
Reputation: 195
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 :
destroy(entity_name)
on each of them and then initializing the class containing the next level.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()
Upvotes: 1