# asteroid.py # ------------ # By MWC Contributors # This module defines an asteroid agent class. class Asteroid: character = 'O' def __init__(self, position, speed=2): # speed = how often it moves (1 = every turn, 2 = every 2 turns, etc.) self.position = position self.speed = speed def play_turn(self, game): width, height = game.board_size # Only move on some turns (for variable speed) if game.turn_number % self.speed != 0: return x, y = self.position # If at bottom, remove asteroid if y == height - 1: game.remove_agent(self) else: ship = game.get_agent_by_name('ship') new_position = (x, y + 1) # If we hit the ship, damage it and remove this asteroid if new_position == ship.position: if hasattr(ship, "take_damage"): ship.take_damage(game) game.remove_agent(self) else: self.position = new_position