generated from Code-Institute-Org/p3-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
356 lines (319 loc) · 16.6 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
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
import random
import time
from time import sleep
# variables that stores words used for the different lists
words_easy = ['boo', 'toad', 'luna', 'bow',]
words_medium = ['luigi', 'kamek', 'peach', 'yoshi', 'mario', 'bowser',
'goomba', 'lakitu',]
words_hard = ['rosalina', 'polterpup', 'wiggler', 'waluigi',
'blooper',]
# variables that randomizes a word from the different lists
random_word_easy = random.choice(words_easy)
random_word_medium = random.choice(words_medium)
random_word_hard = random.choice(words_hard)
# variables that will store the correct and wrong guesses
correct_guess = ['_'] * len(random_word_easy or random_word_medium
or random_word_hard)
wrong_guess = []
# function that adds waiting time
def waiting_time():
for i in range(5):
print('.', end="")
sleep(.5)
print()
# function that prints out the different parts of the hang man
# when the user guesses a wrong letter
def hangman_parts(x):
if x == 0:
print(' ', '------')
print(' ', '| |')
print(' ', '| ')
print(' ', '| ')
print(' ', '| ')
print(' ', '| ')
print('--------------')
if x == 1:
print(' ', '------')
print(' ', '| |')
print(' ', '| O')
print(' ', '| ')
print(' ', '| ')
print(' ', '| ')
print('--------------')
if x == 2:
print(' ', '------')
print(' ', '| |')
print(' ', '| O')
print(' ', '| -|')
print(' ', '| ')
print(' ', '| ')
print('--------------')
if x == 3:
print(' ', '------')
print(' ', '| |')
print(' ', '| O')
print(' ', '| -|-')
print(' ', '| ')
print(' ', '| ')
print('--------------')
if x == 4:
print(' ', '------')
print(' ', '| |')
print(' ', '| O')
print(' ', '| -|-')
print(' ', '| |')
print(' ', '| ')
print('--------------')
if x == 5:
print(' ', '------')
print(' ', '| |')
print(' ', '| O')
print(' ', '| -|-')
print(' ', '| |')
print(' ', '| / ')
print('--------------')
if x == 6:
print(' ', '------')
print(' ', '| |')
print(' ', '| O')
print(' ', '| -|-')
print(' ', '| |')
print(' ', '| / \\')
print('---------------')
# function to show how many tries the user got
# left before the game is over
def lifes(y):
if y == 0:
print('♥ ♥ ♥ ♥ ♥ ♥')
if y == 1:
print('♥ ♥ ♥ ♥ ♥ ♡')
if y == 2:
print('♥ ♥ ♥ ♥ ♡ ♡')
if y == 3:
print('♥ ♥ ♥ ♡ ♡ ♡')
if y == 4:
print('♥ ♥ ♡ ♡ ♡ ♡')
if y == 5:
print('♥ ♡ ♡ ♡ ♡ ♡')
if y == 6:
print('♡ ♡ ♡ ♡ ♡ ♡')
# print out word by word
def print_word_by_word(text, delay=0.1):
words = text.split()
for word in words:
print(word, end=' ', flush=True)
sleep(delay)
print()
# print out letter by letter
def print_letter_by_letter(text, delay=0.1):
for char in text:
print(char, end='', flush=True)
sleep(delay)
print()
# print out letter by letter for inputs
def input_letter_by_letter(prompt):
for char in prompt:
print(char, end='', flush=True)
time.sleep(0.1) # Adjust the delay as needed
user_input = input()
return user_input
# prints out the initial strings when the game starts
while True:
print('')
print_letter_by_letter('Hi and welcome to')
print_letter_by_letter('')
print_word_by_word('██╗░░██╗░█████╗░███╗░░██╗░██████╗'
'░███╗░░░███╗░█████╗░███╗░░██╗')
print_word_by_word('██║░░██║██╔══██╗████╗░██║██╔════╝'
'░████╗░████║██╔══██╗████╗░██║')
print_word_by_word('███████║███████║██╔██╗██║██║░░██╗'
'░██╔████╔██║███████║██╔██╗██║')
print_word_by_word('██╔══██║██╔══██║██║╚████║██║░░╚██╗'
'██║╚██╔╝██║██╔══██║██║╚████║')
print_word_by_word('██║░░██║██║░░██║██║░╚███║╚██████╔╝'
'██║░╚═╝░██║██║░░██║██║░╚███║')
print_word_by_word('╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝░╚═════╝░'
'╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝')
print_word_by_word('')
print_word_by_word('░██████╗██╗░░░██╗██████╗░███████╗██████╗░'
'███╗░░░███╗░█████╗░██████╗░██╗░█████╗░')
print_word_by_word('██╔════╝██║░░░██║██╔══██╗██╔════╝██╔══██╗'
'████╗░████║██╔══██╗██╔══██╗██║██╔══██╗')
print_word_by_word('╚█████╗░██║░░░██║██████╔╝█████╗░░██████╔╝'
'██╔████╔██║███████║██████╔╝██║██║░░██║')
print_word_by_word('░╚═══██╗██║░░░██║██╔═══╝░██╔══╝░░██╔══██╗'
'██║╚██╔╝██║██╔══██║██╔══██╗██║██║░░██║')
print_word_by_word('██████╔╝╚██████╔╝██║░░░░░███████╗██║░░██║'
'██║░╚═╝░██║██║░░██║██║░░██║██║╚█████╔╝')
print_word_by_word('╚═════╝░░╚═════╝░╚═╝░░░░░╚══════╝╚═╝░░╚═╝'
'╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░╚════╝░')
print_word_by_word('')
print_word_by_word('███████╗██████╗░██╗████████╗██╗░█████╗░███╗░░██╗')
print_word_by_word('██╔════╝██╔══██╗██║╚══██╔══╝██║██╔══██╗████╗░██║')
print_word_by_word('█████╗░░██║░░██║██║░░░██║░░░██║██║░░██║██╔██╗██║')
print_word_by_word('██╔══╝░░██║░░██║██║░░░██║░░░██║██║░░██║██║╚████║')
print_word_by_word('███████╗██████╔╝██║░░░██║░░░██║╚█████╔╝██║░╚███║')
print_word_by_word('╚══════╝╚═════╝░╚═╝░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝')
print_word_by_word('')
print_letter_by_letter('The goal of the game is to guess the correct '
'word ')
print_letter_by_letter('the computer randomizes for you before '
'the whole picture ')
print_letter_by_letter('of the hangman is printed'
' and you lose the game.')
print_word_by_word('')
print_letter_by_letter('The category is different characters from the '
'Super Mario games.')
print_letter_by_letter('')
print_letter_by_letter('You got 3 different difficulties to choose from:')
print_letter_by_letter('Easy, medium and hard.')
print_letter_by_letter('')
print_letter_by_letter('The easy difficulty got up til 4 letters.')
print_letter_by_letter('The medium difficulty got up til 6 letters.')
print_letter_by_letter('And the hard difficulty got up til 9 letters.')
print_letter_by_letter('')
print_letter_by_letter('HOW TO PLAY:')
print_letter_by_letter('To guess a letter or choose a input')
print_letter_by_letter('just write the letter or word directly in the '
'terminal')
print_letter_by_letter('and press ENTER')
print_word_by_word('')
print_letter_by_letter('You got 6 tries to guess the correct word.')
print_letter_by_letter('Good luck!')
# functions that shows how many letters the choosen word have
def update_correct_letters(correct_guess):
for letter in correct_guess:
print(letter, end=' ')
print()
# function that starts the hangman game
def play_game(random_word):
# prints out how many letters the choosen word has
print_letter_by_letter('The word has ' + str(len(random_word)) +
' letters')
print('')
# Initialize variables at the beginning
wrong_guess = []
correct_guess = ['_'] * len(random_word)
doRunGame = True
while doRunGame:
# prints the string below between the guessed letters
print("=================================")
# variable for the guess the user will choose
print('')
guess = input_letter_by_letter("Guess a letter: \n")
# Check if the guessed input is more than one letter
if len(guess) == 1:
# checks if the guessed letter is on the choosen word
# and puts it in the correct guess index if true
if guess in correct_guess:
print_letter_by_letter('You already guessed that')
elif guess in random_word:
# prints out the lifes
print('')
lifes(len(wrong_guess))
print('')
index = 0
for i in random_word:
if i == guess:
correct_guess[index] = guess
index += 1
hangman_parts(len(wrong_guess))
update_correct_letters(correct_guess)
# checks if the guessed letter already is guessed or not
# either prints out more of the hangman if the guess is wrong
# print out that the user has used that
# letter already if the letter is already used
else:
if guess in wrong_guess:
print_letter_by_letter('You already guessed that')
elif guess not in wrong_guess:
wrong_guess.append(guess)
print('')
lifes(len(wrong_guess))
print('')
hangman_parts(len(wrong_guess))
update_correct_letters(correct_guess)
# prints out if the user guessed a wrong letter
print('')
print_letter_by_letter(f'Wrong letter {wrong_guess}')
else:
print_letter_by_letter('Please guess only one letter at a '
'time.')
# checks if the wrong guesses is more then 5
# then prints the losing statement
if len(wrong_guess) > 5:
print_letter_by_letter(f'The correct word was {random_word}')
print('')
print_word_by_word('█▄█ █▀█ █░█ █░░ █▀█ █▀ █▀▀ █')
print_word_by_word('░█░ █▄█ █▄█ █▄▄ █▄█ ▄█ ██▄ ▄')
print('')
print_word_by_word('█▀▀ ▄▀█ █▀▄▀█ █▀▀ █▀█ █░█ █▀▀ █▀█ ░')
print_word_by_word('█▄█ █▀█ █░▀░█ ██▄ █▄█ ▀▄▀ ██▄ █▀▄ ▄')
print('')
while True:
play_again = input_letter_by_letter("Do you want to play "
"again? (yes/no):\n")
if play_again == 'yes':
doRunGame = False
break # Start a new game with a new difficulty
elif play_again == 'no':
print_letter_by_letter("Thanks for playing. Goodbye!")
exit()
else:
print_letter_by_letter("Invalid answer.")
if '_' not in correct_guess:
print('')
print_word_by_word('█▄█ █▀█ █░█ █░█░█ █▀█ █▄░█ █')
print_word_by_word('░█░ █▄█ █▄█ ▀▄▀▄▀ █▄█ █░▀█ ▄')
print('')
print_word_by_word('█▀▀ █▀█ █▄░█ █▀▀ █▀█ ▄▀█ ▀█▀ █░█'
'█░░ ▄▀█ ▀█▀ █ █▀█ █▄░█ █▀')
print_word_by_word('█▄▄ █▄█ █░▀█ █▄█ █▀▄ █▀█ ░█░ █▄█'
'█▄▄ █▀█ ░█░ █ █▄█ █░▀█ ▄█')
print('')
while True:
play_again = input_letter_by_letter("Do you want to play "
"again? (yes/no): \n")
if play_again == 'yes':
doRunGame = False
break # Start a new game with a new difficulty
elif play_again == 'no':
print_letter_by_letter("Thanks for playing. Goodbye!")
exit()
else:
print_letter_by_letter("Invalid answer.")
# main game loop
# Check if the entered difficulty is valid
while True:
difficulty = input_letter_by_letter("Please choose a degree of "
"difficulty ('easy',"
"'medium', or 'hard'): \n")
if difficulty == 'easy':
# variable that randomizes a word from the easy list
random_word_easy = random.choice(words_easy)
# variable that adds the randomized word to the random_word var
random_word = random_word_easy
print('')
print_letter_by_letter(f'Let me think of a {difficulty} word...')
waiting_time()
play_game(random_word)
elif difficulty == 'medium':
# variable that randomizes a word from the medium list
random_word_medium = random.choice(words_medium)
# variable that adds the randomized word to the random_word var
random_word = random_word_medium
print('')
print_letter_by_letter(f'Let me think of a {difficulty} word...')
waiting_time()
play_game(random_word)
elif difficulty == 'hard':
# variable that randomizes a word from the hard list
random_word_hard = random.choice(words_hard)
# variable that adds the randomized word to the random_word var
random_word = random_word_hard
print('')
print_letter_by_letter(f'Let me think of a {difficulty} word...')
waiting_time()
play_game(random_word)
else:
print_letter_by_letter("Invalid difficulty.")