python - 如何使用 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循环似乎也不起作用
解决方案
制作功能性游戏菜单其实没那么简单。
您可以创建一个从关卡加载所有游戏模型的函数,一个显示菜单的函数,以及一个显示加载屏幕的最终函数。
加载关卡:
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()
编辑:这里有一个基本示例。
推荐阅读
- javascript - ActionView::Template::Error(没有路由匹配 {:action=>"show", :controller=>"administration/roles" ...可能的不匹配约束:[:id]):
- c# - 怎么可能给用户一定的更新权,却不能正常使用呢?
- java - @RequestBody 在尝试使用 weblogic 11g 中的 fasterxml 将 json 转换为 POJO 时获取空值
- java - Unix时间/纪元时间:日期表示还是时间块?
- visual-studio-code - 如何从光标向前查找和替换
- django - 如何记录所有 Django 序列化程序验证错误?
- sql - 如何使用 SQL 将数据从 R 复制到数据库?
- python - 输入元组列表并查找交集 + 等
- bootstrap-switch - bootstrap-switch 3.4.0 版似乎没有更改底层 INPUT 的已检查属性
- r-markdown - 在 bookdown/rmarkdown 脚注中阻止引用