forked from rushilp002/beachHacks2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
275 lines (220 loc) · 10.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
import os
import sprites
import LinkedList
import random
import pygame
import time
import pygame
from pygame.locals import *
from pygame import mixer
pygame.font.init()
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
SHARK_WIDTH, SHARK_HEIGHT = 80, 80
BOTTLE_WIDTH, BOTTLE_HEIGHT = 10, 10
WIDTH, HEIGHT = 900, 500
SCREEN_COLOR = (150, 150, 255)
WHITE = (255, 255, 255)
BACKGROUND_SPEED = 4
PLAYER_SPEED = 5
FPS = 60
COLLISION = pygame.USEREVENT + 1
FONT = pygame.font.SysFont('timesnewroman', 40)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
SHARK_IMAGE = pygame.image.load(os.path.join('assets', 'shark.png'))
CLAM_IMAGE = pygame.image.load(os.path.join('assets', 'clam.png'))
CRAB_IMAGE = pygame.image.load(os.path.join('assets', 'crab.png'))
BOTTLE_IMAGE = pygame.image.load(os.path.join('assets', 'water_bottle.png'))
ISLAND_IMAGE = pygame.image.load(os.path.join('assets', 'island.png'))
SODA_IMAGE = pygame.image.load(os.path.join('assets', 'soda_can.png'))
OCEAN_IMAGE = pygame.image.load(os.path.join('assets', 'ocean.png'))
HEART_IMAGE = pygame.image.load(os.path.join('assets', 'heart.png'))
WRAPPER_IMAGE = pygame.image.load(os.path.join('assets', 'food_wrapper_0.png'))
FORK_IMAGE = pygame.image.load(os.path.join('assets', 'fork.png'))
BAG_IMAGE = [pygame.image.load(os.path.join('assets', 'plastic_bag.png')), pygame.image.load(os.path.join('assets', 'plastic_bag_1.png')),
pygame.image.load(os.path.join('assets', 'plastic_bag_2.png')),]
SPLASH_IMAGE = [pygame.image.load(os.path.join('assets', 'splash_0.png')), pygame.image.load(os.path.join('assets', 'splash_1.png')),
pygame.image.load(os.path.join('assets', 'splash_2.png')), pygame.image.load(os.path.join('assets', 'splash_3.png'))]
OCEAN = [0, 900 , 1800]
HEARTS = [10, 60, 110, 160, 210,]
ISLAND = pygame.transform.scale(ISLAND_IMAGE, (350, 350))
CRAB = pygame.transform.scale(CRAB_IMAGE, (300, 300))
SHARK = pygame.transform.scale(SHARK_IMAGE, (100, 100))
FORK = [FORK_IMAGE, pygame.transform.rotate(FORK_IMAGE, 90), pygame.transform.rotate(FORK_IMAGE, 180), pygame.transform.rotate(FORK_IMAGE, 270)]
BOTTLES = [BOTTLE_IMAGE, pygame.transform.rotate(BOTTLE_IMAGE, 90), pygame.transform.rotate(BOTTLE_IMAGE, 180), pygame.transform.rotate(BOTTLE_IMAGE, 270)]
SODA = [SODA_IMAGE, pygame.transform.rotate(SODA_IMAGE, 90), pygame.transform.rotate(SODA_IMAGE, 180), pygame.transform.rotate(SODA_IMAGE, 270)]
WRAPPERS = [WRAPPER_IMAGE, pygame.transform.rotate(WRAPPER_IMAGE, 90), pygame.transform.rotate(WRAPPER_IMAGE, 180), pygame.transform.rotate(WRAPPER_IMAGE, 270)]
BAGS = [BAG_IMAGE[0], pygame.transform.rotate(BAG_IMAGE[0], 90), pygame.transform.rotate(BAG_IMAGE[0], 180), pygame.transform.rotate(BAG_IMAGE[0], 270),
BAG_IMAGE[1], pygame.transform.rotate(BAG_IMAGE[1], 90), pygame.transform.rotate(BAG_IMAGE[1], 180), pygame.transform.rotate(BAG_IMAGE[1], 270),
BAG_IMAGE[2], pygame.transform.rotate(BAG_IMAGE[2], 90), pygame.transform.rotate(BAG_IMAGE[2], 180), pygame.transform.rotate(BAG_IMAGE[2], 270)]
SPLASH = [pygame.transform.scale(SPLASH_IMAGE[0], (100, 100)), pygame.transform.scale(SPLASH_IMAGE[1], (100, 100)),
pygame.transform.scale(SPLASH_IMAGE[2], (100, 100)), pygame.transform.scale(SPLASH_IMAGE[3], (100, 100))]
WAVES = [pygame.transform.scale(SPLASH_IMAGE[0], (100, 100)), pygame.transform.scale(SPLASH_IMAGE[1], (100, 100)),
pygame.transform.scale(SPLASH_IMAGE[2], (100, 100)), pygame.transform.scale(SPLASH_IMAGE[3], (100, 100))]
# quotes
QUOTES = [pygame.image.load(os.path.join('assets', 'quote-1.png')), pygame.image.load(os.path.join('assets', 'quote-2.png')),
pygame.image.load(os.path.join('assets', 'quote-3.png')), pygame.image.load(os.path.join('assets', 'quote-4.png')),
pygame.image.load(os.path.join('assets', 'quote-5.png'))]
splash_stage = 0
wave_x = 900
# button vars
# light shade of the button
color_light = (170, 170, 170)
color_dark = (100, 100, 100)
# defining a font
smallfont = pygame.font.SysFont('Corbel', 35)
# rendering a text written in
# this font
text = smallfont.render('play', True, color_dark)
text_end_quit = smallfont.render('exit', True, color_dark)
text_end_play_again = smallfont.render('again', True, color_dark)
def draw_window(list : LinkedList.LinkedList(), shark, count, bob, shark_hp, score):
global splash_stage
curr = list.head
for i in range(3):
WIN.blit(OCEAN_IMAGE, (OCEAN[i], 0))
for i in range(shark_hp):
WIN.blit(HEART_IMAGE, (HEARTS[i], 10))
while curr:
WIN.blit(curr.data.image, (curr.data.get_x(), curr.data.get_y()))
curr.data.set_x(-BACKGROUND_SPEED)
if curr.data.get_x() < -10:
curr.data.set_x(random.randint(10, 910) + WIDTH)
curr.data.hitbox.y = (random.randint(10, 450))
if count % 10 == 0:
if bob:
curr.data.set_y(-5)
bob = False
else:
curr.data.set_y(5)
bob = True
curr = curr.next
for i in range(25):
WIN.blit(WAVES[splash_stage // 7], (wave_x, i * 50))
WIN.blit(shark.image, (shark.get_x(), shark.get_y()))
WIN.blit(SPLASH[splash_stage // 7], (shark.get_x() - 55, shark.get_y()))
if splash_stage == 24:
splash_stage = 0
else:
splash_stage += 1
# SHOW_AVATAR_HEALTH = FONT.render("Health: " + str(shark_hp), 1, WHITE)
# WIN.blit(SHOW_AVATAR_HEALTH, (10,10))
SHOW_SCORE = FONT.render("Score: " + str(score), 1, WHITE)
WIN.blit(SHOW_SCORE, (700, 10))
pygame.display.update()
def handle_avatar_movement(sprite: sprites.Sprites, keys_pressed):
if keys_pressed[pygame.K_UP] and sprite.get_y() > 0: # UP
sprite.set_y(-PLAYER_SPEED)
if keys_pressed[pygame.K_DOWN] and sprite.get_y() + sprite.hitbox.height < HEIGHT: # DOWN
sprite.set_y(PLAYER_SPEED)
if keys_pressed[pygame.K_LEFT] and sprite.get_x() > 0: # LEFT
sprite.set_x(-PLAYER_SPEED)
if keys_pressed[pygame.K_RIGHT] and sprite.get_x() + sprite.hitbox.width < WIDTH: # RIGHT
sprite.set_x(PLAYER_SPEED)
def collision(shark, list, vulnerable):
curr = list.head.next
while curr and vulnerable:
if shark.hitbox.colliderect(curr.data.hitbox):
pygame.event.post(pygame.event.Event(COLLISION))
print("Hit!")
vulnerable = False
curr = curr.next
def main():
run = True
main_menu = True
end_menu = False
vulnerable = True
start = True
bob = False
time = 0
count = 0
counter = 0
score = 0
shark_hp = 5
trash_list = LinkedList.LinkedList()
clock = pygame.time.Clock()
quote = QUOTES[random.randint(0,4)]
shark = sprites.Sprites(SHARK, pygame.Rect(10, 300, SHARK_WIDTH, SHARK_HEIGHT))
for i in range(4):
trash_list.push(sprites.Sprites(BAGS[random.randint(0, 11)], pygame.Rect(random.randint(600, 1800), random.randint(10, 500), 50, 50)))
trash_list.push(sprites.Sprites(BOTTLES[random.randint(0, 3)], pygame.Rect(random.randint(600, 1800), random.randint(10, 500), 10, 10)))
trash_list.push(sprites.Sprites(WRAPPERS[random.randint(0, 3)], pygame.Rect(random.randint(600, 1800), random.randint(10, 500), 10, 10)))
trash_list.push(sprites.Sprites(FORK[random.randint(0, 3)], pygame.Rect(random.randint(600, 1800), random.randint(10, 500), 10, 10)))
trash_list.push(sprites.Sprites(SODA[random.randint(0, 3)], pygame.Rect(random.randint(600, 1800), random.randint(10, 500), 10, 10)))
pygame.display.set_caption("PISTRIS")
while main_menu:
WIN.blit(OCEAN_IMAGE, (0, 0))
WIN.blit(ISLAND, (WIDTH/2 - 350/2, HEIGHT / 2 - 250))
WIN.blit(CLAM_IMAGE, (WIDTH/2 - 25, HEIGHT / 2 - 70))
pygame.draw.rect(WIN, color_light, [WIDTH / 2 - 70, HEIGHT / 2 - 45/2 , 140, 40])
WIN.blit(text, (WIDTH / 2 - 70/2 + 1, HEIGHT / 2 - 45/2))
# stores the (x,y) coordinates into
# the variable as a tuple
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.QUIT()
if event.type == pygame.MOUSEBUTTONDOWN:
if WIDTH / 2 - 70 <= mouse[0] <= WIDTH / 2 + 70 and HEIGHT / 2 - 45/2 <= mouse[1] <= HEIGHT / 2 + 45/2:
main_menu = False
pygame.display.update()
while run:
global wave_x
clock.tick(FPS)
time += 1
count += 1
wave_x -= BACKGROUND_SPEED * 2
if wave_x < -50:
wave_x = 900
if start:
mixer.music.load('Da_Music.mp3')
mixer.music.play(-1)
start = False
if vulnerable is False:
counter += 1
if counter % 30 == 0:
vulnerable = True
if time % 25 == 0:
score += 1
# Scrolling background, resets background ahead if background hits fully offscreen
for i in range(3):
OCEAN[i] -= BACKGROUND_SPEED
if OCEAN[i] == -900:
OCEAN[i] = 1800
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == COLLISION:
shark_hp -= 1
vulnerable = False
if shark_hp <= 0:
end_menu = True
keys_pressed = pygame.key.get_pressed()
handle_avatar_movement(shark, keys_pressed)
collision(shark, trash_list, vulnerable)
draw_window(trash_list, shark, count, bob, shark_hp, score)
while end_menu:
mixer.music.stop()
# fills the screen with a color
WIN.blit(OCEAN_IMAGE, (0, 0))
pygame.draw.rect(WIN, color_light, [WIDTH / 2 + 40, HEIGHT / 2 - 45/2 , 140, 40])
pygame.draw.rect(WIN, color_light, [WIDTH / 2 - 180, HEIGHT / 2 - 45/2 , 140, 40])
WIN.blit(text_end_quit, (WIDTH / 2 + 40 + 75/2 + 5, HEIGHT / 2 - 45/2))
WIN.blit(text_end_play_again, (WIDTH / 2 - 180 + 75/2 - 5, HEIGHT / 2 - 45/2))
WIN.blit(CRAB, (WIDTH/2 - 150, HEIGHT / 2 - 25))
WIN.blit(quote, (10, 10))
# stores the (x,y) coordinates into
# the variable as a tuple
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.QUIT()
if event.type == pygame.MOUSEBUTTONDOWN:
if WIDTH / 2 + 40 <= mouse[0] <= WIDTH / 2 + 180 and HEIGHT / 2 - 45/2 <= mouse[1] <= HEIGHT / 2 + 45 / 2:
pygame.QUIT()
if WIDTH / 2 - 180 <= mouse[0] <= WIDTH / 2 - 40 and HEIGHT / 2 - 45/2 <= mouse[1] <= HEIGHT / 2 + 45 / 2:
main()
pygame.display.update()
pygame.QUIT()
if __name__ == "__main__":
main()