【问题标题】:Ursina Engine game not working as expectedUrsina 引擎游戏无法按预期运行
【发布时间】:2021-09-13 19:32:46
【问题描述】:

我正在使用 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()
  • 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}'

输出是这样的:

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

感谢所有帮助。

【问题讨论】:

标签: python ursina


【解决方案1】:

ursina 使用更新函数对每个实体调用更新。所以你应该把函数分配给一个实体。

【讨论】:

  • 我不知道你是什么意思,更新是在继承自Entity的Menu类中。我该怎么做?
  • def update(self): 与__init__(self): 的缩进级别相同。目前您正在 __init__ 中定义函数,因此它们不属于您的类。
猜你喜欢
  • 2023-02-14
  • 2021-01-09
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2013-01-25
  • 2022-01-02
  • 2022-07-12
  • 1970-01-01
相关资源
最近更新 更多