-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (107 loc) · 4.2 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
# Created by Agamdeep Singh / CodeWithAgam
# Youtube: CodeWithAgam
# Github: CodeWithAgam
# Instagram: @coderagam001 / @codewithagam
# Twitter: @CoderAgam001
# Linkdin: Agamdeep Singh
# Random Moduel to get the words randomly
import random
# The Python files for the ASCII arts, the Hangman Logo and the words
import assets.hangman_arts as hangman_arts
from assets.hangman_words import *
# Welcome message for the user
def Welcome():
print(hangman_arts.logo)
Rules()
def Rules():
# Give the player a choice to get to know How To Play
choice = input("\nDo you want to know How To Play? Type (yes/no): ").lower()
# Conditions to skip/toggle How To Play
if choice == 'yes':
print('''
*How TO PLAY:*\n
- Hangman is game where you've a Word and you have to guess it letter by letter.
- You have 6 Lives to guess the word.
- When you make a wrong guess, you lose 1 life.
- If you lose all lives, you Lose.
- If you guess the word before losing all 6 lives, you Win.
''')
# Give the user a choice to start the game
start = input("Do you want to start? (yes/no): ").lower()
if start == 'yes':
Main()
else:
print("Thanks for using Hangman!")
else:
Main()
# The main code of the game
def Main():
categories_list = animals, languages, programming_langs
print('''
Categories of words available:
1. Animals
2. Languages
3. Programming Languages & Frameworks
''')
categoary = int(input("Choose your Category: "))
# Select a random word from the categoary selected from the categories list
if categoary == 1:
chosen_word = random.choice(categories_list[0])
elif categoary == 2:
chosen_word = random.choice(categories_list[1])
elif categoary == 3:
chosen_word = random.choice(categories_list[2])
# Get the length of the random word
word_length = len(chosen_word)
# Set the no. of lives to 6 and the end of game to False
lives = 6
end_of_game = False
# Create an empty list of blanks as display and add 1 blank to the for each letter in the chosen word
display = []
for _ in range(word_length):
display += "_"
# Give the user a hint
hint = random.choice(chosen_word).upper()
print("\nI have a word, can you guess it?")
print(f"\nHint is: {hint}")
# This while loops through the guess input unit the game gets over, either by winning or losing
while not end_of_game:
# Ask the user to guess a letter
guess = input("Guess a letter: ").lower()
# Check if the word is already gussed by the user
if guess in display:
print(f"You guessed {guess}, but you've already guessed it.")
# Check if the word guess by the user is correct
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
# Check if the word guessed by the user is wrong
if guess not in chosen_word:
print(f"You guessed {guess}, that's not in the word. You Lose 1 Life.")
lives -= 1
hint_needed = input("Do you want a Hint? Type (yes/no): ")
if hint_needed == 'yes':
hint = random.choice(chosen_word).upper()
print(f"\nHint is: {hint}")
else:
print("Let's Continue!")
if lives == 0:
end_of_game = True
print("You lose.")
print(f"The word was: {chosen_word}")
# Join all the elements in the list and turn it into a String
print(f"{' '.join(display)}")
# Check if user has got all letters.
if "_" not in display:
end_of_game = True
print("You win.")
run_again = input("\nDo you want to play again? (yes/no): ")
while True:
if run_again == "yes":
Main()
else:
print("Thanks for playing!")
# Print the current stage corresponding to the no. of lives left and wrong words guessed
print(hangman_arts.stages[lives])
Welcome()