-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
221 lines (200 loc) · 5.5 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
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
import random
import os
from asciimatics.effects import Cycle, Stars
from asciimatics.renderers import FigletText
from asciimatics.scene import Scene
from asciimatics.screen import Screen
def sieunhangao():
def get_hangman_stage(mang):
max_attempts = 7
stages = [
"""
------
|
|
|
|
|
|
------------
""", """
------
| |
|
|
|
|
|
------------
""", """
------
| |
| O
|
|
|
|
------------
""", """
------
| |
| O
| |
| |
|
|
------------
""", """
------
| |
| O
| --|
| |
|
|
------------
""", """
------
| |
| O
| --|--
| |
|
|
------------
""", """
------
| |
| O
| --|--
| |
| /
|
------------
""", """
------
| |
| O
| --|--
| |
| / \\
|
------------
"""
]
return stages[max_attempts - mang]
os.system('cls' if os.name == 'nt' else 'clear')
danh_sach_tu = [
"banana", "apple", "orange", "grapefruit", "strawberry", "watermelon",
"mango", "pineapple", "kiwi", "pear", "peach", "cherry", "blueberry",
"raspberry", "plum", "lemon", "lime", "coconut", "pomegranate", "avocado",
"fig", "grapes", "apricot", "blackberry", "cranberry", "guava", "papaya",
"passionfruit", "dragonfruit", "melon"
]
random_tu = random.choice(danh_sach_tu)
def banner():
print("""
_ _ _ _ _____ __ __ _ _
| | | | /\ | \ | |/ ____| \/ | /\ | \ | |
| |__| | / \ | \| | | __| \ / | / \ | \| |
| __ | / /\ \ | . ` | | |_ | |\/| | / /\ \ | . ` |
| | | |/ ____ \| |\ | |__| | | | |/ ____ \| |\ |
|_| |_/_/ \_\_| \_|\_____|_| |_/_/ \_\_| \_|
""")
print(" # made with Python")
return ""
def batdau():
os.system('cls' if os.name == 'nt' else 'clear')
print(banner())
print("Welcome to Hangman! Let's see if you can guess this word!\n")
sokytu = len(random_tu)
mang = 7
space2 = []
for i in range(sokytu):
space2.append('_')
print("You have", mang, "incorrect guesses left.", "\n")
print("Your word is...")
print(' '.join(space2))
print(get_hangman_stage(7))
while ('_' in space2) and (mang > 0):
# print("see if there is an error: ", random_tu)
chon = input("\nGuess a letter: ")
chon = chon.lower()
os.system('cls' if os.name == 'nt' else 'clear')
for i, kytu in enumerate(random_tu):
if chon == kytu:
space2[i] = kytu
print(banner())
print("Welcome to Hangman! Let's see if you can guess this word!\n")
print(' '.join(space2))
if chon.islower():
if len(chon) == 1:
if chon in random_tu:
print("\nYes! The letter", chon, "is part of the secret word.")
else:
mang -= 1
print("\nNo! The letter", chon, "is not part of the secret word.")
else:
print("\nPlease input 1 letter.")
else:
print("\nPlease input 1 letter.")
print("\nYou have", mang, "incorrect guesses left.")
print(get_hangman_stage(mang))
if mang == 0:
print("You lost!")
print("The secret word: ", random_tu)
print(end())
else:
print("You won!")
print(end())
return ""
def goodbye():
def didit(screen):
screen.clear()
screen.print_at("Goodbye:D!", int(screen.width / 2 - 3),
int(screen.height / 2))
screen.refresh()
screen.wait_for_input(1)
Screen.wrapper(didit)
return""
def end():
def didit(screen):
screen.clear()
screen.print_at("Goodbye:D!", int(screen.width / 2 - 3),
int(screen.height / 2))
screen.refresh()
screen.wait_for_input(1)
print("\nPlay again?\n")
while True:
user_input = (input("(y/N): "))
user_input = (user_input).lower()
if user_input == "y":
print(sieunhangao())
if user_input == "n":
Screen.wrapper(didit)
print("Goodbye:D!\n")
quit()
else:
print("Please re-enter! y(Yes) or N (No).")
print(batdau())
def doit(screen):
effects = [
Cycle(screen, FigletText("Welcome to", font='big'),
int(screen.height / 2 - 8)),
Cycle(screen, FigletText("HANGMAN!", font='big'),
int(screen.height / 2 + 3)),
Stars(screen, 200)
]
screen.play([
Scene(effects, int(screen.width / 2)),
Scene(effects,
int(screen.width / 2) + 1)
],
repeat=False)
screen.clear()
screen.print_at("Made by Dam Duc Huy", int(screen.width / 2 - 3),
int(screen.height / 2))
screen.refresh()
screen.wait_for_input(1)
Screen.wrapper(doit)
print(sieunhangao())