-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
430 lines (337 loc) · 12.2 KB
/
main.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
My Pokemon game.
"""
from random import randint, choice
"""
A Player will be of type user or computer.
You could have a user play against the computer or
two players depending on which you instatiate.
You could also have two computer players fight.
"""
class Player():
def __init__(self, name):
self.name = name
self.pokemon = []
self.choose_3_pokemon()
self.choose_attacker()
def __str__(self):
return self.name
def take_a_turn_against(self, other_player):
print() # skip a line to make it legible
self.your_move(other_player)
if not other_player.game_over():
other_player.take_a_turn_against(self)
else:
show_pokemon()
print(str(self) + " wins the game.")
def make_a_move(self, move, other_player):
if move == 'a':
print(str(self) + " chooses to attack.")
self.attack(other_player)
elif move == 'h':
print(str(self) + " chooses to heal " + self.attacker_name() + ".")
self.heal()
elif move == 's':
print(str(self) + " chooses to switch Pokemon.")
self.switch()
elif move == 'p':
# showing current Pokemon status. This doesn't cost a turn.
show_pokemon()
self.your_move(other_player)
else:
# recursively ask for a different move.
print("I don't know that one.")
self.your_move(other_player)
def game_over(self):
return (self.pokemon[0].dead() and self.pokemon[1].dead()
and self.pokemon[2].dead())
# Three different commands a player can choose.
# attack and heal are redirected to the Pokemon.
# If attacking Pokemon has been knocked out force a switch.
# A forced switch stills costs a turn.
def switch(self):
self.choose_attacker()
print(str(self) + " chooses " + self.attacker_name())
def attack(self, other_player):
if self.attacker.dead():
print("Sorry " + str(self) +
", your Pokemon was knocked out. You must switch.")
self.switch()
else:
type = self.choose_attack()
self.attacker.attack(type, other_player)
def heal(self):
if self.attacker.dead():
print("Sorry " + str(self) +
", your Pokemon was knocked out. You must switch.")
self.switch()
else:
self.attacker.heal(self, 20)
# redirect damage taken to the active Pokemon
def fire_damage(self, damage):
self.attacker.fire_damage(damage)
def grass_damage(self, damage):
self.attacker.grass_damage(damage)
def water_damage(self, damage):
self.attacker.water_damage(damage)
# active Pokemon name, just the name.
def attacker_name(self):
return self.attacker.attacker_name()
"""
User type players will always ask for choices and commands.
"""
class User(Player):
def your_move(self, other_player):
print("Player " + self.name + " your move.")
move = input("Attack (a), heal (h), switch (s) or Pokemon status (p)?")
self.make_a_move(move, other_player)
def choose_3_pokemon(self):
for i in range(3):
monster = choose_pokemon(pocket_monsters)
self.pokemon.append(monster)
pocket_monsters.remove(monster)
def choose_attacker(self):
print("Who will attack?")
self.attacker = choose_pokemon(self.pokemon)
def choose_attack(self):
print("Choose your attack!")
return choose_attack(self.attacker.attack_types())
"""
Computer players will just randomly take actions.
They might not be intelligent actions, just actions.
"""
class Computer(Player):
def your_move(self, other_player):
print("Player " + self.name + " next move.")
self.make_a_move(choice(['a', 'a', 'a', 'a', 'h', 's']), other_player)
def choose_3_pokemon(self):
for i in range(3):
monster = choice(pocket_monsters)
self.pokemon.append(monster)
pocket_monsters.remove(monster)
def choose_attacker(self):
self.attacker = choice(self.pokemon)
if self.attacker.health <= 0:
self.choose_attacker()
def choose_attack(self):
return choice(self.attacker.attack_types())
"""
Pokemon!!! The Pokemon class does almost everything.
Subclasses are of types grass, fire, or water.
Each type has different abilities.
"""
class Pokemon():
def __init__(self, health, attack_power, name):
self.health = health
self.max_health = health
self.attack_power = attack_power
self.name = name
self.init_attacks()
def __str__(self):
return (self.name + " (" + self.type + ") with health " +
str(self.health) + " and attack " + str(self.attack_power) +
".")
# just the name no stats
def attacker_name(self):
return self.name
def attack_types(self):
return self.attacks
def dead(self):
return self.health == 0
# Pokemon will only heal or attack. Players must switch them.
def heal(self, player, healing):
self.health = min(self.health + healing, self.max_health)
print(str(player) + " heals " + self.attacker_name() + ".")
def attack(self, attack, other_player):
print(self.attacker_name() + " attacking " +
other_player.attacker_name() + " with " + attack.name + ".")
damage = attack.calculate_damage(self.attack_power)
self.do_damage_to(other_player, damage)
# Rock, paper, scissors style of extra damage. Each Pokemon type is
# stronger against one other type.
def normal_damage(self, damage):
self.health = max(self.health - damage, 0)
print(str(damage) + " damage done to " + self.attacker_name())
def extra_damage(self, amount):
damage = round(amount * 1.5)
self.health = max(self.health - damage, 0)
print(
str(damage) + " including extra damage done to " +
self.attacker_name())
# default is normal damage for all types of attackers
def grass_damage(self, amount):
self.normal_damage(amount)
def fire_damage(self, amount):
self.normal_damage(amount)
def water_damage(self, amount):
self.normal_damage(amount)
""" Grass type Pokemon has leaf attacks and is weak to fire damage."""
class GrassType(Pokemon):
type = 'grass'
def init_attacks(self):
self.attacks = [
Attack("Leaf Storm", 150, 33),
Attack("Mega Drain", 50, 100),
Attack("Razor Leaf", 100, 50)
]
# Grass Pokemon always do grass damage
def do_damage_to(self, other_player, amount):
other_player.grass_damage(amount)
# Override default to do extra damage if fire attacks me
def fire_damage(self, amount):
self.extra_damage(amount)
"""Water type Pokemon has water attacks and is weak to fire damage."""
class WaterType(Pokemon):
type = 'water'
def init_attacks(self):
self.attacks = [
Attack("Surf", 100, 50),
Attack("Bubble", 50, 100),
Attack("Hydro Pump", 200, 25)
]
# Water Pokemon always do water damage
def do_damage_to(self, other_player, amount):
other_player.water_damage(amount)
# Override default to do extra damage if grass attacks me
def grass_damage(self, amount):
self.extra_damage(amount)
"""Fire type Pokemon has fire attacks and is weak to water damage."""
class FireType(Pokemon):
type = 'fire'
def init_attacks(self):
self.attacks = [
Attack("Ember", 50, 100),
Attack("Fire Punch", 70, 70),
Attack("Flame Wheel", 100, 50)
]
# Fire Pokemon always do fire damage
def do_damage_to(self, other_player, amount):
other_player.fire_damage(amount)
# Override default to do extra damage if water attacks me
def water_damage(self, amount):
self.extra_damage(amount)
"""
We are going to make objects to represent attacks.
Damage calculations and hit/miss are done here.
"""
class Attack():
"""Attacks do the damage calculation."""
def __init__(self, name, power, accuracy):
self.name = name
self.power_percent = power
self.accuracy = accuracy
def __str__(self):
return (self.name + " with " + str(self.power_percent) +
"% of attack power and " + str(self.accuracy) +
"% chance to hit.")
def calculate_damage(self, attack_power):
if randint(1, 100) <= self.accuracy:
max_damage = round(attack_power * self.power_percent / 100)
return randint(1, max_damage)
else:
# Ha Ha missed me
return 0
"""
User interface functions.
Choose a pokemon. Choose an attack.
"""
def choose_pokemon(pokemon):
"""Choose one of several Pokemon."""
index = 1
print("Enter the number for your pokemon choice.")
for monster in pokemon:
print(str(index) + ". " + str(monster))
index = index + 1
index = int(input("Choose? "))
if index < 1 or index > len(pokemon):
print("Not a valid number.")
return choose_pokemon(pokemon)
elif pokemon[index - 1].dead():
print("That Pokemon is already knocked out, choose another.")
return choose_pokemon(pokemon)
else:
return pokemon[index - 1]
def choose_attack(attacks):
"""Choose one of three attacks."""
index = 1
print("Enter the number for your attack choice.")
for attack in attacks:
print(str(index) + ". " + str(attack))
index = index + 1
index = int(input("Choose? "))
if index < 1 or index > len(attacks):
print("Not a valid number.")
return choose_attack(attacks)
else:
return attacks[index - 1]
# a fourth command is to show the current status
def show_pokemon():
show_title()
print(str(user) + " attacking with " + user.attacker_name())
print("Pokemon are:")
for monster in user.pokemon:
print(" " + str(monster))
print()
print(str(computer) + " attacking with " + computer.attacker_name())
print("Pokemon are:")
for monster in computer.pokemon:
print(" " + str(monster))
print()
def show_pikachu():
"""Some ASCII art."""
print("""
`;-. ___,
`.`\_...._/`.-"`
\ / ,
/() () \ .' `-._
|) . ()\ / _.'
\ -'- ,; '. <
;.__ ,;| > \\
/ , / , |.-'.-'
(_/ (_/ ,;|.<`
\ , ;-`
> \ /
(_,-'`> .'
(_,'
""")
def show_title():
print("""
,'\\
_.----. ____ ,' _\ ___ ___ ____
_,-' `. | | /`. \,-' | \ / | | \ |`.
\ __ \ '-. | / `. ___ | \/ | '-. \ | |
\. \ \ | __ | |/ ,','_ `. | | __ | \| |
\ \/ /,' _`.| ,' / / / / | ,' _`.| | |
\ ,-'/ / \ ,' | \/ / ,`.| / / \ | |
\ \ | \_/ | `-. \ `' /| | || \_/ | |\ |
\ \ \ / `-.`.___,-' | |\ /| \ / | | |
\ \ `.__,'| |`-._ `| |__| \/ | `.__,'| | | |
\_.-' |__| `-._ | '-.| '-.| | |
`' '-._|
""")
"""
Call start_game to initialize and get the players taking turns.
Right now we are set up for one user and one computer player.
You can change that if you want.
"""
def start_game():
"""Start the Pokemon game."""
global pocket_monsters, user, computer
show_pikachu()
pocket_monsters = [
GrassType(60, 40, 'Bulbasaur'),
GrassType(40, 60, 'Bellsprout'),
GrassType(50, 50, 'Oddish'),
FireType(30, 70, 'Charmainder'),
FireType(50, 50, 'Ninetails'),
FireType(40, 60, 'Ponyta'),
WaterType(80, 20, 'Squirtle'),
WaterType(70, 30, 'Psyduck'),
WaterType(50, 50, 'Poliwag')
]
#user = Computer(input("Your name? "))
user = Computer("Pikachu")
computer = Computer("Lysandre")
user.take_a_turn_against(computer)
# Get this game started.
start_game()