-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
264 lines (259 loc) · 11.7 KB
/
game.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
#A Hangman game made using the pygame module in python
#The user can press keys to guess the letters in the word
#He/She has 10 hits. If they dont guess the word by then, they loose the game
#With each hit, the drawing takes form. The game ends when the drawing is complete
#The user can view all guessed letter so far below the word
#With each correct word guessed, the user gains 10 points
import pygame
import random_list as r
import random
import list_letters as ll
#colors
bgblue = "#82EEFD"
black = "#242526"
white = "#FFFFFF"
class HangMan:
#drawing the 1st error line
def draw_error_one(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+665, yB+510), (xB+600, yB+510), 3)
#drawing the 2nd error line
def draw_error_two(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+650, yB+330), (xB+650, yB+510), 3)
#drawing the 3rd error line
def draw_error_three(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+330), (xB+650, yB+330), 3)
#drawing the 4th error line
def draw_error_four(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+350), (xB+598, yB+330), 3)
#drawing the 5th error line
def draw_error_five(xB, yB, self):
pygame.draw.rect(self.screenDim, black, pygame.Rect(xB+580, yB+350, 36, 36), 3, 35)
#drawing the 6th error line
def draw_error_six(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+386), (xB+598, yB+440), 3)
#drawing the 7th error line
def draw_error_seven(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+413), (xB+618, yB+413), 3)
#drawing the 8th error line
def draw_error_eight(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+413), (xB+578, yB+413), 3)
#drawing the 9th error line
def draw_error_nine(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+440), (xB+578, yB+460), 4)
#drawing the 10th error line
def draw_error_ten(xB, yB, self):
pygame.draw.line(self.screenDim, black, (xB+598, yB+440), (xB+618, yB+460), 4)
#printing score
def print_score(xB, yB, self, _score):
font_style = pygame.font.Font("EvilEmpire.ttf", 30)
mesg = font_style.render("Score: "+str(_score), True, black)
self.screenDim.blit(mesg, [xB+100, yB+60])
#printing the guessed letters
def print_guessed_char(xB, yB, self):
font_style = pygame.font.Font("EvilEmpire.ttf", 30)
mesg = font_style.render("Guessed Letters: ", True, black)
self.screenDim.blit(mesg, [xB+100, yB+300])
gx_offset = 0
gy_offset = 30
_count = 0
for letter in self.guessed_letters:
if(_count >0 and _count%10 == 0):
gx_offset = 0
gy_offset = gy_offset + 30
let = font_style.render(letter, True, black)
self.screenDim.blit(let, [xB+100+gx_offset, yB+300+gy_offset])
gx_offset = gx_offset + 30
_count = _count+1
#printing characters
def print_char(xB, yB, c, self):
font_style = pygame.font.Font("EvilEmpire.ttf", 60)
mesg = font_style.render(c, True, black)
self.screenDim.blit(mesg, [xB+100+self.offset, yB+180])
self.offset = self.offset + 50
#function to print all letters
def call_print(xB, yB, self):
for i in self.arr:
if(self.guessed_letters.count(i) == 0):
HangMan.print_char(xB, yB, '_', self)
else:
HangMan.print_char(xB, yB, i, self)
#game loop
def game_loop(self, _score):
w, h = pygame.display.get_surface().get_size()
self.offset = 0
if w!=800:
xB = (w-800)/2
yB = (h-600)/2
else:
xB = 0
yB = 0
self.screenDim.fill(bgblue)
pygame.draw.rect(self.screenDim, white, pygame.Rect(xB+80, yB+130, 630, 130))
HangMan.print_score(xB, yB, self, _score)
HangMan.call_print(xB, yB, self)
HangMan.print_guessed_char(xB, yB, self)
while(self.gameover == False):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.end_game = True
self.gameover = True
return _score
if event.type == pygame.VIDEORESIZE:
self.screenDim.fill(bgblue)
w, h = pygame.display.get_surface().get_size()
self.offset = 0
if w!=800:
xB = (w-800)/2
yB = (h-600)/2
else:
xB = 0
yB = 0
HangMan.print_score(xB, yB, self, _score)
pygame.draw.rect(self.screenDim, white, pygame.Rect(xB+80, yB+130, 630, 130))
if(self.hits > 0):
HangMan.draw_error_one(xB, yB, self)
if(self.hits > 1):
HangMan.draw_error_two(xB, yB, self)
if(self.hits > 2):
HangMan.draw_error_three(xB, yB, self)
if(self.hits > 3):
HangMan.draw_error_four(xB, yB, self)
if(self.hits > 4):
HangMan.draw_error_five(xB, yB, self)
if(self.hits > 5):
HangMan.draw_error_six(xB, yB, self)
if(self.hits > 6):
HangMan.draw_error_seven(xB, yB, self)
if(self.hits > 7):
HangMan.draw_error_eight(xB, yB, self)
if(self.hits > 8):
HangMan.draw_error_nine(xB, yB, self)
if(self.hits > 9):
HangMan.draw_error_ten(xB, yB, self)
HangMan.call_print(xB, yB, self)
HangMan.print_guessed_char(xB, yB, self)
if event.type == pygame.KEYDOWN:
if event.key in ll.letters and chr(event.key) not in self.guessed_letters:
self.screenDim.fill(bgblue)
HangMan.print_score(xB, yB, self, _score)
pygame.draw.rect(self.screenDim, white, pygame.Rect(xB+80, yB+130, 630, 130))
self.guessed_letters.append(chr(event.key))
self.offset = 0
HangMan.call_print(xB, yB, self)
HangMan.print_guessed_char(xB, yB, self)
if(chr(event.key) not in self.arr):
self.hits = self.hits+1
self.has_guessed = True
for letter in self.arr:
if letter not in self.guessed_letters:
self.has_guessed = False
break
if(self.hits > 0):
HangMan.draw_error_one(xB, yB, self)
if(self.hits > 1):
HangMan.draw_error_two(xB, yB, self)
if(self.hits > 2):
HangMan.draw_error_three(xB, yB, self)
if(self.hits > 3):
HangMan.draw_error_four(xB, yB, self)
if(self.hits > 4):
HangMan.draw_error_five(xB, yB, self)
if(self.hits > 5):
HangMan.draw_error_six(xB, yB, self)
if(self.hits > 6):
HangMan.draw_error_seven(xB, yB, self)
if(self.hits > 7):
HangMan.draw_error_eight(xB, yB, self)
if(self.hits > 8):
HangMan.draw_error_nine(xB, yB, self)
if(self.hits > 9):
HangMan.draw_error_ten(xB, yB, self)
if(self.hits == 10):
self.gameover = True
pygame.display.update()
return _score
if(self.has_guessed == True):
_score = _score + 10
self.gameover = True
pygame.display.update()
return _score
#test here
print(self.guessed_letters)
pygame.display.update()
def __init__(self, _score, _screenDim) -> None:
#initialising the game
self.screenDim = _screenDim
self.gameover = False
self.end_game = False
#finding a random word
self.index = random.randrange(len(r.words))
self.arr = list(r.words[self.index])
#test here
print(self.arr)
self.guessed_letters = []
self.has_guessed = False
self.offset = 0
self.hits = 0
score = HangMan.game_loop(self, _score)
w, h = pygame.display.get_surface().get_size()
if w!=800:
xB = (w-800)/2
yB = (h-600)/2
else:
xB = 0
yB = 0
if(self.hits == 10):
self.end_game = True
if(self.end_game == False):
HangMan(score, _screenDim)
while(self.end_game == True):
font_style = pygame.font.Font("EvilEmpire.ttf", 60)
mesg = font_style.render("Game over", True, black)
self.screenDim.fill(bgblue)
pygame.draw.rect(self.screenDim, white, pygame.Rect(xB+80, yB+130, 630, 130))
HangMan.draw_error_one(xB, yB, self)
HangMan.draw_error_two(xB, yB, self)
HangMan.draw_error_three(xB, yB, self)
HangMan.draw_error_four(xB, yB, self)
HangMan.draw_error_five(xB, yB, self)
HangMan.draw_error_six(xB, yB, self)
HangMan.draw_error_seven(xB, yB, self)
HangMan.draw_error_eight(xB, yB, self)
HangMan.draw_error_nine(xB, yB, self)
HangMan.draw_error_ten(xB, yB, self)
HangMan.print_score(xB, yB, self, score)
self.screenDim.blit(mesg, [xB+260, yB+180])
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.end_game = False
if event.type == pygame.VIDEORESIZE:
self.screenDim.fill(bgblue)
w, h = pygame.display.get_surface().get_size()
self.offset = 0
if w!=800:
xB = (w-800)/2
yB = (h-600)/2
else:
xB = 0
yB = 0
pygame.draw.rect(self.screenDim, white, pygame.Rect(xB+80, yB+130, 630, 130))
HangMan.draw_error_one(xB, yB, self)
HangMan.draw_error_two(xB, yB, self)
HangMan.draw_error_three(xB, yB, self)
HangMan.draw_error_four(xB, yB, self)
HangMan.draw_error_five(xB, yB, self)
HangMan.draw_error_six(xB, yB, self)
HangMan.draw_error_seven(xB, yB, self)
HangMan.draw_error_eight(xB, yB, self)
HangMan.draw_error_nine(xB, yB, self)
HangMan.draw_error_ten(xB, yB, self)
HangMan.print_score(xB, yB, self, score)
self.screenDim.blit(mesg, [xB+260, yB+180])
pygame.display.update()
pygame.init()
score = 0
pygame.display.set_caption("Let's play HangMan")
_screenDim = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
HangMan(score, _screenDim)
pygame.quit()
quit()