-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman_gui.py
executable file
·424 lines (385 loc) · 17.7 KB
/
hangman_gui.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#-------------------------------------------------------#
# #
# hangman.py #
# --------------------------------------------- #
# #
# Loads a random word from a list of allowed #
# words, specified in an input file, and starts #
# the game for you! #
# #
# Author: Tom Catullo ([email protected]) #
# #
# For simplicity, the graphics.py library #
# by John Zelle was used to generate the #
# GUI for this program. #
# #
#-------------------------------------------------------#
# Import our required libraries and functions
from random import randint
from graphics import *
import os
from random import randint
# getInfile()
#
# Gets a text file containing words to be chosen by the Hangman program
def getInfile():
filename="/Users/alyshareinard/Dropbox/python/hangman/wordlist.txt"
filename2="~\Dropbox\python\hangman\wordlist.txt"
current_dir=os.getcwd()
infile_name=current_dir+"\wordlist.txt"
try:
# First, we check for a file called "wordlist.txt". If it exists in the same directory
# as the Hangman program, then we use this file as our word list automatically
with open(infile_name, 'r'): infile_name = 'wordlist.txt'
except IOError:
infile_name = raw_input('Please specify a text file containing a list of words for the Hangman game to choose from (include the full file path if the file is in a different directory than the Hangman program): ')
found_file = False
while found_file == False:
print(infile_name)
print(filename2)
print(os.getcwd())
# If the user specifies a file name of a file that cannot be found, we keep asking for
# a valid input file until a valid one is specified
while not(found_file):
try:
with open(infile_name, 'r'): found_file = True
except IOError:
infile_name = input('\n{0} was not found!\n\nPlease try again, or specify a different file (include the full file path if the file is in a different directory than the Hangman program): '.format(infile_name))
return infile_name
# chooseWord()
#
# Chooses a word randomly from the list of words taken from the input file
def chooseWord(infile_name):
infile = open(infile_name, 'r')
wordlist = infile.readlines()
total_words = len(wordlist)
random_num = randint(0, total_words - 1)
chosen_word = wordlist[random_num].replace('\n', '')
word_len = len(chosen_word)
return chosen_word, word_len
# drawPiece()
#
# Draws a piece of the Hangman picture when an incorrect letter is guessed. The piece that
# is drawn depends on the number of strikes that the player has amounted thus far
def drawPiece(strike, win, win_width, win_height, win_hangmanpic):
hangman_yaxis = win_width / 4 # The body of the hangman will align with this axis
if strike == 1:
# Strike 1: Draw the post
line1 = Line(Point(win_width -5* win_width / 8, 100), Point(win_width - 5*win_width / 8, 300))
line1.draw(win)
win_hangmanpic.append(line1)
line2 = Line(Point(win_width - 5*win_width / 8, 300), Point(hangman_yaxis, 300))
line2.draw(win)
win_hangmanpic.append(line2)
line3 = Line(Point(hangman_yaxis, 300), Point(hangman_yaxis, 270))
line3.draw(win)
win_hangmanpic.append(line3)
elif strike == 2:
# Strike 2: Draw the head
circle4 = Circle(Point(hangman_yaxis, 254), 16)
circle4.draw(win)
win_hangmanpic.append(circle4)
elif strike == 3:
# Strike 3: Draw the right eye
circle5=Circle(Point(hangman_yaxis-4, 258), 2)
circle5.draw(win)
win_hangmanpic.append(circle5)
elif strike == 4:
#Strike 4: Draw the left eye
circle6=Circle(Point(hangman_yaxis+4, 258), 2)
circle6.draw(win)
win_hangmanpic.append(circle6)
elif strike == 5:
#Strike 5: Draw the nose
circle7=Circle(Point(hangman_yaxis, 251), 1)
circle7.draw(win)
win_hangmanpic.append(circle7)
elif strike == 6:
#Strike 4: Draw the mouth
line45=Line(Point(hangman_yaxis-7, 247), Point(hangman_yaxis+7, 247))
line45.draw(win)
win_hangmanpic.append(line45)
elif strike == 7:
# Strike 3: Draw the torso
line5 = Line(Point(hangman_yaxis, 238), Point(hangman_yaxis, 180))
line5.draw(win)
win_hangmanpic.append(line5)
elif strike == 8:
# Strike 4: Draw the left arm
line6 = Line(Point(hangman_yaxis, 225), Point(hangman_yaxis - 20, 200))
line6.draw(win)
win_hangmanpic.append(line6)
elif strike == 9:
# Strike 5: Draw the right arm
line7 = Line(Point(hangman_yaxis, 225), Point(hangman_yaxis + 20, 200))
line7.draw(win)
win_hangmanpic.append(line7)
elif strike == 10:
# Strike 6: Draw the left leg
line8 = Line(Point(hangman_yaxis, 180), Point(hangman_yaxis - 15, 135))
line8.draw(win)
win_hangmanpic.append(line8)
elif strike == 11:
# Strike 7: Draw the right leg
line9 = Line(Point(hangman_yaxis, 180), Point(hangman_yaxis + 15, 135))
line9.draw(win)
win_hangmanpic.append(line9)
# GAME OVER: Draw a face on our hangman guy
# Draw his "X"-eyes
line10 = Line(Point(hangman_yaxis + 7, 260), Point(hangman_yaxis + 2, 255))
line10.draw(win)
win_hangmanpic.append(line10)
line11 = Line(Point(hangman_yaxis + 2, 260), Point(hangman_yaxis + 7, 255))
line11.draw(win)
win_hangmanpic.append(line11)
line12 = Line(Point(hangman_yaxis - 7, 260), Point(hangman_yaxis - 2, 255))
line12.draw(win)
win_hangmanpic.append(line12)
line13 = Line(Point(hangman_yaxis - 2, 260), Point(hangman_yaxis - 7, 255))
line13.draw(win)
win_hangmanpic.append(line13)
# Draw his mouth
line14 = Line(Point(hangman_yaxis - 7, 247), Point(hangman_yaxis + 7, 247))
line14.draw(win)
win_hangmanpic.append(line14)
# main()
#
# Carries out the main functionality of the program
def main(win, infile_name):
# If a word list file hasn't yet been sepcified, then we need to get one
if not(infile_name):
infile_name = getInfile()
# Choose a word at random from the acquired word list
word, word_len = chooseWord(infile_name)
# Build the grid of empty spaces, one space for each letter of the chosen word
grid = '__'
for i in range(word_len - 1):
grid = grid + ' __'
# Generate the graphics window for the game's GUI
win_width = 1000
win_height = 500
if not(win):
# This will only be called on the first play-through of the game, so that
# a new window isn't generated each time a player chooses to "play again"
win = GraphWin("Hangman", win_width, win_height)
win.setCoords(0, 0, win_width, win_height)
# Draw the game message area, the ground of the Hangman graphic, and the empty grid
win_message = Text(Point(win_width / 3, 70), "Let's play! Guess a letter below to see if it's in the word.")
win_message.setStyle('bold')
win_message.draw(win)
win_ground = Line(Point(win_width / 10, 100), Point(win_width - win_width / 2, 100))
win_ground.draw(win)
win_grid = Text(Point(win_width / 3, win_height - 70), grid)
win_grid.setSize(16)
win_grid.draw(win)
# Draw the input box and accompanying text label
win_guesstxt = Text(Point(win_width / 3 - 35, 30), "Type a letter:")
win_guesstxt.draw(win)
win_guessinput = Entry(Point(win_width / 3 + 30, 28), 4)
win_guessinput.draw(win)
win_guessinput.setText('')
#Draw the letters
letters=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
win_letterinfo=Text(Point(3*win_width/4, 450), "Remaining letters")
win_letterstxt=Text(Point(3*win_width/4, 420), letters)
win_letterstxt.setSize(16)
win_letterinfo.draw(win)
win_letterstxt.draw(win)
# Draw the "Guess it!" button and its label
win_butguess = Rectangle(Point(win_width / 3 + 55, 43), Point(win_width / 3 + 130, 13))
win_butguess.setFill(color_rgb(126, 236, 53))
win_butguess.setOutline(color_rgb(0, 110, 0))
win_butguess.draw(win)
win_butguess_label = Text(Point(win_width / 3 + 94, 28), 'Guess it!')
win_butguess_label.setTextColor(color_rgb(0, 110, 0))
win_butguess_label.draw(win)
# Now, we keep taking guesses of letters until either the player amounts 7 total strikes (wrong
# guesses), or until the player correctly guesses the entire word and the game is won
strikes = 0
guessed_letters = []
win_hangmanpic = []
game_won = False
while strikes < 11 and game_won == False:
# Log the player's mouse click location
## p = win.getMouse()
# If the player clicked outside of the area of the "Guess it!" button, then we go back to the start
# of the loop and wait for another click
# if p.getX() < win_width / 3 + 55 or p.getX() > win_width / 3 + 130 or p.getY() < 13 or p.getY() > 43:
# print("p.getx ", p.getX(), "p.gety ", p.getY())
## if p.getX() > win_width or p.getX() < 0 or p.getY()<0 or p.getY() > 1000:
## continue
# Grab the guessed letter, and if it's invalid, go back to the top of the loop and wait for another guess
guess = win_guessinput.getText().lower()
if len(guess) > 0:
guess=guess[0]
win_guessinput.setText('')
if guess == ' ' or guess == '' or len(guess) != 1:
if len(guess) != 1:
win_message.setText("Please only guess a single letter at a time. Try again!")
continue
# If the guessed letter is in the word, and it hasn't been guessed yet by the player, then we update our
# grid by placing this letter in the appropriate empty spaces on the grid
if guess in word.lower() and not(guess in guessed_letters):
guessed_letters.append(guess)
grid = word
letters.remove(guess.upper())
win_letterstxt.undraw()
win_letterstxt=Text(Point(3*win_width/4, 420), letters)
win_letterstxt.setSize(16)
win_letterstxt.draw(win)
for letter in word:
if (not (letter.lower() in word) or not(letter.lower() in guessed_letters)) and letter != ' ':
grid = grid.replace(letter, ' __ ')
win_grid.setText(grid)
if grid == word:
game_won = True
else:
win_message.setText("Nice! {0} is in the word. Try another letter.".format(guess.upper()))
elif guess in guessed_letters:
# This letter has already been guessed, so alert the player about this and wait for another guess
win_message.setText("You've already guessed {0}. Try another letter.".format(guess.upper()))
elif guess.upper() not in letters:
win_message.setText("That's not a letter!")
else:
# Wrong guess! Add a strike and draw a new piece of the Hangman picture
guessed_letters.append(guess)
letters.remove(guess.upper())
win_letterstxt.undraw()
win_letterstxt=Text(Point(3*win_width/4, 420), letters)
win_letterstxt.setSize(16)
win_letterstxt.draw(win)
strikes = strikes + 1
if strikes>8:
#randomly remove 5 letters
k=0
while len(letters)>len(word) and k<5:
#print("len", len(letters))
val = randint(0, len(letters)-1)
#print("letter", letters[val])
if letters[val].lower() not in word:
guessed_letters.append(letters[val].lower())
#print("guessed", guessed_letters)
letters.remove(letters[val])
k=k+1
win_letterstxt.undraw()
win_letterstxt=Text(Point(3*win_width/4, 420), letters)
win_letterstxt.setSize(16)
win_letterstxt.draw(win)
win_message.setText('{0} is a wrong guess! Try another letter.'.format(guess.upper()))
drawPiece(strikes, win, win_width, win_height, win_hangmanpic)
# The game is over, so let's remove some of the objects at the bottom of the window
# to make room for the "Quit" and "Play Again?" buttons and the final message
win_guesstxt.undraw()
win_guessinput.undraw()
win_butguess.undraw()
win_butguess_label.undraw()
win_message.move(0, -10) # Move the message area down a bit to fill up some space
win_letterstxt.undraw()
win_letterinfo.undraw()
# Update the grid to display the full, actual word that the program chose
win_grid.setText(word.upper())
win_grid.setStyle('bold')
win_grid.setSize(16)
# Change the message at the bottom of the window to reflect a win or loss
if game_won == True:
win_message.setText("Congrats! You've guessed the word correctly.")
win_message.setTextColor(color_rgb(0, 120, 0))
win_grid.setTextColor(color_rgb(0, 120, 0))
# Remove the incomplete Hangman picture
for hm_obj in win_hangmanpic:
hm_obj.undraw()
win_hangmanpic = []
# And replace it with a picture that's representative of winning the game
circle1 = Circle(Point(win_width / 3, 240), 110)
circle1.setOutline(color_rgb(0, 120, 0))
circle1.setFill(color_rgb(126, 236, 53))
circle1.draw(win)
win_hangmanpic.append(circle1)
eye1 = Circle(Point(win_width / 3 - 40, 285), 20)
eye1.setOutline(color_rgb(0, 120, 0))
eye1.draw(win)
win_hangmanpic.append(eye1)
eye2 = eye1.clone()
eye2.move(80, 0)
eye2.draw(win)
win_hangmanpic.append(eye2)
eye1_inner = Circle(Point(win_width / 3 - 40, 285), 5)
eye1_inner.setFill(color_rgb(0, 120, 0))
eye1_inner.setOutline(color_rgb(0, 120, 0))
eye1_inner.draw(win)
win_hangmanpic.append(eye1_inner)
eye2_inner = eye1_inner.clone()
eye2_inner.move(80, 0)
eye2_inner.draw(win)
win_hangmanpic.append(eye2_inner)
mouth = Circle(Point(win_width / 3, 200), 50)
mouth.setFill(color_rgb(0, 120, 0))
mouth.setOutline(color_rgb(0, 120, 0))
mouth.draw(win)
win_hangmanpic.append(mouth)
mouth_cover = Rectangle(Point(win_width / 3 - 50, 200), Point(win_width / 3 + 50, 250))
mouth_cover.setFill(color_rgb(126, 236, 53))
mouth_cover.setOutline(color_rgb(126, 236, 53))
mouth_cover.draw(win)
win_hangmanpic.append(mouth_cover)
else:
win_message.setText("Sorry! You didn't completely guess the word.")
win_message.setTextColor(color_rgb(170, 0, 0))
win_grid.setTextColor(color_rgb(170, 0, 0))
# And make the lines drawn in the Hangman picture red
i = 0
for hm_obj in win_hangmanpic:
if i == 3:
# This is the guy's head, which is a circle, so it requires a different function
# to change the circle's color
hm_obj.setOutline(color_rgb(170, 0, 0))
elif i == 4 or i == 5:
hm_obj.setOutline(color_rgb(255, 255, 255))
else:
hm_obj.setFill(color_rgb(170, 0, 0))
# Increment the index counter
i = i + 1
# Draw a button to ask if the player wants to play again
win_butagain = Rectangle(Point(win_width - 90, 30), Point(win_width, 0))
win_butagain.setFill(color_rgb(126, 236, 53))
win_butagain.setOutline(color_rgb(0, 110, 0))
win_butagain.draw(win)
win_butagain_label = Text(Point(win_width - 45, 15), 'Play again?')
win_butagain_label.setTextColor(color_rgb(0, 110, 0))
win_butagain_label.draw(win)
# As well as a button for if the player want to quit the game
win_butquit = Rectangle(Point(4, 30), Point(54, 0))
win_butquit.setFill(color_rgb(255, 177, 177))
win_butquit.setOutline(color_rgb(170, 0, 0))
win_butquit.draw(win)
win_butquit_label = Text(Point(29, 15), 'Quit')
win_butquit_label.setTextColor(color_rgb(170, 0, 0))
win_butquit_label.draw(win)
# Wait for an option to be selected by the player, then either quit the game or
# play another round, based on what button the player clicks
opt_selected = False
while not(opt_selected):
p = win.getMouse()
# if p.getX() >= (win_width - 90) and p.getY() <= 30:
if p.getX() >= (win_width/2):
# Play again
opt_selected = 1
elif p.getX() <= 54 and p.getY() <= 30:
# Exit game
opt_selected = 2
# Do something based on the option that was selected
if opt_selected == 1:
# Undraw all graphics objects from this game and call another instance of the game
graphic_objs = [win_message, win_ground, win_grid, win_butagain, win_butagain_label, win_butquit, win_butquit_label, win_hangmanpic]
for obj in graphic_objs:
if obj == win_hangmanpic:
for hm_obj in win_hangmanpic:
hm_obj.undraw()
else:
obj.undraw()
main(win, infile_name)
else:
# Close the game window
win.close()
# Start the first instance of the Hangman game
main(False, False)