-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
313 lines (283 loc) · 8.18 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
import random
import sys
import time
from words import collection
#pyfiglet setup
from colorama import init
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint
from pyfiglet import figlet_format
# randomly select a word from the collection
full_word = random.choice(collection)
# get the length of the selected word
full_word_length = len(full_word)
# this prints the list view of the words letters separated
single_letter_word = list(dict.fromkeys(full_word))
single_letter_word_length = len(single_letter_word)
# game info store
wrong = []
correct = []
attempted = []
correct_count = 0
lives = 10
playing = True
term = 0
def drawing():
if lives == 9:
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 8:
print(" ------------------")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 7:
print("-------------------")
print("| |")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 6:
print("-------------------")
print("| |")
print("| (_)")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 5:
print("-------------------")
print("| |")
print("| (_)")
print("| |")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 4:
print("-------------------")
print("| |")
print("| (_)")
print("| \|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 3:
print("-------------------")
print("| |")
print("| (_)")
print("| \|/")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 2:
print("-------------------")
print("| |")
print("| (_)")
print("| \|/")
print("| |")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
elif lives == 1:
print("-------------------")
print("| |")
print("| (_)")
print("| \|/")
print("| |")
print("| / ")
print("|")
print("|")
print("|")
print("|")
print("|")
def display_stats():
word_list = [letter if letter in correct else '_' for letter in full_word]
print('Current word: ', ' '.join(word_list))
time.sleep(0.5)
print("Incorrect letters:", ", ".join(wrong))
time.sleep(0.75)
if lives < 10:
print(" ")
drawing()
print(" ")
time.sleep(0.5)
def winner():
global playing
time.sleep(1)
print(" ")
print(" ")
# displays "You Win!" graphic
cprint(figlet_format('You Win!', font='standard'))
time.sleep(1)
print("Your person survived!")
print(" ")
time.sleep(5)
# ends the game loop
playing = False
def game_over():
global playing, term
time.sleep(1)
if term == 1:
print("-------------------")
print("| |")
print("| ( )")
print("| \|/")
print("| |")
print("| / \ ")
print("|")
print("|")
print("|")
print("|")
print("|")
cprint(figlet_format('Game Over', font='standard'))
time.sleep(0.65)
print("The word was:", full_word)
time.sleep(1)
print(" ")
print("Reload the game to try again")
print(" ")
time.sleep(5)
playing = False
def get_lives():
global term
global lives, playing
if lives == 1:
term = 1
game_over()
else:
lives = lives-1
time.sleep(0.65)
print(" ")
print("INCORRECT")
print(" ")
time.sleep(1)
display_stats()
# ------------------------------------------- START OF THE GAME ------------------------------------------- #
# this displays the title
cprint(figlet_format('Hangman', font='standard'))
# this displays the version number
print("Version 2.0")
print("")
time.sleep(1)
# asks user if they would like to play
play = input("Would you like to play? (y/n): ")
if play == "y":
time.sleep(1)
print(" ")
# ----------- game intro
print("Welcome to Hangman! Try and guess the word before your time runs out!")
time.sleep(2)
print("To end the game at anytime, type 'quit'")
time.sleep(2)
print(" ")
word_list = ['_' for letter in full_word]
print('Current word: ', ' '.join(word_list))
print(" ")
time.sleep(1.5)
# while the user has enough lives this loop runs
while playing == True:
print(" ")
time.sleep(1)
# asks the user to guess a letter
guess = input("Enter your guess: ")
# checks the length of the guess
guesslen = len(guess)
# if the guess has already been attempted then the user will be informed and it will be skipped
if guess in attempted:
time.sleep(0.65)
print("ERROR: You've already guessed this! Try again")
else:
# if the users wants to quit the game then this runs
if guess == "quit":
game_over()
# if the guess is more than one letter long then it will be considered as a word and this will run
elif guesslen > 1:
# if the word guessed is correct then this runs
if guess == full_word:
winner()
# if the word guessed is incorrect then this runs
else:
get_lives()
# if the guess is one letter long and correct then this rusn
elif guess in single_letter_word:
correct.append(guess)
attempted.append(guess)
time.sleep(0.45)
print(" ")
print("CORRECT")
time.sleep(1)
print(" ")
display_stats()
correct_count += 1
# if the user has guessed all the letters correctly then this runs
if correct_count == single_letter_word_length:
time.sleep(1)
print(" ")
print(" ")
print(" ")
print(" ")
cprint(figlet_format('You Win!', font='standard'))
time.sleep(1)
print("Your person survived!")
time.sleep(5)
playing = False
# if the guess is one letter long and incorrect then this runs
elif guess is not single_letter_word:
wrong.append(guess)
attempted.append(guess)
get_lives()
# if the user does not want to play then this runs
elif play == "n":
time.sleep(0.5)
print("Closing Game...")
time.sleep(2)
cprint(figlet_format('Goodbye', font='standard'))
# if the users choice is not valid then this runs
else:
print(" ")
time.sleep(1)
print("ERROR: Input not recognised")
time.sleep(1)
print("The game will now terminate. Reboot the game to try again")
time.sleep(2)