-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.py
80 lines (71 loc) · 1.99 KB
/
Hangman.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
# importing required modules
import random
from asciihangman import hangman
import os
import platform
import time
from logo import logo
from wrong import wrong
from correct import correct
from Final import LOST, WON
# For Clearing The Console
def clear_console():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def main():
"""This is main Function for The Hangman Code I Wrote"""
# hangman_logo
print(logo)
time.sleep(1)
clear_console()
# Required Variables
lives = 6
gameOver = False
hint1 = 0
hint2 = 0
# Response for Getting Word List from Online
word_list = []
file = open("wordlist.txt", "r")
word = file.read()
file.close()
word_list = word.split("\n")
# Other Variables
word_chosen = random.choice(word_list)
word_length = len(word_chosen)
hint1 = random.randint(0, word_length - 1)
hint1 = random.randint(0, word_length - 1)
display = ["_" for i in range(len(word_chosen))]
# Adding two hints
display[hint1] = word_chosen[hint1]
display[hint2] = word_chosen[hint2]
# Main Game loop:
while gameOver == False:
print(hangman[lives], end=" ")
print(f"\n====Remaining Lives: {lives}=========\n")
print(display)
guess = input("Guess The Letter:").lower()
for position in range(word_length):
if word_chosen[position] == guess:
clear_console()
display[position] = guess
print(correct)
time.sleep(0.5)
clear_console()
if guess not in word_chosen:
lives -= 1
clear_console()
print(wrong)
time.sleep(1)
clear_console()
if "_" not in display or lives == 0:
gameOver = True
if lives == 0:
print(LOST)
print(f"The word was {word_chosen}")
else:
print(WON)
print(display)
if __name__ == "__main__":
main()