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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
| from abc import ABC, abstractmethod
import random
class Character(ABC):
def __init__(self, name, health, attack_power):
self.name = name
self.max_health = health
self.health = health
self.attack_power = attack_power
self.level = 1
self.experience = 0
@abstractmethod
def special_attack(self, target):
pass
def attack(self, target):
damage = random.randint(self.attack_power - 5, self.attack_power + 5)
target.take_damage(damage)
return f"{self.name} attacks {target.name} for {damage} damage!"
def take_damage(self, damage):
self.health = max(0, self.health - damage)
if self.health == 0:
return f"{self.name} has been defeated!"
return f"{self.name} takes {damage} damage. Health: {self.health}/{self.max_health}"
def heal(self, amount):
old_health = self.health
self.health = min(self.max_health, self.health + amount)
healed = self.health - old_health
return f"{self.name} heals for {healed} HP. Health: {self.health}/{self.max_health}"
def gain_experience(self, exp):
self.experience += exp
if self.experience >= self.level * 100:
self.level_up()
def level_up(self):
self.level += 1
self.experience = 0
self.max_health += 20
self.health = self.max_health
self.attack_power += 5
return f"{self.name} levels up to level {self.level}!"
def is_alive(self):
return self.health > 0
def status(self):
return (f"{self.name} (Level {self.level}) - "
f"HP: {self.health}/{self.max_health}, "
f"Attack: {self.attack_power}, "
f"EXP: {self.experience}/{self.level * 100}")
class Warrior(Character):
def __init__(self, name):
super().__init__(name, health=120, attack_power=25)
self.armor = 10
def special_attack(self, target):
damage = self.attack_power * 2
target.take_damage(damage)
return f"{self.name} uses Mighty Strike on {target.name} for {damage} damage!"
def take_damage(self, damage):
reduced_damage = max(1, damage - self.armor)
return super().take_damage(reduced_damage)
class Mage(Character):
def __init__(self, name):
super().__init__(name, health=80, attack_power=30)
self.mana = 50
self.max_mana = 50
def special_attack(self, target):
if self.mana >= 20:
self.mana -= 20
damage = self.attack_power * 1.5
target.take_damage(damage)
return f"{self.name} casts Fireball on {target.name} for {damage} damage! Mana: {self.mana}/{self.max_mana}"
else:
return f"{self.name} doesn't have enough mana for Fireball!"
def restore_mana(self, amount):
old_mana = self.mana
self.mana = min(self.max_mana, self.mana + amount)
restored = self.mana - old_mana
return f"{self.name} restores {restored} mana. Mana: {self.mana}/{self.max_mana}"
class Archer(Character):
def __init__(self, name):
super().__init__(name, health=100, attack_power=20)
self.arrows = 30
def special_attack(self, target):
if self.arrows >= 3:
self.arrows -= 3
hits = 3
total_damage = 0
result = f"{self.name} uses Triple Shot on {target.name}:\n"
for i in range(hits):
damage = random.randint(self.attack_power - 2, self.attack_power + 2)
target.take_damage(damage)
total_damage += damage
result += f" Arrow {i+1}: {damage} damage\n"
result += f"Total damage: {total_damage}. Arrows left: {self.arrows}"
return result
else:
return f"{self.name} doesn't have enough arrows for Triple Shot!"
class Game:
def __init__(self):
self.characters = []
def add_character(self, character):
self.characters.append(character)
def battle(self, char1, char2):
print(f"\n=== Battle: {char1.name} vs {char2.name} ===")
print(char1.status())
print(char2.status())
print()
round_num = 1
while char1.is_alive() and char2.is_alive():
print(f"Round {round_num}:")
# 캐릭터 1 공격
if random.choice([True, False]): # 50% 확률로 특수 공격
print(char1.special_attack(char2))
else:
print(char1.attack(char2))
if not char2.is_alive():
break
# 캐릭터 2 공격
if random.choice([True, False]): # 50% 확률로 특수 공격
print(char2.special_attack(char1))
else:
print(char2.attack(char1))
print()
round_num += 1
winner = char1 if char1.is_alive() else char2
print(f"{winner.name} wins the battle!")
winner.gain_experience(50)
print(winner.gain_experience.__doc__ and winner.level_up())
# 사용 예제
if __name__ == "__main__":
game = Game()
warrior = Warrior("Conan")
mage = Mage("Gandalf")
archer = Archer("Legolas")
game.add_character(warrior)
game.add_character(mage)
game.add_character(archer)
# 캐릭터 상태 출력
for char in game.characters:
print(char.status())
# 전투 시뮬레이션
game.battle(warrior, mage)
|