zwierzeta nie nachodzą na siebie

This commit is contained in:
LuminoX 2024-03-24 17:46:13 +01:00
parent 654ab9f92f
commit 4fa93d4bb0
2 changed files with 7 additions and 5 deletions

View File

@ -31,6 +31,7 @@ gate = pygame.image.load('images/gate.png')
fences = set()
animals_position = set()
@ -82,7 +83,7 @@ def draw_Animals():
def spawn_all_animals():
for Animal in Animals:
spawner1 = Spawner(Animal, Enclosures)
spawner1.spawn_animal(fences)
spawner1.spawn_animal(fences, animals_position)

View File

@ -6,7 +6,7 @@ class Spawner:
self.animal = animal
self.enclosures = enclosures
def spawn_animal(self, blocked):
def spawn_animal(self, blocked, taken):
possibilities = self.enclosures
fitting = []
for option in possibilities:
@ -26,14 +26,15 @@ class Spawner:
self.animal.y = random.randint(enclosure.y1, enclosure.y2)
if enclosure.y1 > enclosure.y2:
self.animal.y = random.randint(enclosure.y2, enclosure.y1)
if self.check(blocked):
if self.check(blocked, taken):
break
def check(self, blocked):
def check(self, blocked, taken):
x = self.animal.x
y = self.animal.y
if (x,y) in blocked:
if (x,y) in blocked or (x,y) in taken:
return False
taken.add((x,y))
return True