首页 > 解决方案 > 如何使用 Ursina Python 制作菜单屏幕?

问题描述

通常我会做一个函数或一个 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循环似乎也不起作用

标签: python

解决方案


制作功能性游戏菜单其实没那么简单。

您可以创建一个从关卡加载所有游戏模型的函数,一个显示菜单的函数,以及一个显示加载屏幕的最终函数。

加载关卡:

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()

编辑:这里有一个基本示例。


推荐阅读