1

I'm developing a little game with Ursina and for some reason it doesn't show all the elements and ignores an update. The game is about clicking the randomly generated points. This is my code:

  • menu.py (Main file)
from ursina import *
from point import *
from game import play, play_2

class Menu(Entity):
    def __init__(self, **kwargs):
        super().__init__(parent = camera.ui, ignore_paused = True)

        self.main_menu = Entity(parent = self, enabled = True)
        # More menus

        Text(text = 'Main Menu', parent = self.main_menu, y=0.4, x=0, origin=(0,0))

        def starting(): # Game starts
            self.main_menu.disable()
            play()
            def update(): # The program ignores this update, so the following line isn't executed
                play_2()

        ButtonList(button_dict = {
            'Start':Func(starting),
            'Settings':Func(print, 'Settings was clicked')
            }, y = 0, parent = self.main_menu)


app = Ursina(fullscreen = True)
menu = Menu()
app.run()
  • point.py
from ursina import *

class Point(Entity):
    def __init__(self, position = (0,0,0), parent = camera, **kwargs):
        super().__init__(
            parent = parent,
            model = 'sphere',
            scale = .04,
            position = position,
            collider = 'sphere'
            )

    def input(self, key):
        global generate
        global score
        global missed

        if key == 'left mouse down' and self.hovered:
            score += 1
            self.disable()
            generate = True
        if key == 'left mouse down' and self.hovered == False:
            missed += 1


generate = False
score = 0
missed = 0
  • game.py
from ursina import *
from point import Point, generate, score, missed
from random import uniform

score_text = Text(text = 'h')
missed_text = Text(text = 'h')
points = []

def play(): # Generates all the needed things and an init point
    global score_text
    global missed_text

    camera.position = (0,0,-4)
    camera.rotation_x = 10
    board = Entity(parent = camera, model = 'quad', scale = 2, color = color.rgb(0,0,0), position = (0,0,3))
    score_text.scale = 1
    score_text.position = (.66,.43)
    missed_text.scale = 1
    missed_text.position = (.66,.4)
    point = Point(parent = board)

def play_2(): # The actual game starts
    global generate
    global score
    global missed
    global score_text
    global missed_text
    global points

    if generate == True:
        point = Point(position = (uniform(-.44,.44), uniform(-.265,.265),-.1))
        points.append(point)
        generate = False

    score_text.text = f'Score: {score}'
    missed_text.text = f'Missed: {missed}'

The output is this: This is the main menu. It is the first thing to appear.

This is the game. It will start when you click the Start button.

In the second image you can see one of the points you need to click, however, it is supposed to appear another one in a random location, but it doesn't. Also, the score and missed clicks texts should appear, but they doesn't.

All help is appreciated.

B3CTOR
  • 45
  • 5
  • Please provide a [Minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) instead of posting your whole code. Removing unnecessary parts may even help you find the problem. – Jan Wilamowski Jul 02 '21 at 03:50

1 Answers1

1

ursina calls update on every Entity with an update function. So you should assign the function to an Entity.

pokepetter
  • 1,383
  • 5
  • 8
  • I don't know what do you mean, the update is in the Menu class which inherits from Entity. How should I do it? – B3CTOR Jul 02 '21 at 08:40
  • `def update(self):` at the same indent level as `__init__(self):`. Currently you're defining the functions inside `__init__`, so they don't belong to your class. – pokepetter Jul 02 '21 at 13:33