0

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()
Jacob G
  • 1
  • 2

1 Answers1

1

Instead of generating a position and checking it it's not the same, avoid that position in the first place:

Generate a list of possible positions. You can change the range for different values. This will make a list like this: [(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

Use list comprehension to remove the current position from possible positions and choose a random element from the new list:

entity.position = random.choice([coordinate for coordinate in possible_positions if coordinate != (int(entity.x), int(entity.y))])
pokepetter
  • 1,383
  • 5
  • 8