generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
309 lines (284 loc) · 9.62 KB
/
run.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
import random
class Color:
"""
Sets colors to be called for different texts.
"""
RESET = '\033[0m'
BLUE = '\u001b[34m'
GREEN = '\033[92m'
RED = '\033[91m'
YELLOW = '\u001b[33m'
def get_new_word():
"""
Selects a random word from the words.txt file opened in read format
at the beginning of the game.
Ensures a new random word evry time the game is played.
"""
with open('words.txt', 'r') as word_list:
words = word_list.readlines()
any_word = random.choice(words)[:-1].upper()
return any_word
def introduction_page():
"""
The initial introduction to the hangman game.
The player can read the instuctions or begin the game.
"""
word = get_new_word()
hangman_sign()
print(hangman_construction(0))
print(f"Type {Color.BLUE}1 {Color.RESET} to start the game\n")
print(f"Type {Color.BLUE}2 {Color.RESET} to read the instructions")
selection = False
while not selection:
decision = input("\n")
if decision == "1":
selection = True
begin_game(word)
elif decision == "2":
selection = True
game_instructions()
else:
print(f"\n{Color.YELLOW}Please type {Color.BLUE}1 {Color.YELLOW}or"
f"{Color.BLUE} 2 {Color.YELLOW}to make your decision."
f"{Color.RESET}")
def game_instructions():
"""
Gives the player the instructions to play the game.
"""
print(
"""
The object of the game is to guess the word.
Do this by entering in one letter each go.
If you enter a wrong letter a life will be taken from you.
If you lose all your lives, say goodbye to the hanging individual.
Guess the word before your lives hit 0 and you are a hero.
"""
)
begin = input("Press the enter key to begin the game.\n")
introduction_page()
def begin_game(word):
"""
Starts the game for the user
checks if the letter the player inputs is in the hidden word.
if the letter is correct,game will iterate through hidden word
and letter will go into correct position.
if incorrect,a message will be relayed to the user.
player can see how many lives are left to complete the game.
"""
correct_word = "_" * len(word)
completed = False
guessed_letters = []
lives = 6
print("Help Me!!!\n")
print(f"Lives left {lives}\n")
while completed is not True and lives > 0:
print(hangman_construction(lives))
print(correct_word)
guess = input('Give me some letters please \n')
if len(guess) == 1 and guess.isalpha(
) and guess not in guessed_letters:
if guess.upper() in word:
guessed_letters.append(guess)
word_list = list(correct_word)
indices = [i for i, letter in enumerate(
word) if letter == guess.upper()]
for index in indices:
word_list[index] = guess
correct_word = "".join(word_list)
print(f"\n{Color.GREEN}Great!! {Color.RESET}{guess}"
f" {Color.GREEN}is in the word! \n{Color.RESET}")
if '_' not in correct_word:
completed = True
else:
print(f"\n{Color.YELLOW}Oh no! {Color.RESET}{guess}"
f"{Color.YELLOW} isn't in the word!\n{Color.RESET}")
lives -= 1
print(f"Lives left: {lives} \n")
guessed_letters.append(guess)
print(
"Tried these already: " +
", ".join(guessed_letters) +
"\n")
elif len(guess) != 1:
print(f"\n{Color.YELLOW}Oops!, "
f"Your only allowed to guess {Color.RESET}1"
f" {Color.YELLOW}letter at a time.")
print(f"You used {Color.RESET}{len(guess)} "
f"{Color.YELLOW}characters.\n{Color.RESET}")
elif guess in guessed_letters:
print(f"\n{Color.YELLOW}You have already used me"
f" {Color.RESET}{guess}{Color.YELLOW}!{Color.RESET}")
lives -= 1
if completed:
win_sign()
print(f"{Color.GREEN}Well Done!"
"You are a hero!")
else:
lose_sign()
print("Looks like you arent a hero, "
"Do NOT quit your day job.")
print(f"The word was: {Color.RESET}{word}{Color.RED}.{Color.RESET}")
start_game_again()
def start_game_again():
"""
Asks if the user wants to start the game again.
If not, returns to main screen.
"""
start_again_option = input(f"\nWould you like to try again? "
f"{Color.BLUE}Y{Color.RESET}/{Color.BLUE}N\n"
f"{Color.RESET}").upper()
if start_again_option == "Y":
word = get_new_word()
begin_game(word)
elif start_again_option == "N":
exit()
else:
print(
f"{Color.YELLOW}Time to decide {Color.BLUE}Y {Color.YELLOW}"
f"or {Color.BLUE}N{Color.YELLOW}. You chose "
f"{Color.RESET}{start_again_option}{Color.YELLOW}.{Color.RESET}\n")
start_game_again()
def hangman_construction(lives):
"""
Shows the user how man lives are left before the games ends.
"""
stages = [f"""
=======
|/ |
| {Color.RED}@{Color.RESET}
| /|\\
| |
| / \\
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
""",
f"""
=======
|/ |
| {Color.RED}@{Color.RESET}
| /|\\
| |
| /
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
""",
f"""
=======
|/ |
| {Color.RED}@{Color.RESET}
| /|\\
| |
|
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
""",
f"""
=======
|/ |
| {Color.RED}@{Color.RESET}
| /|
| |
|
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
""",
f"""
=======
|/ |
| {Color.RED}@{Color.RESET}
| |
| |
|
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
""",
f"""
=======
|/ |
| {Color.RED}@{Color.RESET}
|
|
|
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
""",
"""
=======
|/ |
|
|
|
|
_____|_________
/ |\\ /|
______________ / /
| /
______________ /
"""
]
return stages[lives]
def hangman_sign():
"""
A title graphic to be displayed on the title screen.
"""
print(
"""
██╗ ██╗█████╗███╗ ██╗██████╗███╗ ███╗█████╗███╗ ██╗
██║ ████╔══██████╗ ████╔════╝████╗ ██████╔══██████╗ ██║
████████████████╔██╗ ████║ █████╔████╔███████████╔██╗ ██║
██╔══████╔══████║╚██╗████║ ████║╚██╔╝████╔══████║╚██╗██║
██║ ████║ ████║ ╚████╚██████╔██║ ╚═╝ ████║ ████║ ╚████║
╚═╝ ╚═╚═╝ ╚═╚═╝ ╚═══╝╚═════╝╚═╝ ╚═╚═╝ ╚═╚═╝ ╚═══
"""
)
def win_sign():
"""
Displays a win sign for when the player correctly guesses the word
to let them know they were correct.
"""
print(
f"""{Color.GREEN}
::: ::: ::::::::::: :::: :::
:+: :+: :+: :+:+: :+:
+:+ +:+ +:+ :+:+:+ +:+
+#+ +:+ +#+ +#+ +#+ +:+ +#+
+#+ +#+#+ +#+ +#+ +#+ +#+#+#
#+#+# #+#+# #+# #+# #+#+#
### ### ########### ### ####
{Color.RESET}"""
)
def lose_sign():
"""
Displays a lose sign for when the player fails to guess the word
to let them know the lost the game.
"""
print(
f"""{Color.RED}
:::::::::: ::: ::::::::::: :::
:+: :+: :+: :+: :+:
+:+ +:+ +:+ +:+ +:+
:#::+::# +#++:++#++: +#+ +#+
+#+ +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+#
### ### ### ########### ##########
{Color.RESET}"""
)
introduction_page()