Male_zoo_Projekt_SI/spawner.py

45 lines
1.4 KiB
Python
Raw Normal View History

import random
class Spawner:
def __init__(self, animal, enclosures):
self.animal = animal
self.enclosures = enclosures
2024-03-24 17:46:13 +01:00
def spawn_animal(self, blocked, taken):
possibilities = self.enclosures
fitting = []
for option in possibilities:
if option.type == self.animal.environment:
fitting.append(option)
enclosure = random.choice(fitting)
while True:
if enclosure.x1 < enclosure.x2:
self.animal.x = random.randint(enclosure.x1, enclosure.x2)
if enclosure.y1 < enclosure.y2:
self.animal.y = random.randint(enclosure.y1, enclosure.y2)
if enclosure.y1 > enclosure.y2:
self.animal.y = random.randint(enclosure.y2, enclosure.y1)
if enclosure.x1 > enclosure.x2:
self.animal.x = random.randint(enclosure.x2, enclosure.x1)
if enclosure.y1 < enclosure.y2:
self.animal.y = random.randint(enclosure.y1, enclosure.y2)
if enclosure.y1 > enclosure.y2:
self.animal.y = random.randint(enclosure.y2, enclosure.y1)
2024-03-24 17:46:13 +01:00
if self.check(blocked, taken):
break
2024-03-24 17:46:13 +01:00
def check(self, blocked, taken):
x = self.animal.x
y = self.animal.y
2024-03-24 17:46:13 +01:00
if (x,y) in blocked or (x,y) in taken:
return False
2024-03-24 17:46:13 +01:00
taken.add((x,y))
return True