-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcharacter.py
105 lines (81 loc) · 3.12 KB
/
character.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Standard library imports
import msvcrt
# Local folder imports
from health_bar import HealthBar
from tile import player_marker
from weapon import fists, claws, jaws, short_bow
INSTANT_INPUT = False
# ------------ parent class setup ------------
class Character:
def __init__(self,
name: str,
health: int,
) -> None:
self.name = name
self.health = health
self.health_max = health
self.weapon = fists
def attack(self, target) -> None:
if self.health <= 0:
return
target.health -= self.weapon.damage
target.health = max(target.health, 0)
target.health_bar.update()
print(f"{self.name} dealt {self.weapon.damage} damage to "
f"{target.name} with {self.weapon.name}")
def __copy__(self):
new_instance = self.__class__.__new__(self.__class__)
new_instance.__dict__.update(self.__dict__)
# Add any additional attributes that need to be copied
return new_instance
# ------------ subclass setup ------------
class Player(Character):
def __init__(self, name: str = "Player", health: int = 100) -> None:
super().__init__(name=name, health=health)
self.default_weapon = self.weapon
self.health_bar = HealthBar(self, color="green")
self.pos = [0, 0]
self.marker = player_marker
self.movement_options: dict[str, bool]
def equip(self, weapon) -> None:
self.weapon = weapon
print(f"{self.name} equipped a(n) {self.weapon.name}!")
def drop(self) -> None:
print(f"{self.name} dropped the {self.weapon.name}!")
self.weapon = self.default_weapon
def move(self, x: int, y: int) -> None:
self.pos[0] += x
self.pos[1] += y
def calculate_movement_options(self, width, height) -> None:
self.movement_options = {
"up": self.pos[1] > 0, # can go up
"down": self.pos[1] < height - 1, # can go down
"left": self.pos[0] > 0, # can go left
"right": self.pos[0] < width - 1 # can go right
}
def get_movement_input(self) -> None:
choice = msvcrt.getch().decode('utf-8') if INSTANT_INPUT else input()
if self.movement_options["up"] and choice in ("w", "W"):
self.pos[1] -= 1
elif self.movement_options["down"] and choice in ("s", "S"):
self.pos[1] += 1
elif self.movement_options["left"] and choice in ("a", "A"):
self.pos[0] -= 1
elif self.movement_options["right"] and choice in ("d", "D"):
self.pos[0] += 1
# ------------ subclass setup ------------
class Enemy(Character):
def __init__(self,
name: str,
health: int,
weapon=None,
) -> None:
super().__init__(name=name, health=health)
self.weapon = weapon
self.health_bar = HealthBar(self, color="red")
enemies.append(self)
enemies = []
slime = Enemy("Slime", 10, jaws)
goblin = Enemy("Goblin", 20, short_bow)
spider = Enemy("Spider", 15, jaws)
rat = Enemy("Rat", 6, claws)