Skip to content
Advertisement

How do I make a menu screen using Ursina 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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement