-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
executable file
·468 lines (440 loc) · 16.8 KB
/
main.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame
import sys
import config
import random
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((config.sw, config.sh))
p1s = 0
p1e = 0
p2s = 0
p2e = 0
p1score = 0
p2score = 0
p1time = 0
p2time = 0
k = 0
w1 = 0
w2 = 0
def disp(text, colour, pos):
font = pygame.font.Font(config.retro, 15)
display = font.render(text, False, colour)
screen.blit(display, pos)
def instructions():
showing_options = True
while showing_options:
screen.fill(config.BLACK)
font = pygame.font.Font(config.retro, 40)
display = font.render('Instructions', False, config.RED)
screen.blit(display, (config.sw / 4 + 200, config.sh * 0.15))
font = pygame.font.Font(config.retro, 20)
display = font.render('P1: Use the arrow keys to move up, left, down or right.', False, config.GREEN)
screen.blit(display, (config.sw / 4, config.sh * 0.45 - 50))
display = font.render('P2: Use the "W A S D" keys to move up, left, down or right respectively.', False,
config.GREEN)
screen.blit(display, (config.sw / 4, config.sh * 0.45))
display = font.render('Reach the other side of the screen to win a round.', False,
config.GREEN)
screen.blit(display, (config.sw / 4, config.sh * 0.45 + 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
showing_options = False
pygame.display.update()
clock.tick(60)
def intro():
display_crash_text = True
is_selected = {"button_1": False, "button_2": False}
while display_crash_text:
mouse = pygame.mouse.get_pos()
screen.fill(config.BLACK)
font = pygame.font.Font(config.retro, 80)
display = font.render('C Y K ', False, config.GREEN)
screen.blit(display, (config.sw / 2 - 200, config.sh / 4 - 100))
disp('presents you', config.RED, (config.sw / 2 - 170, config.sh
/ 4))
font = pygame.font.Font(config.retro, 30)
display = font.render('THIS AINT MY LAST RIDE', False,
config.BLUE)
screen.blit(display, (config.sw / 2 - 200, config.sh / 4 + 40))
button_1 = pygame.Rect(
int(config.sw / 2) - 150,
int(config.sh / 2) - 40, 200, 50)
button_2 = pygame.Rect(
int(config.sw / 2) - 150,
int(config.sh / 2) + 40, 200, 50)
if is_selected["button_1"]:
pygame.draw.rect(screen, config.WHITE, button_1)
font = pygame.font.Font(config.retro, 20)
display = font.render("Play", False, config.RED)
screen.blit(display, (config.sw / 2 - 140, config.sh / 2 - 25))
else:
pygame.draw.rect(screen, config.RED, button_1)
font = pygame.font.Font(config.retro, 20)
display = font.render("Play", False, config.WHITE)
screen.blit(display, (config.sw / 2 - 140, config.sh / 2 - 25))
if is_selected["button_2"]:
pygame.draw.rect(screen, config.WHITE, button_2)
font = pygame.font.Font(config.retro, 20)
display = font.render("Instructions", False, config.RED)
screen.blit(display, (config.sw / 2 - 140, config.sh / 2 + 55))
else:
pygame.draw.rect(screen, config.RED, button_2)
font = pygame.font.Font(config.retro, 20)
display = font.render("Instructions", False, config.WHITE)
screen.blit(display, (config.sw / 2 - 140, config.sh / 2 + 55))
if button_1.collidepoint(*mouse):
is_selected["button_1"] = True
is_selected["button_2"] = False
elif button_2.collidepoint(*mouse):
is_selected["button_2"] = True
is_selected["button_1"] = False
elif pygame.mouse.get_rel() != (0, 0):
for button in is_selected:
is_selected[button] = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_SPACE:
if is_selected["button_1"]:
display_crash_text = False
elif is_selected["button_2"]:
instructions()
if event.key == pygame.K_UP or event.key == pygame.K_w:
is_selected["button_1"] = True
is_selected["button_2"] = False
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
is_selected["button_1"] = False
is_selected["button_2"] = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if is_selected["button_1"]:
display_crash_text = False
elif is_selected["button_2"]:
instructions()
pygame.display.update()
clock.tick(60)
def crash(x):
display_crash_text = 500
while display_crash_text:
screen.fill(config.BLACK)
disp(x, config.RED, (config.sw / 2 - 150, config.sh / 2))
display_crash_text -= 1
pygame.display.flip()
pygame.display.set_caption('this aint my last ride')
running = True
player = pygame.image.load('boat.png').convert_alpha()
player2 = pygame.image.load('boat2.png').convert_alpha()
player = pygame.transform.scale(player, (80, 40))
player2 = pygame.transform.scale(player2, (80, 40))
obstacle = []
obstacle_mask = []
obstacle_rect = []
offseto_x = []
offseto_y = []
offseto2_x = []
offseto2_y = []
intro()
# crash("Loading game . . .")
for i in range(90):
obstacle.append(pygame.image.load('iceberg.png').convert_alpha())
obstacle[i] = pygame.transform.scale(obstacle[i], (100, 100))
obstacle_mask.append(pygame.mask.from_surface(obstacle[i], 50))
obstacle_rect.append(obstacle[i].get_rect())
offseto_x.append(0)
offseto_y.append(0)
offseto2_x.append(0)
offseto2_y.append(0)
obstacle_rect[i].y = random.randint(30, 650)
obstacle_rect[i].x = random.randint(50, 1600)
enemy = []
enemy_mask = []
enemy_rect = []
offset_x = []
offset_y = []
offset2_x = []
offset2_y = []
for i in range(10):
if i < 6:
enemy.append(pygame.image.load('pirate.png').convert_alpha())
enemy[i] = pygame.transform.scale(enemy[i], (150, 75))
else:
enemy.append(pygame.image.load('bgbattleship.png'
).convert_alpha())
enemy[i] = pygame.transform.scale(enemy[i], (200, 200))
enemy_mask.append(pygame.mask.from_surface(enemy[i], 50))
enemy_rect.append(enemy[i].get_rect())
offset_x.append(0)
offset_y.append(0)
offset2_x.append(0)
offset2_y.append(0)
enemy_rect[i].y = i * 127 + 46
enemy_rect[i].x = random.randint(0, 1600)
enemy_rect[6].y = 2 * 127 + 15
enemy_rect[6].x = 1568
enemy_rect[7].y = 3 * 127 + 10
enemy_rect[7].x = random.randint(0, 1600)
enemy_rect[8].y = 4 * 127 + 10
enemy_rect[8].x = random.randint(0, 1600)
enemy_rect[9].y = 1 * 127 + 10
enemy_rect[9].x = random.randint(0, 1600)
playerXr = 0
playerXl = 0
playerYu = 0
playerYd = 0
speed = config.playerspeed
player_mask = pygame.mask.from_surface(player, 50)
player2_mask = pygame.mask.from_surface(player2, 50)
player_rect = player.get_rect()
player2_rect = player2.get_rect()
afont = pygame.font.Font(None, 16)
hitsurf = afont.render('Hit!!! Oh noes!!', 1, (255, 255, 255))
(last_px, last_py) = (0, 0)
player_rect.x = config.sw / 2
player_rect.y = 760
player2_rect.x = config.sw / 2
player2_rect.y = 0
crash('Round 1')
while running:
screen.fill(config.AQUA)
for i in range(7):
pygame.draw.rect(screen, config.BLUE, [0, i * 127, config.sw,
40])
if not k % 2:
p1time += 1
player_rect.x = player_rect.x + playerXr - playerXl
player_rect.y = player_rect.y + playerYd - playerYu
if player_rect.x <= 0:
player_rect.x = 0
if player_rect.x >= config.sw - 80:
player_rect.x = config.sw - 80
if player_rect.y <= 0:
player_rect.y = 0
pygame.time.wait(500)
crash('P1' + config.suc + str(k // 2 + 1))
w1 += 1
p1score += p1e + p1s
screen.fill(config.BLACK)
player_rect.x = config.sw / 2
player_rect.y = 760
k += 1
playerXl = 0
playerXr = 0
playerYu = 0
playerYd = 0
if player_rect.y >= config.sh - 40:
player_rect.y = config.sh - 40
else:
p2time += 1
player2_rect.x = player2_rect.x + playerXr - playerXl
player2_rect.y = player2_rect.y + playerYd - playerYu
if player2_rect.x <= 0:
player2_rect.x = 0
if player2_rect.x >= config.sw - 80:
player2_rect.x = config.sw - 80
if player2_rect.y <= 0:
player2_rect.y = 0
if player2_rect.y >= config.sh - 40:
player2_rect.y = config.sh - 40
pygame.time.wait(500)
crash('P2' + config.suc + str(k // 2 + 1))
p2score += p2e + p2s
player2_rect.x = config.sw / 2
player2_rect.y = 0
k += 1
w2 += 1
if k == 8:
running = 0
break
crash('Round ' + str(k // 2 + 1))
playerXl = 0
playerXr = 0
playerYu = 0
playerYd = 0
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN \
and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.KEYDOWN:
if not k % 2:
if event.key == pygame.K_LEFT:
playerXl = speed
if event.key == pygame.K_RIGHT:
playerXr = speed
if event.key == pygame.K_UP:
playerYu = speed
if event.key == pygame.K_DOWN:
playerYd = speed
else:
if event.key == pygame.K_a:
playerXl = speed
if event.key == pygame.K_d:
playerXr = speed
if event.key == pygame.K_w:
playerYu = speed
if event.key == pygame.K_s:
playerYd = speed
if event.type == pygame.KEYUP:
if k % 2:
if event.key == pygame.K_a:
playerXl = 0
if event.key == pygame.K_d:
playerXr = 0
if event.key == pygame.K_w:
playerYu = 0
if event.key == pygame.K_s:
playerYd = 0
else:
if event.key == pygame.K_LEFT:
playerXl = 0
if event.key == pygame.K_RIGHT:
playerXr = 0
if event.key == pygame.K_UP:
playerYu = 0
if event.key == pygame.K_DOWN:
playerYd = 0
(px, py) = (player_rect[0], player_rect[1])
(p2x, p2y) = (player2_rect[0], player2_rect[1])
disp('P1 Score: ' + str(p1score + p1s + p1e), config.GREEN, (0, 0))
disp('P1 Time: ' + str(p1time // 40), config.GREEN, (0, 20))
disp('P2 Score: ' + str(p2score + p2s + p2e), config.RED, (1400, 0))
disp('P2 Time: ' + str(p2time // 40), config.RED, (1400, 20))
if k % 2:
disp('Start', config.RED, (config.sw / 2 - 100, 0))
disp('End', config.RED, (config.sw / 2 - 100, 760))
else:
disp('Start', config.GREEN, (config.sw / 2 - 100, 760))
disp('End', config.GREEN, (config.sw / 2 - 100, 0))
p1e = 0
p2e = 0
for i in range(6 + k // 2 + 1):
offset_x[i] = px - enemy_rect[i][0]
offset_y[i] = py - enemy_rect[i][1]
offset2_x[i] = p2x - enemy_rect[i][0]
offset2_y[i] = p2y - enemy_rect[i][1]
a = enemy_mask[i].overlap(player_mask, (offset_x[i],
offset_y[i]))
b = enemy_mask[i].overlap(player2_mask, (offset2_x[i],
offset2_y[i]))
overlap = a or b
if offset_y[i] < 0:
p1e += 10
if offset2_y[i] > 0:
p2e += 10
if overlap:
pygame.time.wait(200)
if not k % 2:
crash('P1' + config.cra)
p1score += p1e + p1s
player_rect.x = config.sw / 2
player_rect.y = 760
else:
crash('P2' + config.cra)
p2score += p2e + p2s
player2_rect.x = config.sw / 2
player2_rect.y = 0
if k + 1 == 8:
running = 0
break
crash('Round ' + str(k // 2 + 2))
k += 1
playerXl = 0
playerXr = 0
playerYu = 0
playerYd = 0
overlap = 0
break
p1s = 0
p2s = 0
for i in range(10 + k // 2 * 20):
offseto_x[i] = px - obstacle_rect[i][0]
offseto_y[i] = py - obstacle_rect[i][1]
offseto2_x[i] = p2x - obstacle_rect[i][0]
offseto2_y[i] = p2y - obstacle_rect[i][1]
if offseto_y[i] < 0:
p1s += 5
if offseto2_y[i] > 0:
p2s += 5
c = obstacle_mask[i].overlap(player_mask, (offseto_x[i],
offseto_y[i]))
d = obstacle_mask[i].overlap(player2_mask, (offseto2_x[i],
offseto2_y[i]))
overlap = c or d
if overlap:
screen.blit(hitsurf, (0, 0))
pygame.time.wait(500)
if not k % 2:
crash('P1' + config.cra)
p1score += p1e + p1s
player_rect.x = config.sw / 2
player_rect.y = 760
pygame.time.wait(300)
else:
crash('P2' + config.cra)
p2score += p2e + p2s
player2_rect.x = config.sw / 2
player2_rect.y = 0
if k + 1 == 8:
running = 0
break
crash('Round ' + str(k // 2 + 2))
pygame.time.wait(300)
k += 1
playerXl = 0
playerXr = 0
playerYu = 0
playerYd = 0
break
if k == 8:
break
for i in range(6 + k // 2 + 1):
if not (k % 2):
if i < 3:
enemy_rect[i].x = (enemy_rect[i].x + (i + 1) * 3 + w1
* 5 + 2) % 1600
elif i < 6:
enemy_rect[i].x = (enemy_rect[i].x + (6 - i) * 3 + w1
* 5 + 2) % 1600
else:
enemy_rect[i].x = (enemy_rect[i].x + w1 * (10 - i) * 3) \
% 1600
else:
if i < 3:
enemy_rect[i].x = (enemy_rect[i].x + (i + 1) * 3 + w2
* 5 + 2) % 1600
elif i < 6:
enemy_rect[i].x = (enemy_rect[i].x + (6 - i) * 3 + w2
* 5 + 2) % 1600
else:
enemy_rect[i].x = (enemy_rect[i].x + w2 * (10 - i) * 2) \
% 1600
screen.blit(enemy[i], (enemy_rect[i][0], enemy_rect[i][1]))
for i in range(10 + k // 2 * 20):
screen.blit(obstacle[i], (obstacle_rect[i][0],
obstacle_rect[i][1]))
screen.blit(player, (player_rect[0], player_rect[1]))
screen.blit(player2, (player2_rect[0], player2_rect[1]))
pygame.display.flip()
clock.tick(40)
if p1score > p2score:
crash('GAME OVER ,P1 , You win')
elif p2score > p1score:
crash('GAME OVER ,P2 , You win')
else:
if p1time < p2time:
crash('GAME OVER ,P1 , You win')
elif p2time < p1time:
crash('GAME OVER, P2 , You win')
else:
crash('GAME OVER, You both win')
pygame.quit()