-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
248 lines (193 loc) · 8.04 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
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
import random, sys, pygame
from pygame.locals import *
# List of possible words that could be selected
WORD_BANK = ["apple", "banana", "carrot", "eggplant", "watermelon", "lemon", "pumpkin", "peaches", "corn", "strawberry","cherry", "orange", "onion", "pineapple", "kiwi", "lettuce", "peas", "spinach", "plum" ]
# This chooses the random word
random.shuffle(WORD_BANK)
WORD = WORD_BANK[0]
pygame.init()
# VARIABLES
WIDTH = 600 # Width of game window
HEIGHT = 600 # Height of game window
PADDING = 15 # Variable that creates a margin in the game window, also
# used for drawing lines
# Colors used
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
LINE_WIDTH = 5 # Width of line for hanger
HEAD_RAD = int(HEIGHT / 25) # Radius of head
BODY_LINE_WIDTH = 3
# Assigns important colors
BACKGROUND_COLOR = WHITE
HANGER_COLOR = BLACK
WIN_COLOR = GREEN
INCORRECT_COLOR = RED
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))
# Hanger points
H_A = (PADDING + int(WIDTH/4),int(HEIGHT/2))
H_B = (H_A[0], int(HEIGHT/6))
H_C = (int(WIDTH/2), H_B[1])
H_D = (int(WIDTH/6), H_A[1])
H_E = (H_A[0] + (H_A[0] - H_D[0]), H_D[1])
H_F = (H_C[0], int(HEIGHT/25) + H_B[1])
# Body points
B_HEAD = (H_F[0], H_F[1] + HEAD_RAD)
B_BODY_1 = (H_F[0], H_F[1] + 2 * HEAD_RAD)
B_BODY_2 = (H_F[0], int(0.8 * H_A[1]))
B_R_ARM_1 = (H_F[0], int(0.6 * H_A[1]) ) # Body right arm point 1
B_R_ARM_2 = (int(H_F[0] * 1.1), int(0.7 * H_A[1])) # Body right arm point 2
B_L_ARM_1 = (H_F[0], int(0.6 * H_A[1]) )
B_L_ARM_2 = (int(H_F[0] * 0.9), int(0.7 * H_A[1]))
B_R_LEG_1 = (H_F[0],int(0.8 * H_A[1]))
B_R_LEG_2 = (int(H_F[0] * 1.1), int(0.9 * H_A[1]))
B_L_LEG_1 = (H_F[0],int(0.8 * H_A[1]))
B_L_LEG_2 = (int(H_F[0] * 0.9), int(0.9 * H_A[1]))
# Name: drawHanger()
# Purpose: Draws the hanger on the game window, in the color BLACK
# Parameters: None
# Returns: Nothing
def drawHanger():
pygame.draw.line(DISPLAY, HANGER_COLOR, H_A,H_B, LINE_WIDTH)
pygame.draw.line(DISPLAY, HANGER_COLOR, H_B,H_C, LINE_WIDTH)
pygame.draw.line(DISPLAY, HANGER_COLOR, H_C,H_F, LINE_WIDTH)
pygame.draw.line(DISPLAY, HANGER_COLOR, H_E,H_D, LINE_WIDTH)
# The following one line functions are used to draw individual body parts
# , so that we can draw the body parts in a sequential order, in the function
# drawBodyParts()
def drawHead():
pygame.draw.circle(DISPLAY, BLACK, B_HEAD, HEAD_RAD, BODY_LINE_WIDTH)
def drawBody():
pygame.draw.line(DISPLAY, BLACK, B_BODY_1, B_BODY_2, BODY_LINE_WIDTH)
def drawRightArm():
pygame.draw.line(DISPLAY, BLACK, B_R_ARM_1, B_R_ARM_2, BODY_LINE_WIDTH)
def drawLeftArm():
pygame.draw.line(DISPLAY, BLACK, B_L_ARM_1, B_L_ARM_2, BODY_LINE_WIDTH)
def drawRightLeg():
pygame.draw.line(DISPLAY, BLACK, B_R_LEG_1, B_R_LEG_2, BODY_LINE_WIDTH)
def drawLeftLeg():
pygame.draw.line(DISPLAY, BLACK, B_L_LEG_1, B_L_LEG_2, BODY_LINE_WIDTH)
PARTS = ['head', 'body', 'left arm', 'right arm', 'left leg', 'right leg']
# Name: drawBodyPart()
# Purpose: Draws body parts to DISPLAY in a sequential order, specified
# by the PARTS list
# Parameters: None
# Returns: Nothing
def drawBodyPart():
if PARTS[0] == 'head':
drawHead()
elif PARTS[0] == 'body':
drawBody()
elif PARTS[0] == 'left arm':
drawLeftArm()
elif PARTS[0] == 'right arm':
drawRightArm()
elif PARTS[0] == 'left leg':
drawLeftLeg()
elif PARTS[0] == 'right leg':
drawRightLeg()
del PARTS[0]
if len(PARTS) == 0:
pygame.display.update()
pygame.quit()
sys.exit()
# Name: drawLines(letters)
# Purpose: Draws a lines to DISPLAY corresponding to the number of letters in word
# In addition, returns a list of graphic coordinates specifying where to
# draw the letters
# Parameters: letters <string>
# Returns: <list> object of tuple points
def drawLines(letters):
global lineSize # Size of each line to draw, made global because it is also the
# font size of the letters to be drawn
numLines = len(letters)
lineSize = WIDTH / numLines
lineSize = lineSize - 2 * PADDING
x = PADDING # Since the only part of the coordinates for the lines drawn is
# the x part, we use x to specify the starting point of the lines
letterSpots = [] # list object that will hold the points of where to draw
# the letters
letterSpotY = int(HEIGHT * 0.75 - lineSize/2 - 4) # Y coordinate of letter objects
for letter in letters:
pygame.draw.line(DISPLAY, BLACK, (x, int(HEIGHT * 0.75)),\
(x + lineSize, int(HEIGHT*0.75)), BODY_LINE_WIDTH)
letterSpots.append((x + lineSize/2, letterSpotY))
x = x +lineSize + 2 * PADDING # update
return letterSpots
# Name: makeLetters()
# Purpose: Creates a list of letter objects, needed in order to actually make the
# correctly entered letters appear on the display
# Parameters: Nothing
# Returns: <list> object of letter objects
def makeLetters():
letterList = []
fontObj = pygame.font.Font('freesansbold.ttf', int(lineSize))
for letter in WORD:
textObj = fontObj.render(letter, True, BLACK, WHITE)
letterList.append(textObj)
return letterList
# Name: wrongLetter(letter)
# Purpose: Called whenever the user enters an incorrect letter, draws it in RED
# on the screen
# Parameters: letter <string>
# Returns: Nothing, modifies the window
def wrongLetter(letter):
wrongLetterX = 7 - len(PARTS)
fontObj = pygame.font.Font('freesansbold.ttf', 20)
wrongLetterObj = fontObj.render(letter, True, INCORRECT_COLOR, BACKGROUND_COLOR)
where = ((wrongLetterX * WIDTH/8), HEIGHT * 0.9)
wrongLetterRect = wrongLetterObj.get_rect()
wrongLetterRect.center = where
DISPLAY.blit(wrongLetterObj, wrongLetterRect)
# Name: won(letters)
# Purpose: Called after every turn, checks if each slot in the inputted
# letter object list is none. As letters from the letter object list
# are drawn, their slot in the list is set to None, in order to
# signify that the letter has already been drawn
# Parameters: <list> of <letter> objects
# Returns: Nothing, fills the window in GREEN, and writes "You Won!"
def won(letters):
for letter in letters:
if letter != None:
return
DISPLAY.fill(WIN_COLOR)
fontObj = pygame.font.Font('freesansbold.ttf', 60)
letterObj = fontObj.render("You won!", True, BLACK, GREEN)
rect = letterObj.get_rect()
rect.center = (300,300)
DISPLAY.blit(letterObj, rect)
# Main code, ran when the game begins
DISPLAY.fill(BACKGROUND_COLOR)
drawHanger()
letterSpot = drawLines(WORD)
letterObject = makeLetters()
print(WORD)
# Main game loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
letterEntered = pygame.key.name(event.key)
indices = []
i = 0
while i < len(WORD):
if WORD[i] == letterEntered:
indices.append(i)
i = i + 1
if len(indices) > 0:
for index in indices:
letterToDraw = letterObject[index]
if letterToDraw == None:
break
rectObj = letterToDraw.get_rect()
rectObj.center = letterSpot[index]
letterObject[index]= None
DISPLAY.blit(letterToDraw, rectObj)
else:
drawBodyPart()
wrongLetter(letterEntered)
won(letterObject)
pygame.display.update()