def run_game(self): fleet = Fleet() herd = Herd() player = self.display_welcome() team_size = user_prompt( "How many on each team? Less than 10 please. :", 10) herd.create_herd(team_size) fleet.create_fleet(team_size, player) self.fleet = fleet self.herd = herd print("\n" * 10) display(self.fleet, self.herd) input("Press enter to start the game...") winner = None game_on = True while game_on: choice = None print("Your turn!\n") if player == 1: choice = self.show_robo_opponent_options() if choice != "skip": self.robo_turn(int(choice)) # Computer Turn print(f"Computer's turn!\n") self.dino_turn(1, 2) else: choice = self.show_dino_opponent_options() if choice != "skip": attack_type = user_prompt( "What type of attack?\n1: Slam (more damage, higher energy cost)\n2: Bite\n:", 2) self.dino_turn(int(choice), int(attack_type)) # Computer Turn print(f"Computer's turn!\n") self.robo_turn(1) # Check for dead if self.herd.dinosaurs[0].health <= 0: print(f"{self.herd.dinosaurs[0].type} has died!") self.herd.dinosaurs.remove(self.herd.dinosaurs[0]) if self.fleet.robots[0].health <= 0: print(f"{self.fleet.robots[0].name} has died!") self.fleet.robots.remove(self.fleet.robots[0]) if len(self.herd.dinosaurs) < 1: winner = "Robots Win!" game_on = False if len(self.fleet.robots) < 1: winner = "Dinosaurs Win!" game_on = False self.display_winner(winner)
from robots import Robot from dinosaurs import Dinosaur from fleet import Fleet from herd import Herd from battlefield import Battlefield r1 = Robot('WALL-E') r2 = Robot('T-800') r3 = Robot('Roomba') d1 = Dinosaur('Trogdor', 55) d2 = Dinosaur('Allosaurus', 42) d3 = Dinosaur('Spinosaurus', 55) Fleet(3) Fleet.create_fleet(r1) Fleet.create_fleet(r2) Fleet.create_fleet(r3) print(Fleet.robot_fleet) Herd(3) Herd.create_herd(d1) Herd.create_herd(d2) Herd.create_herd(d3) print(Herd.dinosaur_herd)
class Battlefield: def __init__(self): self.fleet = Fleet() self.herd = Herd() def display_welcome(self): print('Welcome to the game!') # The next few functions generate a game automatically, with randomly selected dinos/robots for each turn (no user control). def generate_teams(self): self.fleet.create_fleet() self.herd.create_herd() print(f'\nROBOTS:') i = 0 while i < len(self.fleet.robots): print(f'{self.fleet.robots[i].name}') i += 1 print(f'\nDINOSAURS:') i = 0 while i < len(self.herd.dinosaurs): print(f'{self.herd.dinosaurs[i].type}') i += 1 def dino_turn(self): dino_index = random.randint(0, len(self.herd.dinosaurs) - 1) robot_index = random.randint(0, len(self.fleet.robots) - 1) current_dino = self.herd.dinosaurs[dino_index] current_robot = self.fleet.robots[robot_index] print("\n" + Dinosaur.attack(current_dino, current_robot)) if current_robot.health <= 0: print(f'{current_robot.name} health now depleted! {current_robot.name} OUT!') self.fleet.robots.remove(current_robot) else: print(f'{current_robot.name} health now {current_robot.health}') if current_dino.energy <= 0: print(f'{current_dino.type} energy now depleted! {current_dino.type} OUT!') self.herd.dinosaurs.remove(current_dino) else: print(f'{current_dino.type} energy now {current_dino.energy}') def robot_turn(self): dino_index = random.randint(0, len(self.herd.dinosaurs) - 1) robot_index = random.randint(0, len(self.fleet.robots) - 1) current_dino = self.herd.dinosaurs[dino_index] current_robot = self.fleet.robots[robot_index] print(f'\n{current_robot.name} attacks {current_dino.type} with {current_robot.weapon.type}!') Robot.attack(current_robot, current_dino) if current_dino.health <= 0: print(f'{current_dino.type} health now depleted! {current_dino.type} OUT!') self.herd.dinosaurs.remove(current_dino) else: print(f'{current_dino.type} health now {current_dino.health}') if current_robot.power_level <= 0: print(f'{current_robot.name} power level now depleted! {current_robot.name} OUT!') self.fleet.robots.remove(current_robot) else: print(f'{current_robot.name} power level now {current_robot.power_level}') def run_game(self): self.display_welcome() self.generate_teams() while len(self.herd.dinosaurs) > 0 and len(self.fleet.robots) > 0: self.dino_turn() if len(self.fleet.robots) > 0: self.robot_turn() if len(self.herd.dinosaurs) == 0: print('Robots win!') if len(self.fleet.robots) == 0: print('Dinosaurs win!') # These functions begin to take user input to control aspects of the game. They'll play as Team Robot. def user_fleet_builder(self): while True: try: amount_of_robots = input("How many combatants do you want on each team? Max 5 ") amount_of_robots = int(amount_of_robots) break except ValueError: print("\nOops! Enter a number from 1 to 5. ") if amount_of_robots > 5 or amount_of_robots <= 0: print('\nOops! That number was outside the available range. Your fleet has been set to 5 robots.') amount_of_robots = 5 self.fleet.user_fleet(amount_of_robots) def team_setup(self): print(f'\nHere is Team Robot: ') i = 0 while i < len(self.fleet.robots): name = self.fleet.robots[i].name weapon = self.fleet.robots[i].weapon.type attack_power = self.fleet.robots[i].weapon.attack_power energy_drain = self.fleet.robots[i].weapon.energy_drain print(f'{name}: {weapon}, attack power {attack_power}, energy drain per attack {energy_drain}') i += 1 dino_amount = len(self.fleet.robots) # while True: # try: # dino_amount = input('\nHow many dinosaurs do you want to fight? ') # dino_amount = int(dino_amount) # break # except ValueError: # print('Oops! Enter the number of dinosaurs you want to fight. ') self.herd.create_herd_custom_amount(dino_amount) print('\nHere is Team Dinosaur: ') i = 0 while i < len(self.herd.dinosaurs): name = self.herd.dinosaurs[i].type attack_power = self.herd.dinosaurs[i].attack_power energy_drain = self.herd.dinosaurs[i].energy_drain print(f'{name}, attack power {attack_power}, energy drain per attack {energy_drain}') i += 1 def show_team_info(self): print(f'\nHere is Team Robot: ') i = 0 while i < len(self.fleet.robots): name = self.fleet.robots[i].name weapon = self.fleet.robots[i].weapon.type attack_power = self.fleet.robots[i].weapon.attack_power health = self.fleet.robots[i].health power_level = self.fleet.robots[i].power_level energy_drain = self.fleet.robots[i].weapon.energy_drain print( f'Robot {i + 1}: {name}, {weapon}, attack power {attack_power}, health {health}, ' f'power level {power_level}, energy drain per attack {energy_drain}') i += 1 print('\nHere is Team Dinosaur: ') i = 0 while i < len(self.herd.dinosaurs): name = self.herd.dinosaurs[i].type attack_power = self.herd.dinosaurs[i].attack_power health = self.herd.dinosaurs[i].health energy = self.herd.dinosaurs[i].energy energy_drain = self.herd.dinosaurs[i].energy_drain print(f'{name}: attack power {attack_power}, health {health}, energy {energy}, ' f'energy drain per attack {energy_drain}') i += 1 def user_robot_turn(self): if len(self.fleet.robots) == 1: current_robot = self.fleet.robots[0] else: while True: robot_input = input('\nWhich robot do you want to use this turn? ') try: robot_index = 6 i = 0 while i < len(self.fleet.robots): if robot_input == self.fleet.robots[i].name: robot_index = i i += 1 current_robot = self.fleet.robots[robot_index] break except IndexError: print("Oops! No robots with that name. Try again... ") user_select = '' while user_select != 'attack' and user_select != 'heal': user_select = input(f'\nWould you like to attack a dinosaur, or heal your robot? ' f'Healing adds a random number of health points from 5-20 (up to max health), ' f'but it costs 15 power level! ' f'Enter "attack" or "heal" ') if user_select != 'attack' and user_select != 'heal': print(f'\nInvalid input! Try again...') if user_select == 'heal': self.user_robot_heal(current_robot) elif user_select == 'attack': self.user_robot_attack(current_robot) def user_robot_heal(self, robot): health_gain = random.randint(5, 20) robot.health += health_gain if robot.health > 100: print(f'{robot.name} health maxed out!') robot.health = 100 else: print(f'\n{robot.name} gains {health_gain} health. {robot.name} health now {robot.health}!') robot.power_level -= 15 if robot.power_level > 0: print(f'\n{robot.name} power level now {robot.power_level}!') elif robot.power_level <= 0: print(f'{robot.name} power level now depleted! {robot.name} OUT!') self.fleet.robots.remove(robot) def user_robot_attack(self, current_robot): if len(self.herd.dinosaurs) == 1: current_dino = self.herd.dinosaurs[0] else: current_dino = '' while current_dino == '': dino_name = 'Dinosaur ' + input('\nWhich dinosaur do you want to attack? Enter their Dinosaur Number: ') i = 0 while i < len(self.herd.dinosaurs): if self.herd.dinosaurs[i].type == dino_name: current_dino = self.herd.dinosaurs[i] i += 1 if current_dino == '': print(f'\nInvalid input! Try again...') print(f'\n{current_robot.name} attacks {current_dino.type} with {current_robot.weapon.type}!') Robot.attack(current_robot, current_dino) if current_dino.health <= 0: print(f'{current_dino.type} health now depleted! {current_dino.type} OUT!') self.herd.dinosaurs.remove(current_dino) else: print(f'{current_dino.type} health now {current_dino.health}') if current_robot.power_level <= 0: print(f'{current_robot.name} power level now depleted! {current_robot.name} OUT!') self.fleet.robots.remove(current_robot) else: print(f'{current_robot.name} power level now {current_robot.power_level}') def run_game_team_robots(self): print('You are Team Robot!') self.user_fleet_builder() self.team_setup() user_ready = 'yes' while len(self.herd.dinosaurs) > 0 and len(self.fleet.robots) > 0 and user_ready == 'yes': self.user_robot_turn() user_ready = input('\nReady to continue? Enter "yes" when ready ') while user_ready != "yes": user_ready = input('\nReady to continue? Enter "yes" when ready ') if len(self.herd.dinosaurs) > 0: self.dino_turn() if len(self.fleet.robots) > 0: user_ready = input('\nReady to continue? Enter "yes" when ready ') while user_ready != "yes": user_ready = input('\nReady to continue? Enter "yes" when ready ') self.show_team_info() if len(self.herd.dinosaurs) == 0: print('All dinosaurs depleted! Robots win!') if len(self.fleet.robots) == 0: print('All robots depleted! Dinosaurs win!')
from dinosaur import Dinosaur from herd import Herd from fleet import Fleet from battlefield import Battlefield star_fleet = Fleet("Star Fleet") star_fleet.create_fleet() the_dinos = Herd("The Dinos") the_dinos.create_herd()
class Battlefield: def __init__(self): self.fleet = Fleet() self.herd = Herd() self.dino_count = 0 self.robo_count = 0 self.fleet_health = 0 self.herd_health = 0 self.turn_arrow = '' for robo in self.fleet.robots: self.fleet_health += robo.health for dino in self.herd.dinos: self.herd_health += dino.health def run_game(self): self.fleet.create_fleet() self.herd.create_herd() self.display_welcome() coin = random.randint(1, 2) if coin == 1: print("Coin flip came up heads it's the Dinosaur's turn") self.dino_turn(self.herd.dinos[self.dino_count]) else: print("Coin flip came up Tails it's the Robot's turn") self.robo_turn(self.fleet.robots[self.robo_count]) while self.fleet_health > 0 and self.herd_health > 0: if self.turn_arrow == 'dinos': print(f"It is the dinosaur's turn next") self.dino_turn(self.herd.dinos[self.dino_count]) else: print("It is the Robot's turn next") self.robo_turn(self.fleet.robots[self.robo_count]) self.display_winners() def display_welcome(self): print( "Welcome, we will now have the opening coin flip. Dinosaurs go first on heads" ) def battle(self): self.fleet_health = 0 self.herd_health = 0 for robo in self.fleet.robots: self.fleet_health += robo.health for dino in self.herd.dinos: self.herd_health += dino.health def dino_turn(self, dinosaur): self.show_dino_opponent_options() dinosaur.attack(self.fleet.robots[ int(input("What robot number do you wish to attack")) - 1]) if self.dino_count >= 2: self.dino_count = 0 else: self.dino_count += 1 self.battle() self.turn_arrow = "robots" def robo_turn(self, robot): self.show_robo_opponent_options() robot.attack(self.herd.dinos[ int(input("What dinosaur number do you wish to attack")) - 1]) if self.robo_count >= 2: self.robo_count = 0 else: self.robo_count += 1 self.battle() self.turn_arrow = "dinos" def show_dino_opponent_options(self): i = 0 for robo in self.fleet.robots: if robo.health > 0: print( f"Robot number {i+1} is armed with a {robo.weapon.type} and has {robo.health} remaining" ) i += 1 def show_robo_opponent_options(self): i = 0 for dino in self.herd.dinos: if dino.health > 0: print(f"dinosaur number {i + 1} has {dino.health} remaining") i += 1 def display_winners(self): if self.fleet_health > self.herd_health: print("The Robots won") else: print("The Dinosaurs won")
class Battlefield: def __init__(self): self.fleet = Fleet() self.herd = Herd() self.turn = "robot" self.run = False self.winner = '' def display_welcome(self): print() print() print("Welcome to Robots Vs Dinosaurs.") print() time.sleep(2) print("Here you will be given a fleet of robots.") print() time.sleep(2) print( "Your robots will pick a random weapon and you will choose which robot attacks that turn." ) print() time.sleep(2) print( "Each attack consumes energy if your robot runs out of energy they won't be able to attack." ) print() time.sleep(2) print("If your robots run out of health they die.") print() time.sleep(2) print("Eliminate all the dinosaurs to win.") print() time.sleep(2) print("Standy by initializing robots") print("10%") print() time.sleep(1) print("Standy by initializing robots") print("30%") print() time.sleep(1) print("Standy by initializing robots") print("60%") print() time.sleep(1) print("Standy by initializing robots") print("100%") print("Initialization Complete") print() time.sleep(1) self.fleet.create_fleet() self.herd.create_herd() print() start = input( "Your fleet is ready to deploy? Are you ready to start the battle? (y/n): " ) test_start = start.lower() if test_start == 'y': self.run = True self.run_game() print("Good luck") print() else: print() print("Goodbye") def show_dino_options(self): print("Waiting for Dinosaur to attack") print() def show_robo_options(self): valid_for_show_robo = False while valid_for_show_robo == False: length = len(self.fleet.robots) if length == 3: print() selection = int( input( "Select which Robot is going to attack: (1, 2, or 3) : " )) - 1 if selection != 0 and selection != 1 and selection != 2: print("Invalid input please try again") print() else: selected_attacker = self.fleet.robots[selection] valid_for_show_robo = True return selected_attacker elif length == 2: print() selection = int( input("Select which Robot is going to attack: (1 or 2 ) : " )) - 1 print() if selection != 0 and selection != 1: print("Invalid input please try again") print() else: selected_attacker = self.fleet.robots[selection] valid_for_show_robo = True return selected_attacker elif length == 1: time.sleep(2) selected_attacker = self.fleet.robots[0] valid_for_show_robo = True return selected_attacker def dino_turn(self): if self.turn == "dino": self.show_dino_options() def robo_turn(self): if self.turn == "robot": self.show_robo_options() def display_winners(self): if self.winner == "robot": print("The dinosaurs have been defeated. Robots win!!!") else: print("The Robots have been defeated. dinosaurs win!!!") def battle(self): if self.turn == 'robot': attacker = self.show_robo_options() attacker.attack(self.herd.dinosaurs[0]) self.turn = "dino" print() else: self.show_dino_options() self.herd.dinosaurs[0].attack(self.fleet.robots[0]) self.turn = "robot" print() def check_for_death(self): if self.herd.dinosaurs[0].health <= 0: print(self.herd.dinosaurs[0].type + " Was defeated") print() del self.herd.dinosaurs[0] elif self.fleet.robots[0].health <= 0: print(self.fleet.robots[0].name + " Was defeated") print() del self.fleet.robots[0] def check_for_win(self): if len(self.herd.dinosaurs) == 0: self.winner = 'robot' self.run = False elif len(self.fleet.robots) == 0: self.winner = 'dino' self.run = False def run_game(self): while self.run == True: self.battle() self.check_for_death() self.check_for_win() if self.winner != "": self.display_winners()
class Battlefield: def __init__(self): self.turn = 1 self.user_selector = 0 self.enemy_selector = 0 self.target = 0 self.fleet = Fleet() self.herd = Herd() self.user_choice = "" self.user = None self.enemy = None self.user_attacker = None self.enemy_attacked = None self.enemy_attacker = None self.user_attacked = None self.try_again = 0 def run_game(self): self.display_welcome() self.fleet.create_fleet() self.herd.create_herd() self.user_choice = input('Choose "dinos" or "robots"') while (self.user_choice != 'dinos') and (self.user_choice != 'robots'): self.user_choice = input( 'Incorrect Input: Choose "dinos" or "robots"') if self.user_choice == "dinos": self.user = self.herd.dinosaurs self.enemy = self.fleet.robots print("\nYou're team of dinos consists of:") for dino in self.user: print( f'Type - {dino.type}, Energy - {dino.energy}, Power - {dino.attack_power},Health - {dino.health}' ) print("\nYou're enemy robots are:") for robo in self.enemy: print( f'Name - {robo.name}, Energy - {robo.power_level}, Health - {robo.health}, Weapon - {robo.weapon.type} (atk) {robo.weapon.attack_power}' ) else: self.user = self.fleet.robots self.enemy = self.herd.dinosaurs print("\nYou're team of robots consists of:") for robo in self.user: print( f'Name - {robo.name}, Energy - {robo.power_level}, Health - {robo.health}, Weapon - {robo.weapon.type} (atk) {robo.weapon.attack_power}' ) print("\nYou're enemy dinosaurs are:") for dino in self.enemy: print( f'Type - {dino.type}, Energy - {dino.energy}, Power - {dino.attack_power},Health - {dino.health}' ) self.battle() def display_welcome(self): print("Welcome to Robots vs Dinosaurs!") def battle(self): if self.turn % 2 == 1: if self.user == self.herd.dinosaurs: self.dino_turn() if self.enemy_attacked.health < 1: print(f"{self.enemy_attacked.name} has died!") del self.enemy[self.enemy_selector] else: self.robot_turn() if self.enemy_attacked.health < 1: print(f"{self.enemy_attacked.type} has died!") del self.enemy[self.enemy_selector] else: if self.user == self.herd.dinosaurs: self.robot_turn() if self.user_attacked.health < 1: print(f"{self.user_attacked.type} has died!") del self.user[self.user_selector] else: self.dino_turn() if self.user_attacked.health < 1: print(f"{self.user_attacked.name} has died!") del self.user[self.user_selector] if len(self.user) > 0: if len(self.enemy) > 0: self.enemy_selector = 0 self.turn += 1 self.battle() else: print(f"You've Won! The winning team is:") self.display_winners() self.play_again() if self.try_again == 1: self.run_game() else: print("\n Thanks for playing Robots vs Dinosaurs!!") else: print("You've Lost... The winning team is:") self.display_winners() self.play_again() if self.try_again == 1: self.run_game() else: print("\n Thanks for playing Robots vs Dinosaurs!!") def dino_turn(self): if self.user == self.herd.dinosaurs: self.user_attacker = self.user[self.user_selector] print( f"\nYou're attacker is: Type - {self.user_attacker.type}, Energy - {self.user_attacker.energy}, Power - {self.user_attacker.attack_power},Health - {self.user_attacker.health}" ) print(f'\nPick an enemy to attack!\n') self.show_robo_opponent_options() self.enemy_selector = int(input("Enter enemy number:")) while (self.enemy_selector < 0) or (self.enemy_selector > len(self.enemy) - 1): self.enemy_selector = int( input("Incorrect Input: Please select enemy number")) self.enemy_attacked = self.enemy[self.enemy_selector] print( f"\n{self.user_attacker.type} attacks {self.enemy_attacked.name} for {self.user_attacker.attack_power} damage!" ) self.user_attacker.attack_robot(self.enemy_attacked) if self.user_selector >= (len(self.user) - 1): self.user_selector = 0 else: self.user_selector += 1 else: self.target = random.randint(0, len(self.user) - 1) self.enemy_attacker = self.enemy[random.randint( 0, len(self.enemy) - 1)] self.user_attacked = self.user[self.target] print( f"\n{self.enemy_attacker.type} attacks {self.user_attacked.name} for {self.enemy_attacker.attack_power} damage!" ) self.enemy_attacker.attack_robot(self.user_attacked) if self.user_selector >= (len(self.user) - 1): self.user_selector = 0 def show_robo_opponent_options(self): for robo in self.enemy: print( f'Enter "{self.enemy_selector}" - {robo.name}, Energy - {robo.power_level}, Health - {robo.health}, Weapon - {robo.weapon.type} (atk) {robo.weapon.attack_power}' ) self.enemy_selector += 1 def robot_turn(self): if self.user == self.fleet.robots: self.user_attacker = self.user[self.user_selector] print( f"\nYou're attacker is: Name - {self.user_attacker.name}, Energy - {self.user_attacker.power_level}, Health - {self.user_attacker.health}, Weapon - {self.user_attacker.weapon.type} (atk) {self.user_attacker.weapon.attack_power}" ) print("\nPick an enemy to attack!\n") self.show_dino_opponent_options() self.enemy_selector = int(input("Enter enemy number:")) while (self.enemy_selector < 0) or (self.enemy_selector > len(self.enemy) - 1): self.enemy_selector = int( input("Incorrect Input: Please select enemy number")) self.enemy_attacked = self.enemy[self.enemy_selector] print( f"\n{self.user_attacker.name} attacks {self.enemy_attacked.type} for {self.user_attacker.weapon.attack_power} damage!" ) self.user_attacker.attack_dinosaur(self.enemy_attacked) if self.user_selector >= (len(self.user) - 1): self.user_selector = 0 else: self.user_selector += 1 else: self.target = random.randint(0, len(self.user) - 1) self.enemy_attacker = self.enemy[random.randint( 0, len(self.enemy) - 1)] self.user_attacked = self.user[self.target] print( f"\n{self.enemy_attacker.name} attacks {self.user_attacked.type} for {self.enemy_attacker.weapon.attack_power} damage!" ) self.enemy_attacker.attack_dinosaur(self.user_attacked) if self.user_selector >= (len(self.user) - 1): self.user_selector = 0 def show_dino_opponent_options(self): for dino in self.enemy: print( f'Enter "{self.enemy_selector}" - {dino.type}, Energy - {dino.energy}, Health - {dino.health}, Power - {dino.attack_power}' ) self.enemy_selector += 1 def display_winners(self): if self.user_choice == "dinos": if len(self.user) == 0: print("The Robots!") for robot in self.fleet.robots: print(f"Champion {robot.name}!!!") else: print("The Dinosaurs!") for dino in self.herd.dinosaurs: print(f"Champion {dino.type}!!!") else: if len(self.user) == 0: print("The Dinosaurs!") for dino in self.herd.dinosaurs: print(f"Champion {dino.type}!!!") else: print("The Robots!") for robot in self.fleet.robots: print(f"Champion {robot.name}!!!") def play_again(self): self.try_again = input(f'Enter "1" if you want to play again!') if not self.try_again == "1": self.try_again = 0 else: self.try_again = 1