def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.scree_width,ai_settings.scree_height)) background = pygame.image.load(r'images/background.bmp') pygame.display.set_caption("Alien Invasion") stats = GameStats(ai_settings) # 创建一辆飞船 ship = Ship(ai_settings,screen) # 创建一个用于存储子弹的编组 bullets = Group() aliens = Group() # 创建一群外星人 gf.create_fleet(ai_settings,screen,aliens,ship) # 开始游戏主循环 while True: gf.check_events(ai_settings, screen, ship, bullets) ship.update() gf.update_aliens(ai_settings,aliens,ship,stats,screen,bullets) gf.update_bullets(bullets,aliens,ai_settings, screen,ship) gf.update_screen(ai_settings, screen, ship,aliens, bullets,background)
def __init__(self) -> None: """ __init__() create Model object. """ self.screens: Dict[str, View] = { "Main": Main_Screen, "Start": Start_Screen, "Game Over": Game_Over_Screen, } self.current_screen = "Start" self.ship = Ship() self.current_obstacles: List[Obstacle] = [] self.time = 0 self.screen_size: Tuple[int, int] = (0, 0) self.obstacle_movement_speed = 10
def add_dict_of_ships(self, dict_of_ships): """ turns a dictionary into a set of ships and adds them to the ship ship must be in format {"s_type":"2", "set_of_coordinates" : [{"x":"a", "y":1},{"x":"a", "y":2}]} """ for ship in dict_of_ships: set_of_coordinates = [] for coordinates in ship['set_of_coordinates']: new_coordinates = Coordinates(coordinates['x'], coordinates['y']) set_of_coordinates.append(new_coordinates) new_ship = Ship(int(ship['s_type']), set_of_coordinates) self.ships.append(new_ship)
def test_ship_is_dead(self, ): coordinates1 = Coordinates("a",3) coordinates2 = Coordinates("a", 4) coordinates3 = Coordinates("a", 2) ship = Ship(3, [coordinates1, coordinates2, coordinates3]) ship.hit_points = 0 self.assertEqual(True, ship.is_dead()) ship.hit_points = 1 self.assertEqual(False, ship.is_dead())
def test_ship_dead_coordinates(self, ): ship = Ship(1, [Coordinates("b",2)]) actual = ship.calculate_dead_coordinates() expected = [Coordinates("b",1), Coordinates("b",3), Coordinates("a",1), Coordinates("a",3), Coordinates("c",1), Coordinates("c",3), Coordinates("a",2), Coordinates("c",2)] for i in range(len(expected)): expected[i].x = actual[i].x ship = Ship(2, [Coordinates("b", 2), Coordinates("b", 1)]) actual = ship.calculate_dead_coordinates() expected = [Coordinates("b", 3), Coordinates("a", 1), Coordinates("a", 3), Coordinates("c", 1), Coordinates("c", 3), Coordinates("a", 2), Coordinates("c", 2)] for i in range(len(expected)): expected[i].x = actual[i].x
def addHulk(self, name, position): self.hulks[name] = (self.makePT(Ship(pos=position, name=name), None))
def test_is_hit(self): set_of_coordinates= [Coordinates('a', 1), Coordinates('a', 2)] ship = Ship(2, set_of_coordinates) self.assertTrue(ship.is_hit(Coordinates('a', 1))) self.assertFalse(ship.is_hit(Coordinates('a', 3)))