import random class Spawner: def __init__(self, animal, enclosures): self.animal = animal # Wyrażenie listowe filtrujące tylko te wybiegi, które pasują do środowiska zwierzęcia self.enclosures = [enclosure for enclosure in enclosures if enclosure.type == self.animal.environment] def spawn_animal(self, blocked, taken): enclosure = random.choice(self.enclosures) while True: if self.animal.adult: self.animal.x = random.randint(enclosure.x1+1, enclosure.x2-2) self.animal.y = random.randint(enclosure.y1+1, enclosure.y2-2) else: self.animal.x = random.randint(enclosure.x1+1, enclosure.x2) self.animal.y = random.randint(enclosure.y1+1, enclosure.y2) if self.check(blocked, taken): break def check(self, blocked, taken): x = self.animal.x y = self.animal.y if (x,y) in blocked or (x,y) in taken: return False if self.animal.adult: adult_fields = [(x, y), (x+1,y), (x,y+1), (x+1,y+1)] # Duże zwierze zajmuje 4 pola if any(field in taken for field in adult_fields): # Jeśli stawiane zwierze jest dorosłe i jakiekolwiek pole jest zajęte, to nie można postawić zwierzęcia return False for field in adult_fields: # Dodaj wszystkie pola zajęte przez duże zwierzę taken.add(field) else: taken.add((x,y)) return True