def start_game(): pygame.init() game_settings = Settings() screen = pygame.display.set_mode( (game_settings.screen_width, game_settings.screen_height)) pygame.display.set_caption("Starship Troopers") # Create the starship starship = Starship(game_settings, screen) # Create a group of missiles missiles = Group() # Create a group of aliens and a fleet aliens = Group() gf.create_alien_fleet(game_settings, screen, starship, aliens) while True: gf.check_for_events(game_settings, screen, starship, missiles) starship.update_position(game_settings) gf.update_missiles_position_and_count(missiles) gf.update_aliens(game_settings, aliens) gf.update_screen(game_settings, screen, starship, missiles, aliens)
def test_passwordAnalyzer(self): starship = Starship() passwordRange = 112233, 112234 self.assertEqual(starship.validPasswordCombinations(*passwordRange), 1) passwordRange = 123444, 123445 self.assertEqual(starship.validPasswordCombinations(*passwordRange), 0) passwordRange = 111122, 111123 self.assertEqual(starship.validPasswordCombinations(*passwordRange), 1)
def random_ship(): ''' Return a starship with randomly generated values TO-DO: - Randomness needs to implemented, possibly using `faker` module ''' return Starship('USS Enterprise',StarshipClass.CONSTITUTION,'NCC-1701',400)
def test_tmx(): app = bounce.pyGameAppMap(bounce.MapFile) app.init() app.starship = Starship(app.clock, app.wmap) app.sprites.add(app.starship) app.on_execute()
def generate_ships(selected_encounters: List[Dict[str, int]], selected_coords: List[Coords], ai_difficulty: type[BaseAi]): for encounter, co in zip(selected_encounters, selected_coords): star_system = self.grid[co.y][co.x] all_ships: List[str] = [] for k, v in encounter.items(): all_ships.extend([k] * v) safe_spots = star_system.find_random_safe_spots( how_many=len(all_ships)) for k, local_co in zip(all_ships, safe_spots): ship_class = ALL_SHIP_CLASSES[k] starship = Starship(ship_class, ai_difficulty, local_co.x, local_co.y, star_system.coords.x, star_system.coords.y) starship.game_data = self yield starship
def ship_gen(_name, _class, _registry, _crew): ship = Starship(_name, _class, _registry, _crew) print(ship)
def handle_torpedo(self, *, shipThatFired: Starship, torpsFired: int, heading: int, coords: Tuple[Coords], torpedo_type: Torpedo, ships_in_area: Dict[Coords, Starship]): torpedo = torpedo_type #posX, posY = shipThatFired.local_coords.x, shipThatFired.local_coords.y descriptive_number = "a" if torpsFired == 1 else f"{torpsFired}" plural = "torpedo" if torpsFired == 1 else "torpedos" self.engine.message_log.add_message( f"Firing {descriptive_number} {torpedo.name} {plural} at heading {heading}..." if shipThatFired.is_controllable else f"{shipThatFired.name} has fired {descriptive_number} {torpedo.name} {plural} at heading {heading:3.2f}...", colors.yellow) g: SubSector = self.grid[shipThatFired.sector_coords.y][ shipThatFired.sector_coords.x] shipsInArea = ships_in_area for t in range(torpsFired): hitSomething = False missed_the_target = False x = 0 y = 0 for co in coords: #x_, y_ = co.x, co.y if not (0 <= co.x < CONFIG_OBJECT.subsector_width) or not ( 0 <= co.y < CONFIG_OBJECT.subsector_height): break x, y = co.x, co.y try: star = g.stars_dict[co] self.engine.message_log.add_message( f"The torpedo impacts against a star at {co.x}, {co.y}." ) hitSomething = True except KeyError: try: planet = g.planets_dict[co] planet.hit_by_torpedo(shipThatFired.is_controllable, self, torpedo) hitSomething = True except KeyError: try: ship = shipsInArea[co] try: crew_readyness = shipThatFired.life_support.crew_readyness except AttributeError: crew_readyness = 1 try: target_crew_readyness = ship.life_support.crew_readyness except AttributeError: target_crew_readyness = 1 estimated_enemy_impulse = ship.impulse_engine.get_effective_value hitSomething = shipThatFired.roll_to_hit( ship, damage_type=DAMAGE_TORPEDO, estimated_enemy_impulse=estimated_enemy_impulse, systems_used_for_accuray=( shipThatFired.sensors.get_effective_value, shipThatFired.torpedo_launcher. get_effective_value), crew_readyness=crew_readyness, target_crew_readyness=target_crew_readyness) if hitSomething: ship_name = "We were" if ship.is_controllable else f"{ship.name} was" shipThatFired_name = "us" if shipThatFired.is_controllable else shipThatFired.name self.engine.message_log.add_message( f'{ship_name} hit by a {torpedo.name} torpedo from {shipThatFired.name}. ' ) ship.take_damage( torpedo.damage, f'Destroyed by a {torpedo.name} torpedo hit from the {shipThatFired_name}', damage_type=DAMAGE_TORPEDO) else: ship_name = "us" if ship.is_controllable else ship.name shipThatFired_name = "us" if shipThatFired.is_controllable else shipThatFired.name self.engine.message_log.add_message( f'A {torpedo.name} torpedo from {shipThatFired_name} missed {ship_name}. ' ) missed_the_target = True except KeyError: pass if hitSomething: break if not hitSomething: self.engine.message_log.add_message( "The torpedo misses the target!" if missed_the_target else f"The torpedo vears off into space at {x}, {y}!", colors.orange) shipThatFired.torpedo_launcher.torps[torpedo] -= torpsFired
def set_up_game(self, ship_name: str, captain_name: str): self.captain_name = captain_name self.grid = [[SubSector(self, x, y) for x in self.subsecs_range_x] for y in self.subsecs_range_y] self.player_subsector_info = [[ SubSectorInfo(x, y) for x in self.subsecs_range_x ] for y in self.subsecs_range_y] self.enemy_subsector_info = [[ SubSectorInfo(x, y) for x in self.subsecs_range_x ] for y in self.subsecs_range_y] # create stars and planets for x in self.subsec_size_range_x: for y in self.subsec_size_range_y: self.grid[y][x].random_setup(self.star_number_weights, self.star_number_weights_len) # create a tuple that contains self.subsecs_range_x * self.subsecs_range_y Coords system_coords = tuple( Coords(x=x, y=y) for x in self.subsecs_range_x for y in self.subsecs_range_y) all_enemy_encounters: List[Dict[str, int]] = [] for a in self.scenerio.enemy_encounters: l = list(a.generate_ships()) all_enemy_encounters.extend(l) total_enemy = len(all_enemy_encounters) all_allied_encounters: List[Dict[str, int]] = [] for a in self.scenerio.allied_encounters: l = list(a.generate_ships()) all_allied_encounters.extend(l) total_allied = len(all_allied_encounters) selected_coords = choices(system_coords, k=total_enemy + total_allied + 1) # we use k = total + 1 because the last coord in selected_coords will be used as the players starting point selected_enemy_coords = selected_coords[:total_enemy] # we use k = total + 1 because the last coord in selected_coords will be used as the players starting point selected_allied_coords = selected_coords[total_enemy:total_enemy + total_allied] try: player_starting_coord = choice(selected_allied_coords) except IndexError: coords_without_enemies = [ co for co in system_coords if co not in selected_enemy_coords ] player_starting_coord = choice(coords_without_enemies) def generate_ships(selected_encounters: List[Dict[str, int]], selected_coords: List[Coords], ai_difficulty: type[BaseAi]): for encounter, co in zip(selected_encounters, selected_coords): star_system = self.grid[co.y][co.x] all_ships: List[str] = [] for k, v in encounter.items(): all_ships.extend([k] * v) safe_spots = star_system.find_random_safe_spots( how_many=len(all_ships)) for k, local_co in zip(all_ships, safe_spots): ship_class = ALL_SHIP_CLASSES[k] starship = Starship(ship_class, ai_difficulty, local_co.x, local_co.y, star_system.coords.x, star_system.coords.y) starship.game_data = self yield starship self.all_enemy_ships = list( generate_ships(all_enemy_encounters, selected_enemy_coords, self.difficulty)) self.target_enemy_ships = [ ship for ship in self.all_enemy_ships if ship.is_mission_critical ] self.all_allied_ships = list( generate_ships(all_allied_encounters, selected_allied_coords, self.allied_ai)) self.target_allied_ships = [ ship for ship in self.all_allied_ships if ship.is_mission_critical ] randXsec = player_starting_coord.x randYsec = player_starting_coord.y locPos = self.grid[randYsec][randXsec].find_random_safe_spot() player_ship_class = self.scenerio.your_ship self.player = Starship(player_ship_class, BaseAi, locPos.x, locPos.y, randXsec, randYsec, name=ship_name) self.player.game_data = self self.engine.player = self.player all_other_ships = self.all_enemy_ships + self.all_allied_ships shuffle(all_other_ships) self.all_other_ships = all_other_ships self.total_starships = [self.player] + self.all_other_ships self.ships_in_same_sub_sector_as_player = self.grab_ships_in_same_sub_sector( self.player, accptable_ship_statuses={ STATUS_ACTIVE, STATUS_CLOAK_COMPRIMISED, STATUS_CLOAKED, STATUS_DERLICT, STATUS_HULK }) self.visible_ships_in_same_sub_sector_as_player = [ ship for ship in self.ships_in_same_sub_sector_as_player if ship.ship_status.is_visible ] self.set_condition() for ship in self.total_starships: self.run_update_for_ship(ship) self.engine.message_log.add_message( f"Welcome aboard, {self.player.ship_class.nation.captain_rank_name} {self.captain_name}." )