简体   繁体   English

Python Ursina 液位开关

[英]Python Ursina level switch

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.我正在尝试在 ursina 中创建一个 2D 游戏,我有一个 class FirstLevel ,我在其中创建玩家 2D 实体、敌人、立方体等,我还使用此类的update方法进行玩家操作等。在我的 main.py 中,首先,我创建了一个菜单,例如带有“开始”和“退出”按钮等的界面,然后如果单击“开始”按钮,我将运行第一级。

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)?我的问题是:我可以创建第二个 class,比如说, SecondLevel (从第一个继承一些信息,比如玩家速度、敌人等)并摧毁FirstLevel class(在第一级老板被摧毁之后)? If not, does someone know how I can switch between different class Entities (which are levels in my case)?如果没有,有人知道我如何在不同的 class 实体(在我的情况下是级别)之间切换吗?

The source code is here: https://github.com/VulpeanuAdrian/LordMuffinGame源代码在这里: 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)

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:有两种方法可以实现您想要的,第一种非常简单,第二种更面向 object:

  • 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.删除特定实体(此处从您的级别)的一种方法是简单地将实体存储在列表中,遍历它们并在每个实体上使用destroy(entity_name)然后初始化包含下一个级别的 class。
  • 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.如果你更喜欢 OOP 编程,你应该让每个级别成为一个Entity ,并且让你定义的每个Entity都成为它自己的父级,这样你就可以销毁父级 class(级别),而 ursina 也会销毁它的子级。 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.如果你想将一个Entity作为其他东西的父级,你可以为删除你放入其中的所有实体的级别手动设置自定义销毁方法。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM