-
Notifications
You must be signed in to change notification settings - Fork 0
/
HangmanGame.py
208 lines (174 loc) · 6.9 KB
/
HangmanGame.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
#############################################
#######THIS IS THE HANGMAN GAME PROJECT########
############# BY SHOURYA PANT ################
############################################
import pygame
import random
pygame.init()
winHeight = 600
winWidth = 700
win=pygame.display.set_mode((winWidth,winHeight))
pygame.display.set_caption("Hangman Game! BY SHOURYA PANT")
##################################################
### Initializing The COLOURS that will be used in the PROJECT###
##################################################
BLACK = (0,0, 0)
WHITE = (255,255,255)
RED = (255,0, 0)
GREEN = (50,205,50)
BLUE = (0,0,255)
LIGHT_BLUE = (51,161,201)
PURPLE=(150,0,211)
ORANGE=(255,69,0)
MAGENTA=(220,25,60)
YELLOW=(255,215,0)
RANDOMCOLOUR=(255,20,147)
##############################################################
#### Initializing The FONTS AND VARIABLES that will be used in the PROJECT####
##############################################################
btn_font = pygame.font.SysFont("Algerian", 30)
guess_font = pygame.font.SysFont("Permanentmarker", 40)
lost_font = pygame.font.SysFont('Ravie', 24)
word_font=pygame.font.SysFont('Jokerman', 30)
TITLE_FONT=pygame.font.SysFont('Permanentmarker',40)
word = ''
buttons = []
guessed = []
hangmanPics = [pygame.image.load('hangman0.png'), pygame.image.load('hangman1.png'), pygame.image.load('hangman2.png'), pygame.image.load('hangman3.png'), pygame.image.load('hangman4.png'), pygame.image.load('hangman5.png'), pygame.image.load('hangman6.png')]
limbs = 0
#################################################################################
## DEFINING THE REDRAW GAME WINDOW FUNCTION THAT WILL PLAY A MAIN ROLE IN OUR PROJECT ##
#################################################################################
def redraw_game_window():
global guessed
global hangmanPics
global limbs
win.fill(LIGHT_BLUE)
text = TITLE_FONT.render("HANGMAN BY SHOURYA PANT", 1, YELLOW)
win.blit(text, (winWidth/2 - text.get_width()/2, 480))
# Buttons
for i in range(len(buttons)):
if buttons[i][4]:
pygame.draw.circle(win, BLACK, (buttons[i][1], buttons[i][2]), buttons[i][3])
pygame.draw.circle(win, buttons[i][0], (buttons[i][1], buttons[i][2]), buttons[i][3] - 2)
label = btn_font.render(chr(buttons[i][5]), 1, BLACK)
win.blit(label, (buttons[i][1] - (label.get_width() / 2), buttons[i][2] - (label.get_height() / 2)))
spaced = spacedOut(word, guessed)
label1 = guess_font.render(spaced, 1, PURPLE)
rect = label1.get_rect()
length = rect[2]
win.blit(label1,(winWidth/2 - length/2, 400))
pic = hangmanPics[limbs]
win.blit(pic, (winWidth/2 - pic.get_width()/2 + 20, 150))
pygame.display.update()
###########################################################################
### DEFINING THE RANDOM WORD FUNCTION THAT WILL GENERATE A RANDOMIZED WORD ###
##################### FROM THE GIVEN FILE WORDS.TXT ###########################
##########################################################################
def randomWord():
file = open('words.txt')
f = file.readlines()
i = random.randrange(0, len(f) - 1)
return f[i][:-1]
def hang(guess):
global word
if guess.lower() not in word.lower():
return True
else:
return False
def spacedOut(word, guessed=[]):
spacedWord = ''
guessedLetters = guessed
for x in range(len(word)):
if word[x] != ' ':
spacedWord += '_ '
for i in range(len(guessedLetters)):
if word[x].upper() == guessedLetters[i]:
spacedWord = spacedWord[:-2]
spacedWord += word[x].upper() + ' '
elif word[x] == ' ':
spacedWord += ' '
return spacedWord
def buttonHit(x, y):
for i in range(len(buttons)):
if x < buttons[i][1] + 20 and x > buttons[i][1] - 20:
if y < buttons[i][2] + 20 and y > buttons[i][2] - 20:
return buttons[i][5]
return None
###########################################################################
###############DETERMINING WHETHER THE USER IS WINNER OR LOSER ################
##########################################################################
def end(winner=False):
global limbs
lostTxt = 'You Lost, press any key to play again...'
winTxt = 'WINNER!, press any key to play again...'
redraw_game_window()
pygame.time.delay(1000)
win.fill(GREEN)
if winner == True:
label = lost_font.render(winTxt, 1, RANDOMCOLOUR)
else:
label = lost_font.render(lostTxt, 1, ORANGE)
wordTxt = lost_font.render(word.upper(), 1, RED)
wordWas = word_font.render('The phrase was: ', 1, BLUE)
win.blit(wordTxt, (winWidth/2 - wordTxt.get_width()/2, 295))
win.blit(wordWas, (winWidth/2 - wordWas.get_width()/2, 245))
win.blit(label, (winWidth / 2 - label.get_width() / 2, 140))
pygame.display.update()
again = True
while again:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
again = False
reset()
def reset():
global limbs
global guessed
global buttons
global word
for i in range(len(buttons)):
buttons[i][4] = True
limbs = 0
guessed = []
word = randomWord()
###########################################################################
#############ADDING THE ALPHABETS BUTTON IN OUR HANGMAN GAME ################
###########################################################################
increase = round(winWidth / 13)
for i in range(26):
if i < 13:
y = 40
x = 25 + (increase * i)
else:
x = 25 + (increase * (i - 13))
y = 85
buttons.append([LIGHT_BLUE, x, y, 20, True, 65 + i])
word = randomWord()
inPlay = True
while inPlay:
redraw_game_window()
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
inPlay = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
inPlay = False
if event.type == pygame.MOUSEBUTTONDOWN:
clickPos = pygame.mouse.get_pos()
letter = buttonHit(clickPos[0], clickPos[1])
if letter != None:
guessed.append(chr(letter))
buttons[letter - 65][4] = False
if hang(chr(letter)):
if limbs != 5:
limbs += 1
else:
end()
else:
print(spacedOut(word, guessed))
if spacedOut(word, guessed).count('_') == 0:
end(True)
pygame.quit()