使用Ursina游戏引擎在Python中随机化屏幕上出现的形状对



我希望在游戏运行时,屏幕上出现一对随机的形状,当按下键盘上的左移或右移按钮时,使用Python中的Ursina引擎,将这对形状删除并替换为另一对形状。目前,我的代码可以工作,但当第三对形状出现在屏幕上时,它总是会崩溃。我不知道问题到底是什么,也不知道如何解决。有什么帮助吗?

from ursina import *
import random 
#creates the instance of the game
app = Ursina()
#contains a list of the shapes
shape_a = [] #set a shapes on the right side
shape_b = [] #set b shapes on the left side

#Entities refer to shapes created that appear in pairs
#SET A
#Visibilities turned off for each entity
s1a = Entity(model = 'cube', color = color.orange, scale = (2,2), position = (-2.5,0,0), rotation = Vec3 (45,45,45), texture = 'white_cube')
s1a.visible  = False
s2a = Entity(model = 'cube', color = color.orange, scale = (2,2), position = (-2.5,0,0), rotation = Vec3 (45,-45,45), texture = 'white_cube')
s2a.visible  = False
s3a = Entity(model = 'sphere', color = color.orange, scale = (2,2), position = (-2.5,0,0), rotation = Vec3 (45,45,45), texture = 'white_cube')
s3a.visible  = False
s4a = Entity(model = 'quad', color = color.orange, scale = (2.5,2), position = (-2.5,0,0), rotation = Vec3 (45,-45,45), texture = 'white_cube')
s4a.visible  = False
#Appends each entity to the corresponding list
shape_a.append(s1a)
shape_a.append(s2a)
shape_a.append(s3a)
shape_a.append(s4a)

#Entities refer to shapes created that appear in pairs
#SET B
#Visibilities turned off for each entity
s1b = Entity(model = 'cube', color = color.orange, scale = (2,2), position = (2.5,0,0), rotation = Vec3 (45,45,45), texture = 'white_cube')
s1b.visible  = False
s2b = Entity(model = 'cube', color = color.orange, scale = (2.5,2), position = (2.5,0,0), rotation = Vec3 (45,-45,45), texture = 'white_cube')
s2b.visible  = False
s3b = Entity(model = 'quad', color = color.orange, scale = (2,2), position = (2.5,0,0), rotation = Vec3 (45,45,45), texture = 'white_cube')
s3b.visible  = False
s4b = Entity(model = 'sky_dome', color = color.orange, scale = (0.5,0.5), position = (2.5,0,0), rotation = Vec3 (45,-45,45), texture = 'white_cube')
s4b.visible  = False
#Appends each entity to the corresponding list
shape_b.append(s1b)
shape_b.append(s2b)
shape_b.append(s3b)
shape_b.append(s4b)

#Shows the first pair of shapes on the screen.
a = random.choice(shape_a)
a.visible = True
b = random.choice(shape_b)
b.visible = True
#Called to turn off the visibility of the current shapes
def Falsefunction ():
#a = random.choice(shape_a)
a.visible = False
#b = random.choice(shape_b)
b.visible = False

for i in range(1,20):
def input(key):
#receives input from the keyboard
if key == "left shift" or key == "right shift":
Falsefunction()
#After the current pair of shapes disappear from the screen, the next lines of code causes another
#random pair of shapes to appear as long as the loop keeps running.
a = random.choice(shape_a)
a.visible = True
b = random.choice(shape_b)
b.visible = True 
app.run()

问题在于一些形状没有按预期访问,可能是由于Falsefunction方法正在更改哪些对象的问题(因为它没有明确地获取这两个对象并返回它们,所以很难看到它更改的确切对象(。它似乎在几次之后发生,这表明它与随机选择有关,因此它似乎正确地关闭了列表中的一些对象visible属性,但对某些对象来说,它似乎不起作用。

为了找到确切的问题,你可以尝试打印出整个列表的可见状态,看看问题从哪里开始发生。

作为一个更清楚地了解具体情况的简单解决方案,您可以在每个键输入上关闭两个列表中所有对象的可见性,这样就不会发生错误。

那么,您甚至不需要选择对象ab。您只需从列表中随机选择一个对象,然后直接设置其属性。然后,在每次调用Falsefunction时,只需将所有对象中的所有可见属性设置为False。如果你喜欢用ab编写它的方式,你也可以保持所有这些不变,只更改Falsefunction,结果会是一样的。

显然,如果你有大量的对象,这会变得效率低下,最好能真正找出确切的问题。对于数量较少的对象,它不会对性能产生任何影响。

...
# Shows the first pair of shapes on the screen.
random.choice(shape_a).visible = True
random.choice(shape_b).visible = True

# Called to turn off the visibility of all shapes
def Falsefunction():
for shape in shape_a + shape_b:
shape.visible = False

def input(key):
# receives input from the keyboard
if key == "left shift" or key == "right shift":
Falsefunction()
# After the current pair of shapes disappear from the screen, the next lines of code causes another
# random pair of shapes to appear as long as the loop keeps running.
random.choice(shape_a).visible = True
random.choice(shape_b).visible = True

app.run()

最新更新