Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a menu screen using Ursina Python?

Tags:

python

Usually I would make a function or an if statement, like this:

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

def game():
    # code

home_screen()

or something like:

game = 1

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

if game == 2:
    # code for game

if game == 3:
    # so on 

The latter needs a global variable with a class, which is fine for me. However in Ursina, none of these work, either the update function stops on the former, or the color.red, color.blue, etc. stops working out of nowhere, or the second if statement just doesn't run. Does anyone have an alternative? I'm thinking of just making a home_screen.py file entirely but that won't do much good, and I'm not sure how that can be implemented anyway.

Edit: while loops also don't seem to work

like image 474
OHTYCH 100 Avatar asked Jun 18 '26 19:06

OHTYCH 100


1 Answers

Making a functional game menu is actually not that simple.

You could make a function that loads all the game models from a level, a function that shows a menu, and a final one that shows a loading screen.

Load a level :

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

Show the menu :

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

Show the loading screen :

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

Rest of the file :

from ursina import *

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

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

Edit : a basic example is available here.

like image 141
cr8f2kvi9b7gqpf1 Avatar answered Jun 20 '26 09:06

cr8f2kvi9b7gqpf1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!