1

我正在用 Ursina 开发一个小游戏,由于某种原因,它没有显示所有元素并且忽略了更新。游戏是关于点击随机生成的点。这是我的代码:

  • menu.py(主文件)
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()
  • 点.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
  • 游戏.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}'

输出是这样的: 这是主菜单。 这是第一个出现的东西。

这就是游戏。 当您单击“开始”按钮时,它将开始。

在第二张图片中,您可以看到需要单击的点之一,但是,它应该在随机位置出现另一个点,但事实并非如此。此外,分数和未点击的文本应该会出现,但它们不会。

感谢所有帮助。

4

1 回答 1

1

ursina 使用更新函数对每个实体调用更新。因此,您应该将功能分配给实体。

于 2021-07-02T08:02:00.857 回答