Week 5
| class Ship: | |
| def __init__(self, oxygen, hull, name, crew): # constructor | |
| self.oxygen = oxygen | |
| self.hull = hull | |
| self.name = name | |
| self.crew = crew | |
| self.morale = 100 | |
| def check_defeat_status(self): | |
| if self.oxygen <= 0: | |
| return " Oxygen depleted" | |
| elif self.hull <= 0: | |
| return " Hull destroyed" | |
| return None | |
| def deduct_oxygen(self): | |
| self.oxygen -= 8 | |
| def show_resources(self): | |
| print(f" Oxygen: {self.oxygen} | Hull: {self.hull} | Morale: {self.morale}") | |
| def __str__(self): | |
| return f""" | |
| Ship object with the following parameters: | |
| oxygen = {self.oxygen} | |
| hull = {self.hull} | |
| name = {self.name}""" | |
| class AsteroidField: | |
| def __init__(self, danger_level): | |
| self.danger_level = danger_level | |
| self.base_damage = 10 | |
| def process(self, ship): | |
| damage = self.base_damage + (self.danger_level * 2) | |
| damage = int(damage * (200 - ship.morale) / 100) | |
| ship.hull = ship.hull - damage | |
| ship.morale = ship.morale - 5 | |
| return f" Asteroid debris slams into the hull! Hull takes {damage} damage." | |
| class Raider: | |
| def __init__(self, danger_level): | |
| self.danger_level = danger_level | |
| self.base_damage = 12 | |
| self.oxygen_cost = 5 | |
| def process(self, ship): | |
| damage = self.base_damage + (self.danger_level * 2) | |
| damage = int(damage * (200 - ship.morale) / 100) | |
| oxygen_lost = int(self.oxygen_cost * (200 - ship.morale) / 100) | |
| ship.hull = ship.hull - damage | |
| ship.oxygen = ship.oxygen - oxygen_lost | |
| ship.morale = ship.morale - 10 | |
| return ( | |
| f" Raiders attack! The crew fights them off but takes " | |
| f"{damage} hull damage and uses {oxygen_lost} extra oxygen." | |
| ) | |
| class Trader: | |
| def __init__(self): | |
| self.oxygen_refill = 15 | |
| self.hull_repair = 10 | |
| def process(self, ship): | |
| refill = int(self.oxygen_refill * ship.morale / 100) | |
| repair = int(self.hull_repair * ship.morale / 100) | |
| ship.oxygen = ship.oxygen + refill | |
| ship.hull = ship.hull + repair | |
| ship.morale = ship.morale + 10 | |
| return ( | |
| f" A friendly trader offers supplies. " | |
| f"Oxygen restored by {refill}, hull repaired by {repair}." | |
| ) | |
| class EmptySpace: | |
| def process(self, ship): | |
| ship.morale = ship.morale + 5 | |
| return " All is calm. The crew rests and takes in the view." |
| from engine.display import show_encounter | |
| from classes import AsteroidField, Raider, Trader, EmptySpace | |
| def should_stop(): | |
| answer = input(" Should stop? (y/n): ") | |
| if answer == "y": | |
| return True | |
| else: | |
| return False | |
| def scan_destination(planet): | |
| danger_level = planet["danger_level"] | |
| if danger_level < 3: # 1 or 2 | |
| print(f" {planet['name']} >> SAFE") | |
| elif danger_level == 3: | |
| print(f" {planet['name']} >> RISKY") | |
| else: | |
| print(f" {planet['name']} >> DANGEROUS") | |
| def create_encounter(destination): | |
| encounter_type = destination["encounter"] | |
| danger = destination["danger_level"] | |
| if encounter_type == "asteroid_field": | |
| return AsteroidField(danger) | |
| elif encounter_type == "raider": | |
| return Raider(danger) | |
| elif encounter_type == "trader": | |
| return Trader() | |
| else: | |
| return EmptySpace() | |
| def process_destination(ship, destination): | |
| if should_stop(): | |
| encounter = create_encounter(destination) | |
| narration = encounter.process(ship) | |
| show_encounter(narration) | |
| if destination["has_water"]: | |
| ship.oxygen = ship.oxygen + 20 | |
| ship.morale = ship.morale + 5 | |
| show_encounter(" The crew extracts oxygen from the planet's water. Oxygen restored by 20.") | |
| else: | |
| show_encounter(" You fly past without stopping.") |
| from constants import STARTING_HULL, STARTING_OXYGEN, SHIP_NAME, CREW_DESCRIPTION, GALAXY_SIZE | |
| from functions import scan_destination, process_destination | |
| from engine.display import show_intro, show_destination, show_defeat, show_victory | |
| from engine.galaxy import create_galaxy | |
| from classes import Ship | |
| def main(): | |
| ship = Ship(STARTING_OXYGEN, STARTING_HULL, SHIP_NAME, CREW_DESCRIPTION) | |
| show_intro(ship.name, ship.crew) | |
| galaxy = create_galaxy(GALAXY_SIZE) | |
| # game loop | |
| for i in range(len(galaxy)): | |
| destination = galaxy[i] | |
| show_destination(destination, i, len(galaxy)) | |
| scan_destination(destination) | |
| ship.deduct_oxygen() | |
| process_destination(ship, destination) | |
| ship.show_resources() | |
| cause = ship.check_defeat_status() | |
| if cause: | |
| show_defeat(ship.name, cause) | |
| return | |
| show_victory(ship.name) | |
| if __name__ == "__main__": | |
| main() |
评论
?
参与讨论