我正在尝试使用 ursina 引擎在 Aimlab 中制作类似 Grindshot 的游戏。每次单击目标时,我都需要移动它们。他们应该移动到已经不是任何目标的地方。这部分有效,但它们偶尔不会改变位置,因为它们会随机移动到与 我认为之前相同的位置。(有时需要点击 2 次才能移动目标)
我试图创建一个新变量(previousposition),在其中存储了以前的位置,但它不起作用并且程序冻结了。我认为这是因为变量随着新位置的变化而变化,然后进入无限循环。请帮忙。
import random
from ursina.prefabs.first_person_controller import FirstPersonController
class Cube(Entity):
def __init__(self, position = (0,0,0)):
super().__init__(
position = position,
model = "cube",
origin_y = 0.5,
texture = "white_cube",
color = color.white,
collider= 'box')
class Target(Button):
def __init__(self, position = (0,0,0), scale = (0.7, 0.7, 0.7)):
super().__init__(
parent = scene,
position = position,
model = "cube",
origin_y = 0.5,
texture = "white_cube",
color = color.red,
highlight_color = rgb(255, 88, 74),
scale = scale)
def input(self, key):
if self.hovered:
if key == "left mouse down":
mouse.hovered_entity.x = random.randint(-2,2)
mouse.hovered_entity.y = random.randint(1, 4)
while target1.position == target2.position or target1.position == target3.position or target2.position == target3.position:
mouse.hovered_entity.x = random.randint(-2, 2)
mouse.hovered_entity.y = random.randint(1, 4)
app = Ursina()
target1 = Target(position = (0,3, 5))
target2 = Target(position = (1,4, 5))
target3 = Target(position = (2,2, 5))
for x in range(9):
for z in range(9):
floor = Cube(position = (x-4,0,z-4))
player = FirstPersonController()
app.run()
__________________________________________________________________________________________________
#Not Working
from ursina import *
import random
from ursina.prefabs.first_person_controller import FirstPersonController
class Cube(Entity):
def __init__(self, position = (0,0,0)):
super().__init__(
position = position,
model = "cube",
origin_y = 0.5,
texture = "white_cube",
color = color.white,
highlight_color = color.red,
collider= 'box')
class Target(Button):
def __init__(self, previousposition, position = (0,0,0), scale = (0.7, 0.7, 0.7)):
super().__init__(
parent = scene,
position = position,
model = "cube",
origin_y = 0.5,
texture = "white_cube",
color = color.red,
highlight_color = color.red,
scale = scale)
self.previousposition = previousposition
def input(self, key):
if self.hovered:
if key == "left mouse down":
mouse.hovered_entity.position = self.previousposition
mouse.hovered_entity.x = random.randint(-2,2)
mouse.hovered_entity.y = random.randint(1, 4)
while target1.position == target2.position or target1.position == target3.position or target2.position == target3.position:
mouse.hovered_entity.x = random.randint(-2, 2)
mouse.hovered_entity.y = random.randint(1, 4)
while self.previousposition == target1.position or target2.position or target3.position:
mouse.hovered_entity.x = random.randint(-2, 2)
mouse.hovered_entity.y = random.randint(1, 4)
app = Ursina()
target1 = Target((0,0,0), position = (0,3, 5))
target2 = Target((0,0,0), position = (1,4, 5))
target3 = Target((0,0,0), position = (2,2, 5))
for x in range(9):
for z in range(9):
floor = Cube(position = (x-4,0,z-4))
player = FirstPersonController()
app.run()