This is my first attempt at OOP/classes: a game where you will select a character and use attacks and abilities to defeat the enemy character. I have a few specific questions:
I was told that having the "while" statement in the Character doesn't make any sense but I don't know where I should put those. Also, notable right away, is what seems to me like a scissors and tape way of taking turns... I feel like there's a better way but I could not think of it.
import random class Character: def __init__(self, health, strength, mobility, energy): self.health = health self.strength = strength self.mobility = mobility self.energy = energy turn = False is_target = True is_dazed = False is_stunned = False is_debilitated = False is_confused = False is_terrified = False while is_debilitated: mobility = 0 def success_check(self): defense = roll_d_x(attack_dice) if defense >= defender.mobility: success = True else: success = False return success def skip_turn(self): print("Skipping turn.") self.turn = False player1 = None player2 = None defender = None attack_dice = 100 nec_starting_health = 80 nec_starting_strength = 10 nec_starting_mobility = 10 nec_starting_energy = 20 ghost_starting_health = 10 ghost_starting_strength = 10 ghost_starting_mobility = 30 ghost_starting_terr = 5 skel_starting_health = 30 skel_starting_strength = 30 skel_starting_mobility = 10 skel_starting_terr = 10 mons_starting_health = 50 mons_starting_strength = 20 mons_starting_mobility = 20 mons_starting_terr = 20 My idea for minions was to give the Necromancer a list and append these minions to it...good idea/bad idea?
class Necromancer(Character): def __init__(self): self.health = nec_starting_health self.strength = nec_starting_strength self.mobility = nec_starting_mobility self.energy = nec_starting_energy self.friendly_minions = list() def gain_health(self, amount): self.health += amount print(f"Health gained: {amount}") if self.health > nec_starting_health: self.health = nec_starting_health def lose_health(self, amount): self.health -= amount print(f"Health lost: {amount}") self.energy += amount print(f"Life Force gained: {amount}") if self.health <= 0: game_over = True def gain_strength(self, amount): self.strength += amount if self.strength > nec_starting_strength: self.strength = nec_starting_strength def lose_strength(self, amount): self.strength -= amount if self.strength < 0: self.strength = 0 def gain_mobility(self, amount): self.mobility += amount if self.mobility > nec_starting_mobility: self.mobility = nec_starting_mobility def lose_mobility(self, amount): self.mobility -= amount if self.mobility < 0: self.mobility = 0 def gain_energy(self, amount): self.energy += amount print(f"Life Force gained: {amount}") if self.energy > (nec_starting_energy + nec_starting_energy): self.energy = (nec_starting_energy + nec_starting_energy) def lose_energy(self, amount): self.energy -= amount print(f"Life Force lost: {amount}") if self.energy < 0: self.energy = 0 def sacrifice_random_minion(self): if len(self.friendly_minions) == 0: print("No Minion to sacrifice.") else: ran_minion = random.randint(0, (len(self.friendly_minions) - 1)) sacrificed_minion = self.friendly_minions[ran_minion] print("Minion sacrificed.") sacrificed_minion.show_attributes() self.friendly_minions.remove(sacrificed_minion) def attack(self): if self.is_dazed: print("Dazed...attack unsuccessful.") elif self.is_confused: confused_test = roll_d_x(2) if confused_test == 1: print("Confused...attack unsuccessful.") else: success = self.success_check() if success: defender.lose_health(self.strength) self.gain_health(20) else: print(f"Attack missed.") def cull(self): if self.is_stunned: print("Stunned...ability unsuccessful.") elif self.is_confused: confused_test = roll_d_x(2) if confused_test == 1: print("Confused...ability unsuccessful.") else: success = self.success_check() if success: if len(self.friendly_minions) == 0: print("Ability failed. No Minion to sacrifice.") else: ran_minion = random.randint(0, (len(self.friendly_minions) - 1)) sacrificed_minion = self.friendly_minions[ran_minion] print("Minion sacrificed.") sacrificed_minion.show_attributes() self.gain_health(sacrificed_minion.health) self.friendly_minions.remove(sacrificed_minion) else: print("Ability failed.") def call_ghost(self): if self.is_stunned: print("Stunned...ability unsuccessful.") elif self.is_confused: confused_test = roll_d_x(2) if confused_test == 1: print("Confused...ability unsuccessful.") else: success = self.success_check() if success: self.lose_health(5) self.lose_energy(10) ghost = Ghost() self.friendly_minions.append(ghost) print("Ghost Called.") else: print("Ability failed.") def raise_skeleton(self): if self.is_stunned: print("Stunned...ability unsuccessful.") elif self.is_confused: confused_test = roll_d_x(2) if confused_test == 1: print("Confused...ability unsuccessful.") else: success = self.success_check() if success: self.lose_health(10) self.lose_energy(25) skeleton = Skeleton() self.friendly_minions.append(skeleton) print("Skeleton Raised.") else: print("Ability failed") def summon_monster(self): if self.is_stunned: print("Stunned...ability unsuccessful.") elif self.is_confused: confused_test = roll_d_x(2) if confused_test == 1: print("Confused...ability unsuccessful.") else: success = self.success_check() if success: self.lose_health(20) self.lose_energy(40) monster = Monster() self.friendly_minions.append(monster) print("Monster Created.") else: print("Ability failed.") def start_turn(self): self.gain_energy(((100 - self.health) / 2) + 10) for minion in self.friendly_minions: minion.attack() def end_of_turn(self): self.is_dazed = False self.is_stunned = False self.is_debilitated = False self.is_confused = False self.is_terrified = False self.gain_energy((100 - self.health) / 2 + 10) if (self.health + self.energy) > 100: self.energy = (100 - self.health) class Minion: def __init__(self, health, strength, mobility, chance_terrify): self.health = health self.strength = strength self.mobility = mobility self.chance_terrify = chance_terrify is_target = True def show_attributes(self): print(f"Minion:\n{self.health} Health\n{self.strength} Strength\n{self.mobility} Mobility") def attack(self): flip = roll_d_x(2) if flip == 1: print("Minion attack failed.") else: defense = roll_d_x(100) if defense >= defender.mobility: defender.lose_health(self.strength) print(f"The minion attack is successful.") else: print(f"Attack missed.\n(Defense Roll, {defense}. Defender's Mobility, {defender.mobility})") class Ghost(Minion): def __init__(self): self.health = ghost_starting_health self.strength = ghost_starting_strength self.mobility = ghost_starting_mobility self.chance_terrify = ghost_starting_terr class Skeleton(Minion): def __init__(self): self.health = skel_starting_health self.strength = skel_starting_strength self.mobility = skel_starting_mobility self.chance_terrify = skel_starting_terr class Monster(Minion): def __init__(self): self.health = mons_starting_health self.strength = mons_starting_strength self.mobility = mons_starting_mobility self.chance_terrify = mons_starting_terr def roll_d_x(max_num): roll = random.randint(1, max_num) return roll This, again, is a but of scissors and tape. I'm not sure of a better way to do this.
def pick_starting_player(): flip = roll_d_x(2) if flip is 1: player1.turn = True else: player2.turn = True if player1.turn: print("Player 1 goes first.") else: print("Player 2 goes first.") def end_turn_p1(): player1.turn = False player2.turn = True def end_turn_p2(): player2.turn = False player1.turn = True This is for testing ... I need to learn how to make a main I think... Eventually, I would like to get this into pygame(?), have clickable buttons for abilities, visual monsters and heroes, etc...
player1 = Necromancer() player2 = Necromancer() pick_starting_player() game_over = False while not game_over: while player1.turn is True: defender = player2 player1.start_turn() print("***\nPlayer1") player1.show_skills() while True: user_skill = input("Enter the skill you wish to use: ") if user_skill.lower() == 'attack': player1.attack() break elif user_skill.lower() == 'cull': player1.cull() break elif user_skill.lower() == 'call ghost': player1.call_ghost() break elif user_skill.lower() == 'raise skeleton': player1.raise_skeleton() break elif user_skill.lower() == 'summon monster': player1.summon_monster() break else: print("Invalid skill") continue player1.end_of_turn() end_turn_p1() while player2.turn is True: defender = player1 player2.start_turn() print("Player2 does nothing on his turn.") end_turn_p2()