0

我正在尝试使用 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()
4

1 回答 1

1

不要生成一个位置并检查它是否相同,而是首先避免该位置:

生成可能位置的列表。您可以更改不同值的范围。这将创建一个这样的列表:[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]

import itertools
possible_positions = [[(x,y) for y in range(4)] for x in range(3)]
possible_positions = list(itertools.chain(*possible_positions)) # flatten list

使用列表推导从可能的位置中删除当前位置并从新列表中选择一个随机元素:

entity.position = random.choice([coordinate for coordinate in possible_positions if coordinate != (int(entity.x), int(entity.y))])
于 2022-02-09T12:11:32.010 回答