-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame4.py
344 lines (305 loc) · 11.4 KB
/
game4.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
"""
A great game in which the player has to collect pink hearts! However, the player will
lose points if a black heart is collected! If the score goes negative, the player loses.
See which of your friends can get the highest score!!!
Controls: anything that the player's computer thinks is arrow key input
@author: jovanduy
"""
import pygame
import random
import time
class DrawableSurface():
""" A class that wraps a pygame.Surface and a pygame.Rect """
def __init__(self, surface, rect):
""" Initialize the drawable surface """
self.surface = surface
self.rect = rect
def get_surface(self):
""" Get the surface """
return self.surface
def get_rect(self):
""" Get the rect """
return self.rect
class GameModel():
""" Represents the state of the Game """
def __init__(self, width, height):
""" Initialize the Game model """
self.width = width
self.height = height
self.background = Background(height)
# put character in middle of screen
self.character = Character(width/2 - 15, height/2 - 20)
self.score_text = ScoreText(self.character)
self.hearts = []
for i in range(20):
# more pink hearts than black
if random.randint(0, 30) < 17:
heart = Heart()
self.hearts.append(heart)
else:
black_heart = BlackHeart()
self.hearts.append(black_heart)
def is_hit(self, heart, character):
if (int(heart.x) in range(int(character.x), int(character.x+14))) and (int(heart.y) in range(int(character.y), int(character.y+48))):
character.score += heart.points
return True
return False
def update(self, delta_t, width, height):
""" Updates the model and its constituent parts """
self.character.update(delta_t, width, height)
self.score_text.update()
for heart in self.hearts:
if self.is_hit(heart, self.character):
heart.reset()
heart.update(delta_t)
class Background():
def __init__(self, screen_height):
""" Initializes the border """
self.image = pygame.image.load('images/full_heart.png')
self.tiles = []
for i in range(100):
self.tiles.append(DrawableSurface(self.image, pygame.Rect(i*32, screen_height-32, 32, 32)))
self.tiles.append(DrawableSurface(self.image, pygame.Rect(i*32, 0, 32, 32)))
self.tiles.append(DrawableSurface(self.image, pygame.Rect(0, i*32, 32, 32)))
self.tiles.append(DrawableSurface(self.image, pygame.Rect(608, i*32, 32, 32)))
def draw(self, screen):
""" Draws the border """
for drawable_surface in self.tiles:
screen.blit(drawable_surface.get_surface(),
drawable_surface.get_rect())
class Character():
""" Represents the player in the game """
def __init__(self,x,y):
""" Initialize the character at the specified position
x, y """
self.x = x
self.y = y
# velocities
self.vx = 0
self.vy = 0
self.image = pygame.image.load('images/stand.png')
self.image.set_colorkey((255,255,255))
self.score = 0
self.score_max = self.score
self.is_dead = False
def draw(self, screen):
""" get the drawables that make up the character """
screen.blit(self.image, self.image.get_rect().move(self.x, self.y))
def update(self, delta_t, width, height):
""" Update the character over time. The character is not
allowed past the border of hearts """
if self.score > self.score_max:
self.score_max = self.score
if self.x + self.vx*delta_t <= 30 or self.x + self.vx*delta_t >= width-80:
pass
else:
self.x += self.vx*delta_t
if self.y + self.vy*delta_t <= 30 or self.y + self.vy*delta_t >= height-110:
pass
else:
self.y += self.vy*delta_t
def score(self, is_pink):
""" Updates the score of the Character based on the color
of the heart """
if is_pink:
self.score += 1
else:
self.score -= 1
def move_down(self):
""" increases y velocity so the character can move down """
self.vy += 150
def move_up(self):
""" decreases y velocity so the character can move up """
self.vy -= 150
def move_right(self):
""" increases x velocity so the character can move right """
self.vx += 150
def move_left(self):
""" decreases x velocity so the character can move left """
self.vx -= 150
def move_nowhere_horizontal(self):
self.vx = 0
def move_nowhere_vertical(self):
self.vy = 0
class Heart(object):
""" Represents the hearts to be collected in the game """
def __init__(self):
self.points = 1
# randomly choose from which side of the screen the heart starts
self.side = random.randint(1,4)
if self.side == 1 or self.side == 3:
self.x = random.randint(50,600)
self.vx = 0
if self.side == 1:
self.y = 50
self.vy = 150
else:
self.y = 480 - 10
self.vy = -150
else:
self.y = random.randint(50,400)
self.vy = 0
if self.side == 2:
self.x = 50
self.vx = 150
else:
self.x = 400
self.vx = -150
self.image = pygame.image.load('images/full_heart.png')
self.image.set_colorkey((255,255,255))
self.been_hit = False
def draw(self, screen):
screen.blit(self.image, self.image.get_rect().move(self.x, self.y))
def reach_end_screen(self):
if self.side == 1:
if self.y >= 470:
return True
elif self.side == 2:
if self.x >= 630:
return True
elif self.side == 3:
if self.y <= 10:
return True
elif self.side == 4:
if self.x <= 0:
return True
return False
def reset(self):
self.side = random.randint(1,4)
if self.side == 1 or self.side == 3:
self.x = random.randint(50,600)
self.vx = 0
if self.side == 1:
self.y = 50
self.vy = 150
else:
self.y = 480 - 10
self.vy = -150
else:
self.y = random.randint(50,400)
self.vy = 0
if self.side == 2:
self.x = 50
self.vx = 150
else:
self.x = 400
self.vx = -150
def update(self, delta_t):
if self.reach_end_screen():
self.reset()
self.x += self.vx*delta_t
self.y += self.vy*delta_t
class BlackHeart(Heart):
def __init__(self):
self.points = -1
# randomly choose from which side of the screen the heart starts
self.side = random.randint(1,4)
if self.side == 1 or self.side == 3:
self.x = random.randint(50,600)
self.vx = 0
if self.side == 1:
self.y = 50
self.vy = 150
else:
self.y = 480 - 10
self.vy = -150
else:
self.y = random.randint(50,400)
self.vy = 0
if self.side == 1:
self.x = 50
self.vx = 150
else:
self.x = 400
self.vx = -150
self.image = pygame.image.load('images/black_heart.png')
self.image.set_colorkey((255,255,255))
self.been_hit = False
class ScoreText(object):
def __init__(self, character):
pygame.font.init()
self.font = pygame.font.Font('freesansbold.ttf', 80)
self.character = character
self.score_text = self.font.render(str(self.character.score), 1, (255, 255, 255))
def update(self):
self.score_text = self.font.render(str(self.character.score), 1, (255, 255, 255))
def draw(self, screen):
screen.blit(self.score_text, (40,40))
class GameView(object):
def __init__(self, model, width, height):
""" Initialize the view of the Game """
pygame.init()
# to retrieve width and height use screen.get_size()
self.screen = pygame.display.set_mode((width, height))
# this is used for figuring out where to draw stuff
self.model = model
def draw(self):
""" draw the game window """
# light blue background color
self.screen.fill((68,218,255))
self.model.background.draw(self.screen)
self.model.score_text.draw(self.screen)
self.model.character.draw(self.screen)
for heart in self.model.hearts:
heart.draw(self.screen)
pygame.display.update()
class Game(object):
""" The main Game class """
def __init__(self):
""" Initialize the Game game. Use Game.run() to
start the game """
self.model = GameModel(640, 480)
self.view = GameView(self.model, 640, 480)
self.controller = Controller(self.model)
def run(self):
""" the main runloop... loop until character's score is negative """
last_update = time.time()
while True:
if self.model.character.score < 0:
print 'Your highest score was: ' + str(self.model.character.score_max)
break
self.view.draw()
self.controller.process_events()
delta_t = time.time() - last_update
self.model.update(delta_t, 640, 480)
last_update = time.time()
class Controller():
def __init__(self, model):
""" initialize the (what the computer thinks is) keyboard input controller """
self.model = model
self.up_pressed = False
self.down_pressed = False
self.left_pressed = False
self.right_pressed = False
def process_events(self):
""" process an arrow key press """
pygame.event.pump()
if not(pygame.key.get_pressed()[pygame.K_UP]):
self.up_pressed = False
elif not(self.up_pressed):
self.up_pressed = True
self.model.character.move_up()
if not(pygame.key.get_pressed()[pygame.K_DOWN]):
self.down_pressed = False
elif not(self.down_pressed):
self.down_pressed = True
self.model.character.move_down()
if not(pygame.key.get_pressed()[pygame.K_LEFT]):
self.left_pressed = False
elif not(self.left_pressed):
self.left_pressed = True
self.model.character.move_left()
if not(pygame.key.get_pressed()[pygame.K_RIGHT]):
self.right_pressed = False
elif not(self.right_pressed):
self.right_pressed = True
self.model.character.move_right()
# if both up and down or left and right are pressed, do not move
# vertically or horizontally, respectively
if not(self.up_pressed) and not(self.down_pressed):
self.model.character.move_nowhere_vertical()
if not(self.left_pressed) and not(self.right_pressed):
self.model.character.move_nowhere_horizontal()
if __name__ == '__main__':
game = Game()
game.run()