-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbattle_part10.py
More file actions
215 lines (154 loc) · 5.19 KB
/
battle_part10.py
File metadata and controls
215 lines (154 loc) · 5.19 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PyPet Battle Game:
[ ] Two fighters are randomly chosen from a list of PETS, each starting with a
health of 100
[ ] Print out details about the chosen fighters
[ ] Each fighter takes a turn attacking the other until one fighter wins.
- Each attack will have a description and do randomly selected amount of
damage between 10-30
- Each attack will print out the description of the attack, the damage it
did, and the health of each fighter at the end of the turn
- Whoever reaches 0 first loses and the other player wins.
[ ] At the end of the game, announce the winner
"""
# The convention is to name modules (Python files) using
# lower_case_with_underscore
#
# The code for a project should be in a directory named using lowernounderscore
# for example:
#
# myproject/
# my_module.py
# my_script.py
# ### Imports ################################################################
import random
import time
from pets import PICS, PETS
# ## Global Variables ########################################################
# the range of damage each player can do
#
# this is a data type called a tuple
# it is just like a list, except it is
# immutable, meaning it cannot be changed
POWER = (10, 30)
# the number of seconds to pause for dramatic effect
DELAY = 1
# the max width of the screen
WIDTH = 56
MAX_HEALTH = 100
# a list of attacks
FIGHTIN_WORDS = (
"nips at",
"takes a swipe at",
"glares sternly at",
"ferociously smacks",
"savagely boofs",
"is awfully mean to",
"can't even believe",
"throws mad shade at",
)
# ## Functions ###############################################################
# ### pet functions ###
#
def setup(pets):
"""Takes a list of pets and sets initial attributes"""
for pet in pets:
pet['health'] = MAX_HEALTH
pet['pic'] = PICS[pet['species']]
def show(pet):
"""Takes a pet and prints health and details about them"""
name_display = f"{pet['name']} {pet['pic']}"
health_display = f"{pet['health']} of {MAX_HEALTH}"
rcol_width = WIDTH - len(name_display) - 1
print(name_display, health_display.rjust(rcol_width))
# ### game event functions ###
#
def attack(foe):
"""Inflict a random amount of damage is inflicted on foe, then return the
damage and attack used"""
# choose an attack
act = random.choice(FIGHTIN_WORDS)
# randomly set damage
damage = random.randint(POWER[0], POWER[1])
# inflict damage
foe['health'] -= damage
# return the amount of damage attack and description
return damage, act
# ### top-level game functions ###
#
def lotto():
"""Return two randomly chosen PETs"""
# randomly reorder the PETS list
random.shuffle(PETS)
# return the first two items in the PETS list
return [PETS[0], PETS[1]]
def intro(fighters):
"""Takes a list of two PETs (fighters) and prints their details"""
print("\n Tonight...\n")
time.sleep(DELAY)
# announce the fighters
header = f"*** {fighters[0]['name']} -vs- {fighters[1]['name']} ***"
print(header.center(WIDTH, " "), "\n\n")
# pause for input
input("ARE YOU READY TO RUMBLE?!")
print("." * WIDTH, "\n")
def fight(fighters):
"""Repeat rounds of the fight until one wins then
Take a list of two PETs and return the winning PET"""
# winning fighter
winner = None
# the index in the fighters list of the attacker in each round
current = 0
# ### rounds of the fight
#
while winner is None:
# pick whose turn it is
attacker = fighters[current]
rival = fighters[not current]
# pause for input
input(f"\n{attacker['name']} FIGHT>")
# the attack
damage, act = attack(rival)
# pause for effect, then print attack details
time.sleep(DELAY)
print(f"\n {attacker['name']} {act} {rival['name']}...\n")
# pause for effect, then print damage
time.sleep(DELAY)
print(f"-{damage} {rival['name']}".center(WIDTH), "\n")
# one more pause before the round ends
time.sleep(DELAY)
# check for a loser (placeholder)
winner = random.choice(fighters)
# print updated fighter health
print()
for combatant in fighters:
show(combatant)
# print a line at the end of every round
print("-" * WIDTH, "\n")
# flip current to the other fighter for the next round
current = not current
#
# ### end of fighting rounds
# return the winner
return winner
def endgame(winner):
"""Takes a PET (winner) and announce that they won the fight"""
# The main() function should be at the last function defined
#
def main():
"""PyPet Battle Game"""
print("\nWelcome to the THUNDERDOME!")
fighters = lotto()
setup(fighters)
intro(fighters)
winner = fight(fighters)
endgame(winner)
# ## Runner ##################################################################
# This calls the main() function if the script is being run directly
# but not if it is being imported as a module
# This should always be at the very end of the script
#
if __name__ == "__main__":
main()