-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk_hangmonkey.py
164 lines (147 loc) · 4.88 KB
/
tk_hangmonkey.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
import PIL.Image
import PIL.ImageTk
import random
from quitter import Quitter
from tkinter import *
class Hangman(Frame):
"Creates an app that plays a game of hangman"
def __init__(self, parent=None):
Frame.__init__(self, parent)
# game variables
self.wordlist = ['monkey',
'zebra',
'giraffe',
'tiger',
'elephant',
'iguana',
'hippopotamus',
'rhinoceros',
'chimpanzee',
'orangutan',
'hyena',
'gorilla',
'leopard',
'bobcat',
'flamingo',
'alligator',
'crocodile',
'parrot']
self.pics = ['img/Hangman0.png',
'img/Hangman1.png',
'img/Hangman2.png',
'img/Hangman3.png',
'img/Hangman4.png',
'img/Hangman5.png',
'img/Hangman6.png']
self.secret_word = self.getWord()
self.missed_letters = ''
self.correct_letters = ''
self.count = 0
# create game directions
directions = Label(self, text="Guess my secret word!")
directions.config(height=2, fg='white', bg='tomato')
directions.pack(side=TOP, fill=X)
# create space for the secret word
word_space = Label(self, height=5)
word_display = self.showWord(word_space)
word_space.pack(side=TOP, fill=X, pady=10)
# create a space for strike/ missed letter information
miss = Frame(self)
start_pic = PIL.Image.open(self.pics[0])
start_load = PIL.ImageTk.PhotoImage(start_pic)
strike_pics = Label(miss, image=start_load)
strike_pics.image = start_load
strike_pics.pack(side=LEFT)
miss_lets = Label(miss)
miss_lets.pack(side=RIGHT)
miss.pack(side=TOP)
# create an entry for your guess
guess_space = Frame(self)
guess_space.config(bg='lime green')
var = StringVar()
guess_dir = Label(guess_space, text="Pick a letter:")
guess_dir.config(bg='lime green')
guess_dir.pack(side=LEFT, padx=10, pady=10)
guess_ent = Entry(guess_space, textvariable=var)
guess_ent.config(bg='gold')
guess_ent.pack(side=RIGHT, padx=10, pady=10)
guess_space.pack(side=TOP, pady=5)
# create a button to enter guess
guess_btn = Button(self, text='GUESS')
guess_btn.config(command=(lambda: self.turn(var.get(), word_space, directions, strike_pics, miss_lets)))
guess_btn.pack(side=TOP, fill=X, padx=10)
# create buttons for play again and quit
stop_space = Frame(self)
new_game = Button(stop_space, text='New Game')
new_game.config(command=(lambda: self.new_game(word_space, directions, strike_pics, miss_lets, start_load)))
new_game.pack(side=LEFT, padx=10)
quit = Quitter(stop_space).pack(side=RIGHT)
stop_space.pack(side=TOP, fill=X)
def getWord(self):
"Chooses a secret word to begin the game"
choice = random.randint(0, (len(self.wordlist)-1))
secret_word = self.wordlist[choice]
return secret_word
def showWord(self, tag):
"Displays the secret word with hidden/guessed letters revealed"
blanks = '_' * len(self.secret_word)
for i in range(len(self.secret_word)):
if self.secret_word[i] in self.correct_letters:
blanks = blanks[:i] + self.secret_word[i] + blanks[i+1:]
tag.config(text=blanks)
def counter(self):
"Keeps track of the number of strikes"
self.count += 1
return self.count
def new_game(self, ws_tag, dir_tag, sp_tag, ml_tag, start_load):
"Starts a new game of hangman"
self.secret_word = self.getWord()
self.count = 0
self.correct_letters = ''
self.missed_letters = ''
word_display = self.showWord(ws_tag)
ws_tag.config(text=word_display)
dir_tag.config(text="Guess my secret word!")
sp_tag.config(image=start_load)
sp_tag.image = start_load
ml_tag.config(text='')
def turn(self, letter, ws_tag, dir_tag, sp_tag, ml_tag):
"Gameloop to reveal the word before getting 6 strikes"
if letter in self.secret_word:
# add letter to correct_letters
self.correct_letters = self.correct_letters + letter
# change the secret_word display
new_word = self.showWord(ws_tag)
# if all of the letters in correct_letters are in secret_word:
foundAll = True
for i in range(len(self.secret_word)):
if self.secret_word[i] not in self.correct_letters:
foundAll = False
# initiate win message
if foundAll:
dir_tag.config(text=" You win! Monkey lives!")
dir_tag.config(bg='orange')
else:
# increase the strike count by one
total = self.counter()
# if strike count == 6:
if total == 6:
# initiate loss message
dir_tag.config(text="My word was {}. You lose. Monkey dies.".format(self.secret_word.upper()))
dir_tag.config(bg='dodger blue')
else:
self.missed_letters = self.missed_letters + letter
# change the display of strike count
pic = self.pics[total]
load = PIL.Image.open(pic)
render = PIL.ImageTk.PhotoImage(load)
sp_tag.config(image=render)
sp_tag.image = render
# changes the display of missed letters
ml_tag.config(text=self.missed_letters)
if __name__ == '__main__':
root = Tk()
root.title('Hangmonkey')
hm = Hangman(root)
hm.pack(side=TOP)
root.mainloop()