I am trying to make a game like grindshot in aimlab using ursina engine. I need to move the targets each time you click on them. They should move somewhere where already isn´t any target. This part works but they ocassionally don´t change the position because they are randomly moved to the same position as they were before I think. (It sometimes takes 2 clicks to move the target)
I tried to create a new variable (previousposition) in which I stored the previous position but it didn´t work and the program froze. I think its because the variable changes with the new position and then it goes to an endless loop. Please help.
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()