Adds an entity, making sure the entity does not override another entity >>> wt = WaTor(WIDTH, HEIGHT) >>> wt.set_planet([[None, None], [None, None]]) >>> wt.add_entity(True) >>> len(wt.get_entities()) 1 >>> wt.add_entity(False)
(self, prey: bool)
| 151 | self.height = len(planet) |
| 152 | |
| 153 | def add_entity(self, prey: bool) -> None: |
| 154 | """ |
| 155 | Adds an entity, making sure the entity does |
| 156 | not override another entity |
| 157 | |
| 158 | >>> wt = WaTor(WIDTH, HEIGHT) |
| 159 | >>> wt.set_planet([[None, None], [None, None]]) |
| 160 | >>> wt.add_entity(True) |
| 161 | >>> len(wt.get_entities()) |
| 162 | 1 |
| 163 | >>> wt.add_entity(False) |
| 164 | >>> len(wt.get_entities()) |
| 165 | 2 |
| 166 | """ |
| 167 | while True: |
| 168 | row, col = randint(0, self.height - 1), randint(0, self.width - 1) |
| 169 | if self.planet[row][col] is None: |
| 170 | self.planet[row][col] = Entity(prey=prey, coords=(row, col)) |
| 171 | return |
| 172 | |
| 173 | def get_entities(self) -> list[Entity]: |
| 174 | """ |