|
| 1 | +import math |
| 2 | +from random import randint |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +from entity import Entity |
| 6 | +from equipment import armors, weapons |
| 7 | +from dice import roll |
| 8 | + |
| 9 | +CHARACTER_ATTRIBUTES = ("Strength", "Dexterity", "Constitution", "Intelligence", "Wisdom", "Charisma") |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class PlayerClass: |
| 13 | + name: str |
| 14 | + starting_hp: int |
| 15 | + hit_die: int |
| 16 | + |
| 17 | +classes = list() |
| 18 | +classes.append(PlayerClass("Fighter", 6, 10)) |
| 19 | + |
| 20 | +# Class to represent a base D&D character. |
| 21 | +class Character(Entity): |
| 22 | + def __init__(self): |
| 23 | + Entity.__init__(self, input("Name your character: ")) |
| 24 | + self.level = 1 |
| 25 | + self.picker("class", classes) |
| 26 | + self.reset_attributes() |
| 27 | + self.pick_attributes(26) |
| 28 | + self.max_hp = self.player_class.starting_hp + \ |
| 29 | + self.get_attribute_mod(CHARACTER_ATTRIBUTES[2]) * self.level |
| 30 | + self.current_hp = self.max_hp |
| 31 | + print(self.get_attrs_str()) |
| 32 | + self.picker("armor", armors) |
| 33 | + self.picker("weapon", weapons) |
| 34 | + |
| 35 | + def get_attribute_mod(self, attribute): |
| 36 | + return math.floor((self.attributes[attribute] - 10) / 2) |
| 37 | + |
| 38 | + def get_attribute_mod_cost(self, attribute): |
| 39 | + a = self.attributes[attribute] |
| 40 | + if a < 14: |
| 41 | + return 1 |
| 42 | + elif a == 14: |
| 43 | + return 2 |
| 44 | + elif a > 14: |
| 45 | + return "MAX" |
| 46 | + |
| 47 | + def reset_attributes(self): |
| 48 | + self.attributes = dict() |
| 49 | + for a in CHARACTER_ATTRIBUTES: |
| 50 | + self.attributes[a] = 8 |
| 51 | + |
| 52 | + def pick_attributes(self, points): |
| 53 | + numbers = range(1, len(CHARACTER_ATTRIBUTES) + 1) |
| 54 | + error = "" |
| 55 | + |
| 56 | + while points > 0: |
| 57 | + print(" Attribute\tValue\tModifier\tCost") |
| 58 | + for n, a in zip(numbers, CHARACTER_ATTRIBUTES): |
| 59 | + print(f"{n}: {a}\t{self.attributes[a]}\t{self.get_attribute_mod(a)}\t{self.get_attribute_mod_cost(a)}") |
| 60 | + print() |
| 61 | + print(f"Attribute Points: {points}") |
| 62 | + print(error) |
| 63 | + choice = int(input("Choose an attribute to increase: ")) |
| 64 | + print() |
| 65 | + if choice in numbers: |
| 66 | + choice -= 1 |
| 67 | + a = CHARACTER_ATTRIBUTES[choice] |
| 68 | + cost = self.get_attribute_mod_cost(a) |
| 69 | + if cost == "MAX": |
| 70 | + error = f"{CHARACTER_ATTRIBUTES[choice]} is at maximum" |
| 71 | + elif cost <= points: |
| 72 | + self.attributes[a] += 1 |
| 73 | + points -= cost |
| 74 | + error = "" |
| 75 | + else: |
| 76 | + error = f"Not enough points to increase {a}" |
| 77 | + else: |
| 78 | + error = f"Please pick a number between {min(numbers)} and {max(numbers)}" |
| 79 | + |
| 80 | + def get_attack_bonus(self): |
| 81 | + return self.get_attack_attr_bonus() + self.get_proficiency_bonus() |
| 82 | + |
| 83 | + def get_attack_attr_bonus(self): |
| 84 | + return max(self.get_attribute_mod["Strength"], |
| 85 | + self.get_attribute_mod["Dexterity"]) \ |
| 86 | + if self.weapon.finesse else self.get_attribute_mod("Strength") |
| 87 | + |
| 88 | + def get_proficiency_bonus(self): |
| 89 | + return math.floor(2 + ((self.level - 1) * 0.25)) |
| 90 | + |
| 91 | + def roll_weapon_damage(self): |
| 92 | + return roll(*self.weapon.damage) + self.get_attack_attr_bonus() |
| 93 | + |
| 94 | + |
| 95 | + # Report back character name, species, job, stats, and equipment. |
| 96 | + def __str__(self): |
| 97 | + return f"{self.name} is a level {self.level} {self.player_class.name}\n" \ |
| 98 | + f"{self.get_health_str()}\n" \ |
| 99 | + f"Armor: {self.armor.name} AC: {self.armor.armor_class} Weapon: {self.weapon.name}" \ |
| 100 | + f"{self.get_attrs_str()}" |
| 101 | + |
| 102 | + # Display character's stats. |
| 103 | + def get_attrs_str(self): |
| 104 | + return ''.join([f"{a}: {self.attributes[a]}\n" for a in CHARACTER_ATTRIBUTES]) |
| 105 | + |
| 106 | + def picker(self, pick_type, pick_list): |
| 107 | + choices = [g.name for g in pick_list] |
| 108 | + numbers = range(1, len(pick_list) + 1) |
| 109 | + |
| 110 | + for i, name in zip(numbers, choices): |
| 111 | + print(f"{i}: {name}") |
| 112 | + |
| 113 | + prompt = f"Which {pick_type} would you like? " |
| 114 | + |
| 115 | + while True: |
| 116 | + choice = int(input(prompt)) |
| 117 | + if choice in numbers: |
| 118 | + break |
| 119 | + else: |
| 120 | + prompt = f"Please pick a number between {min(numbers)} and {max(numbers)}: " |
| 121 | + |
| 122 | + if pick_type == "armor": |
| 123 | + self.armor = armors[choice - 1] |
| 124 | + print(f"You chose {self.armor.name}.\n") |
| 125 | + elif pick_type == "weapon": |
| 126 | + self.weapon = weapons[choice - 1] |
| 127 | + print(f"You chose {self.weapon.name}.\n") |
| 128 | + elif pick_type == "class": |
| 129 | + self.player_class= classes[choice - 1] |
| 130 | + print(f"You chose {self.player_class.name}.\n") |
| 131 | + |
| 132 | + |
| 133 | + def attack(self, enemy): |
| 134 | + attack_roll = roll(1, 20) + self.get_attack_bonus() |
| 135 | + print(f"{self.name} swings their {self.weapon.name} at the {enemy.name}!") |
| 136 | + if enemy.armor_class < attack_roll: |
| 137 | + print(f"{self.name} smacks the {enemy.name} a good one! {attack_roll} vs {enemy.get_ac()} AC") |
| 138 | + enemy.take_damage(self.roll_weapon_damage()) |
| 139 | + else: |
| 140 | + print(f"{self.name} wiffs!") |
| 141 | + |
| 142 | + def get_ac(self): |
| 143 | + return self.armor.ac + max(self.armor.max_dex_bonus, self.attributes["Dexterity"]) |
0 commit comments