-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblock_life.py
More file actions
419 lines (324 loc) · 9.79 KB
/
block_life.py
File metadata and controls
419 lines (324 loc) · 9.79 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Imports
import pygame
import random
# Initialize game engine
pygame.init()
# Window
WIDTH = 800
HEIGHT = 600
SIZE = (WIDTH, HEIGHT)
TITLE = "Block Life"
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption(TITLE)
# Timer
clock = pygame.time.Clock()
refresh_rate = 60
# Colors
YELLOW = (255, 205, 1)
WHITE = (255, 255, 255)
BROWN = (100, 75, 55)
SKY_BLUE = (135, 206, 235)
# Sounds
start_theme = None
main_theme = "sounds/theme.ogg"
end_theme = None
coin_sound = pygame.mixer.Sound("sounds/coin.ogg")
# Fonts
font_xs = pygame.font.Font(None, 40)
font_sm = pygame.font.Font(None, 48)
font_md = pygame.font.Font(None, 64)
font_lg = pygame.font.Font(None, 96)
font_xl = pygame.font.Font("fonts/LuckiestGuy.ttf", 112)
# Images
''' platforms '''
mud_left = pygame.image.load("images/mud_left.png").convert_alpha()
mud_middle = pygame.image.load("images/mud_middle.png").convert_alpha()
mud_right = pygame.image.load("images/mud_right.png").convert_alpha()
platform_surf = pygame.Surface([600, 48], pygame.SRCALPHA, 32)
platform_surf.blit(mud_left, [0, 0])
for i in range(48, 600-48, 48):
platform_surf.blit(mud_middle, [i, 0])
platform_surf.blit(mud_right, [600-48, 0])
''' stars '''
star = pygame.image.load("images/star.png").convert_alpha()
star_sm = pygame.image.load("images/star_sm.png").convert_alpha()
''' block '''
happy = pygame.image.load("images/happy.png").convert_alpha()
scared = pygame.image.load("images/scared.png").convert_alpha()
falling = pygame.image.load("images/falling.png").convert_alpha()
dead = pygame.image.load("images/dead.png").convert_alpha()
''' background '''
rocks = pygame.image.load("images/rocks.png").convert_alpha()
bg_surf = pygame.Surface([WIDTH, HEIGHT + 600], pygame.SRCALPHA, 32)
bg_surf.fill(SKY_BLUE)
for x in range(0, WIDTH, 200):
for y in range(400, HEIGHT + 600, 200):
bg_surf.blit(rocks, [x, y])
''' window icon '''
icon = pygame.image.load("images/icon.png").convert_alpha()
pygame.display.set_icon(icon)
# Stages
START = 0
PLAYING = 1
END = 2
# Physics
gravity = 1
terminal_velocity = 7
# Helper functions
def intersects(rect1, rect2):
left1 = rect1[0]
right1 = rect1[0] + rect1[2] - 1
top1 = rect1[1]
bottom1 = rect1[1] + rect1[3] - 1
left2 = rect2[0]
right2 = rect2[0] + rect2[2] - 1
top2 = rect2[1]
bottom2 = rect2[1] + rect2[3] - 1
return not (right1 < left2 or right2 < left1 or
bottom1 < top2 or bottom2 < top1)
def generate_platform(y):
opening_size = 200
w = WIDTH - opening_size
h = 50
offset = random.randrange(0, w)
wall1 = [0, y, w, h]
wall2 = [w + opening_size, y, w, h]
wall1[0] -= offset
wall2[0] -= offset
if random.randrange(0, 2) == 0:
x = random.randrange(50, WIDTH - 50)
y = y - 48
coin = [x, y, 36, 36]
else:
coin = None
return wall1, wall2, coin
def set_music(track):
if pygame.mixer.music.get_busy():
pygame.mixer.music.fadeout(2500)
if track != None:
pygame.mixer.music.load(track)
pygame.mixer.music.play(-1)
def setup():
global block, block_vx, block_vy, block_speed, face
global platform_gap, walls, wall_speed, stage
global ticks, score, coins, collected_coins
global bg_y, scroll_speed
''' Make a block '''
block = [376, 72, 48, 48]
block_vx = 0
block_vy = 0
block_speed = 6
face = happy
''' scoring '''
ticks = 0
score = 0
collected_coins = 0
''' make starting walls '''
platform_gap = 200
walls = []
coins = []
for i in range(5):
left, right, coin = generate_platform(i * platform_gap + 2 * platform_gap)
walls.append(left)
walls.append(right)
if coin != None:
coins.append(coin)
''' background scroll position '''
bg_y = 0
scroll_speed = 0
''' initial wall speed '''
wall_speed = 2
''' set stage '''
stage = START
''' music '''
set_music(start_theme)
def update_platforms():
global walls, coins
to_remove = []
for wall in walls:
wall[1] -= wall_speed
if wall[1] + wall[3] < 0:
to_remove.append(wall)
if len(to_remove) > 0:
walls = [wall for wall in walls if wall not in to_remove]
y = walls[-1][1] + platform_gap
left, right, coin = generate_platform(y)
walls.append(left)
walls.append(right)
if coin != None:
coins.append(coin)
def update_coins():
global score
to_remove = None
for coin in coins:
coin[1] -= wall_speed
if coin[1] + coin[3] < 0:
to_remove = coin
if to_remove != None:
coins.remove(to_remove)
def update_block():
global block, block_vx, block_vy, block_speed, face
global score, collected_coins
global message, message_timer
global bg_y
''' apply gravity '''
block_vy += gravity
block_vy = min(block_vy, terminal_velocity)
if block[1] < 500:
block[1] += block_vy
else:
for obj in (walls + coins):
obj[1] -= block_vy
bg_y -= block_vy
''' resolve collisions with each wall '''
for wall in walls:
if intersects(block, wall):
block_vy = 0
block[1] = wall[1] - block[3]
''' move the block in horizontal direction '''
block[0] += block_vx
''' resolve collisions with each wall again '''
for wall in walls:
if intersects(block, wall):
if block_vx > 0:
block[0] = wall[0] - block[2]
elif block_vx < 0:
block[0] = wall[0] + wall[2]
''' block wraps around screen '''
if block[0] < 0 - block[2]:
block[0] = WIDTH - block[2]
elif block[0] > WIDTH:
block[0] = 0 - block[2]
''' collect coins '''
hit_list = [coin for coin in coins if intersects(block, coin)]
for hit in hit_list:
coin_sound.play()
coins.remove(hit)
collected_coins += 1
score += 100
if collected_coins == 15:
block_speed += 2
message = "Speed Bonus!"
message_timer = 2 * refresh_rate
''' update face '''
if block_vy > 3:
face = falling
elif block[1] < 150:
face = scared
else:
face = happy
def update_score():
global score
score += 1
def update_level():
global wall_speed
if ticks % 100 == 0:
wall_speed += .08
def update_bg():
global bg_y, scroll_speed
if bg_y > -400:
scroll_speed = wall_speed
else:
scroll_speed = 5 * wall_speed // 6
bg_y -= scroll_speed
if bg_y < -600:
bg_y = -400
# Drawing functions
def draw_background():
screen.blit(bg_surf, [0, bg_y])
def draw_block():
screen.blit(face, [block[0], block[1]])
def draw_platforms():
for wall in walls:
screen.blit(platform_surf, [wall[0], wall[1]])
def draw_coins():
for coin in coins:
screen.blit(star, [coin[0], coin[1]])
def draw_score():
text = font_md.render(str(score), 1, WHITE)
screen.blit(text, [16, 16])
text = font_xs.render(str(collected_coins), 1, YELLOW)
w = text.get_width()
screen.blit(text, [WIDTH - w - 16, 20])
screen.blit(star_sm, [WIDTH - w - 48, 20])
def draw_start_screen():
text = font_xl.render("BLOCK LIFE", 1, BROWN)
w = text.get_width()
screen.blit(text, [WIDTH/2 - w/2, 180])
text = font_xs.render("Press SPACE to start", 1, WHITE)
w = text.get_width()
screen.blit(text, [WIDTH/2 - w/2, 290])
def draw_end_screen():
text = font_lg.render("GAME OVER", 1, WHITE)
w = text.get_width()
screen.blit(text, [WIDTH/2 - w/2, 210])
text = font_xs.render("Press SPACE to play again", 1, WHITE)
w = text.get_width()
screen.blit(text, [WIDTH/2 - w/2, 280])
def draw_message(message):
text = font_md.render(message, 1, WHITE)
w = text.get_width()
screen.blit(text, (WIDTH/2 - w/2, 200))
# Game loop
setup()
done = False
message_timer = 0
while not done:
# Event processing (React to key presses, mouse clicks, etc.)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if stage == START:
set_music(main_theme)
stage = PLAYING
elif stage == END:
setup()
state = pygame.key.get_pressed()
# Game logic
if stage == PLAYING:
''' block controls '''
left = state[pygame.K_LEFT]
right = state[pygame.K_RIGHT]
if left:
block_vx = -block_speed
elif right:
block_vx = block_speed
else:
block_vx = 0
''' update game '''
update_platforms()
update_coins()
update_block()
update_score()
update_level()
update_bg()
''' check for dead block '''
if block[1] < 0:
stage = END
face = dead
set_music(end_theme)
''' messages '''
if message_timer > 0:
message_timer -= 1
''' count ticks '''
ticks += 1
# Drawing code
draw_background()
draw_block()
draw_platforms()
draw_coins()
draw_score()
if stage == START:
draw_start_screen()
elif stage == END:
draw_end_screen()
elif message_timer > 0:
draw_message(message)
# Update screen
pygame.display.flip()
# Limit refresh rate
clock.tick(refresh_rate)
# Close window and quit
pygame.quit()