gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:46:05 25 4
gpt4 key购买 nike

通常我会创建一个函数或一个 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()

编辑:一个基本示例可用here .

关于python - 如何使用 Ursina Python 制作菜单屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65462110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com