Ursina Python Level Switch

28 Views Asked by At

When trying to go back to the main menu, it says 'menu' not defined whereas it has been used previously in other functions. This only happens when I press the 'escape' key to exit the level and go back to the main menu.

from panda3d.core import loadPrcFile
loadPrcFile("/z/# Computer Science Project/Prototypes/Prototype 1/Config.prc")
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import basic_lighting_shader as bls
from levels.levels import *
from objects.objects import *

def showmainmenu():
    levelmenu.enabled=False
    #destroy(levelmenu)
    Wait(.1)
    menu.enabled=True

def quit():
    application.quit()

def playgame():
    menu.enabled=False
    #destroy(menu)
    Wait(.1)
    FirstLevel()

def loadlevelone():
    levelmenu.enabled=False
    #destroy(levelmenu)
    Wait(.1)
    FirstLevel()

def selectlevelmenu():
    menu.enabled=False
    #destroy(menu)
    Wait(.1)
    levelmenu.enabled=True

#def quitlevel():
    #menu.enabled=True


if __name__ == "__main__":


    app = Ursina(title="Sky Fighter")

    window.exit_button.enabled = False
    window.fps_counter.enabled = False
    window.fullscreen = False

    menu = Menu(enabled=True)
    menu.playbutton.on_click=playgame
    menu.levelsbutton.on_click=selectlevelmenu
    menu.quitbutton.on_click=quit

    levelmenu = LevelMenu()
    levelmenu.enabled=False
    levelmenu.backbutton.on_click=showmainmenu
    levelmenu.levelone.on_click=loadlevelone

    app.run()

THE ABOVE SHOWS main.py AND BELOW SHOWS levels.py

from ursina import *
from ursina.prefabs.radial_menu import RadialMenu
from ursina.prefabs.radial_menu import RadialMenuButton
from ursina.prefabs.first_person_controller import FirstPersonController
from objects.objects import *
from main import quitlevel
from main import *

class FirstLevel(Entity):
    def __init__(self, **kwargs):
        super().__init__()
        self.timer = Timer()
        self.finished = False
        self.controller = FirstPersonController(speed = 10, jump_height = 5, jump_up_duration = 1, fall_after = 0.35, gravity = 1.5, x = -5, z = -5)
        self.sprint = False
        self.timed = False

        #self.pause_handler = Entity(ignore_paused=True)
        #self.pause_text = Text("PAUSED", origin=(0,0), scale=2, enabled=False)

        self.sky = Sky(parent=self)
        self.ground = Entity(model = "Plane",
            color = color.green,
            scale = (100,1,100),
            position = (0,0,0),
            texture = "Grass",
            collider = "box",
            parent=self)
        self.start = StartPlatform((0,0,0),(5,1,5))
        self.end = EndPlatform((21,6,21),(5,1,5))

        self.platform1 = Platform((7,2,7),(5,1,5))
        self.platform2 = Platform((14,4,14),(5,1,5))

    def timing(self):
        hit_info = raycast(origin = self.controller.world_position, direction=(0, -1, 0),ignore= (self.controller,Platform,self.ground), distance = .5, debug = False)

        #Detect whether the ray hits the start or end platforms
        hit = str(hit_info.entity)
        if hit == "start_platform" and self.timed != True and self.finished != True: #Select if the player is on the start platform while not timed or finished
            self.timed = True
        if hit == "end_platform" and self.timed == True: #Select if the player has reached the end and they are timed
            self.timed = False
            self.finished = True

        if self.timed:
            self.timer.t += time.dt #Add the time delta since the last frame
            self.timer.text = str(round(self.timer.t, 2)) #Set the timer text to the time
    
    def update(self):
        self.timing()
    
    #def pause_handler_input(self, key):
        #if key == "escape":
            #application.paused = not application.paused
            #self.pause_text.enabled = application.paused

    def input(self, key):
        #self.pause_handler.input = self.pause_handler_input
        if held_keys["left control"]:
            self.sprint = True
            self.controller.speed = 20
        if self.sprint and not held_keys["left control"]:
            self.controller.speed = 10
        if key == "enter":
            self.finished = False
            self.timed = False
            self.timer.text = "0"
            self.timer.t = 0
        if key == "backspace":
            quit()
        if key == "escape":
            
            self.enabled = False

This error is then displayed:

Traceback (most recent call last):
  File "C:\Program Files\Python38-32\lib\site-packages\direct\showbase\EventManager.py", line 49, in eventLoopTask
    self.doEvents()
  File "C:\Program Files\Python38-32\lib\site-packages\direct\showbase\EventManager.py", line 43, in doEvents
    processFunc(dequeueFunc())
  File "C:\Program Files\Python38-32\lib\site-packages\direct\showbase\EventManager.py", line 99, in processEvent
    messenger.send(eventName, paramList)
  File "C:\Program Files\Python38-32\lib\site-packages\direct\showbase\Messenger.py", line 337, in send
    self.__dispatch(acceptorDict, event, sentArgs, foundWatch)
  File "C:\Program Files\Python38-32\lib\site-packages\direct\showbase\Messenger.py", line 422, in __dispatch
    result = method (*(extraArgs + sentArgs))
  File "C:\Program Files\Python38-32\lib\site-packages\ursina\main.py", line 189, in input
    entity.input(key)
  File "z:\# Computer Science Project\Prototypes\Prototype_3\levels\levels.py", line 73, in input
    quitlevel()
  File "z:\# Computer Science Project\Prototypes\Prototype_3\main.py", line 38, in quitlevel
    menu.enabled=True
NameError: name 'menu' is not defined
:task(error): Exception occurred in PythonTask eventManager
0

There are 0 best solutions below