如何使用Ursina Python制作菜单屏幕?

3
通常我会创建一个函数或者if语句,像这样:

def home_screen():
     # code
     if condition:
         game()

def game():
    # code

home_screen()

或类似于:

game = 1

if game == 1:
     # code for home screen
     if condition:
          game = 2

if game == 2:
    # code for game

if game == 3:
    # so on 

后者需要一个具有类的全局变量,这对我来说没问题。但是在Ursina中,这些方法都不起作用,要么更新函数停在前面,要么color.red、color.blue等突然停止工作,或者第二个if语句根本没有运行。有没有其他替代方法?我考虑完全制作一个home_screen.py文件,但那并没有什么好处,而且我也不确定如何实现它。
编辑:while循环也似乎不起作用。
1个回答

3

制作一个功能齐全的游戏菜单实际上并不那么简单。

你可以创建一个函数,从关卡中加载所有游戏模型,创建一个显示菜单的函数,并创建最后展示加载屏幕的函数。

加载关卡:

def loadLevel():
  global loading_screen
  ground = Entity(model='quad', scale=10, y=-2, collider='box') # dummy entities
  player = FirstPersonController()
  player_model = Entity(model='player', parent=player)
  building = Entity(model='building', collider='box')
  destroy(loading_screen) # delete the loading screen when finished

显示菜单:

def showMenu():
  play = Button('Play', on_click=showLoadingScreen) # A play button that show the loading menu when clicked

显示加载屏幕:

from direct.stdpy import thread # we need threading to load entities in the background (this is specific to ursina, standard threading wouldn't work)

def showLoadingScreen():
  global screen
  screen = Entity(model='quad', texture='loading_image')
  thread.start_new_thread(function=loadLevel, args='') # load entities in the background

文件的其余部分:

from ursina import *

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

  screen = None # for global statement
  showMenu()
  
  app.run()

编辑:这里提供一个基本的例子,点击这里查看。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接