-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSnoSnow.py
466 lines (364 loc) · 14.7 KB
/
SnoSnow.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
import pygame
from pygame.locals import *
from pygame.math import Vector2
from pygame import mixer
import time
import random
import math
#0 = Menu, 1 = Game, 2 = Quit
game_state = 0
score = 0
score_int = 0
max_score = 0
entities_alive = []
class Player():
global game_state
#Transform
position = pygame.Vector2()
scale = pygame.Vector2()
#Physics
velocity_y = 0
gravity_scale = 3000
jump_force = 2000
speed = 400
is_grounded = False
#Graphics
player_sprite = 0
rotation = 0
rot_speed = 400
scale_speed = 3
player_collider = 0
def __init__(self, desired_scale_x, desired_scale_y, Width, Height):
self.scale.x = desired_scale_x
self.scale.y = desired_scale_y
self.scalar = 50
self.player_sprite = pygame.sprite.Sprite()
self.player_sprite.image = pygame.image.load("Data/Textures/Snowball.png").convert_alpha()
self.player_sprite.rect = self.player_sprite.image.get_rect()
self.player_sprite.image = pygame.transform.scale(self.player_sprite.image, (int(self.scale.x), int(self.scale.y)))
self.position.x = Width / 2
self.position.y = Height / 2
self.scalar = self.scale.x
def move(self, keys, dt):
global score_int
global score
global max_score
if(keys[0]):
self.position.x -= 0.01 * dt * self.speed
self.rotation += 0.01 * dt * self.rot_speed
if(keys[1]):
self.position.x += 0.01 * dt * self.speed
self.rotation -= 0.01 * dt * self.rot_speed
if(keys[2] and self.is_grounded):
self.velocity_y -= 0.01 * dt * self.jump_force
self.velocity_y += 0.00001 * dt * self.gravity_scale
self.position.y += self.velocity_y * dt
self.player_sprite.rect.topleft = self.position.x, self.position.y
#Scale The Snowball
if(self.scalar < 12):
game_state = 0
if(score > max_score):
max_score = score
#score = 0
#score_int = 0
else:
self.scalar -= 0.01 * dt * self.scale_speed
def draw(self, screen, color, dt, colliders):
img_copy = pygame.transform.scale(self.player_sprite.image, (int(self.scalar), int(self.scalar)))
img_copy = pygame.transform.rotate(img_copy, self.rotation)
self.collisions(colliders, img_copy.get_height())
screen.blit(img_copy, (self.position.x - int(img_copy.get_width() / 2), self.position.y - int(img_copy.get_height() / 2)))
def collisions(self, colliders, scale):
#Top of box
self.is_grounded = False
for i in range(len(colliders)):
if(self.position.y - int(scale / 2) >= colliders[i].top - scale and colliders[i].typeof == "environment"):
self.is_grounded = True
self.position.y = colliders[i].top - int(scale / 2) + 5
self.velocity_y = 0
if(self.position.x + int(self.player_sprite.image.get_width() / 2) >= 720):
self.position.x = 720 - int(self.player_sprite.image.get_width() / 2)
if(self.position.x - int(self.player_sprite.image.get_width() / 2) <= 0):
self.position.x = 0 + int(self.player_sprite.image.get_width() / 2)
class Box_Collider():
position = Vector2()
scale = Vector2()
top = 0
right = 0
left = 0
down = 0
typeof = "default"
def __init__(self, desired_position_x, desired_position_y, desired_scale_x, desired_scale_y, typeof):
self.position.x = desired_position_x
self.position.y = desired_position_y
self.scale.x = desired_scale_x
self.scale.y = desired_scale_y
self.typeof = typeof
self.top = self.position.y - self.scale.y / 2
self.right = self.position.x + self.scale.x / 2
self.left = self.position.x - self.scale.x / 2
self.down = self.position.y + self.scale.y / 2
def draw(self, screen, color):
pygame.draw.rect(screen, color, (self.position.x, self.position.y, self.scale.x, self.scale.y))
class Entity():
tag = "Default"
bottom = 0
entity_color = (0, 0, 0)
sound = 0
radius = 0
right = 0
left = 0
def move(self, dt):
self.position.y += dt * self.speed
self.bottom = self.position.y + (self.radius * 2)
self.right = self.position.x + (self.radius * 2)
self.left = self.position.x
def collisions(self, enemies):
if(self.position.y > 390):
enemies.remove(self)
enemies = enemies[:-1]
self.sound.play()
def initialiser(self, color, sfx_path):
self.entity_color = color
self.sound = mixer.Sound(sfx_path)
def draw(self, screen):
pygame.draw.circle(screen, self.entity_color, (self.position.x, self.position.y), self.radius)
class Enemy(Entity):
def __init__(self):
self.speed = random.randrange(1, 5)
self.position = Vector2()
self.position.x = random.randrange(0, 720)
self.position.y = 0
self.initialiser((102, 107, 102),"Data/Sounds/RockHit.wav")
self.tag = "enemy"
self.radius = random.randrange(10, 30)
class Snowball(Entity):
def __init__(self):
self.speed = random.randrange(1, 5)
self.position = Vector2()
self.position.x = random.randrange(0, 720)
self.position.y = 0
self.initialiser((255, 255, 255),"Data/Sounds/SnowballHit.wav")
self.tag = "snowball"
self.radius = random.randrange(10, 30)
class Spawner():
global game_state
global entities_alive
total_enemies_spawned = 0
time_elapsed = 0
screen = 0
time_between_spawns = 100
concurrent_enemys = 0
sound = 0
def __init__(self, screen):
self.screen = screen
self.sound = mixer.Sound("Data/Sounds/SnowballPowerup.wav")
def check_for_player(self, player, scalar):
global game_state
global score_int
global score
global max_score
for i in range(len(entities_alive)):
try:
if(entities_alive[i].bottom > 380 + 50 - scalar):
if(entities_alive[i].position.x > player.position.x - player.scale.x and entities_alive[i].position.x < player.position.x + player.scale.x):
if(entities_alive[i].tag == "enemy"):
game_state = 0
if(score > max_score):
max_score = score
#score = 0
#score_int = 0
else:
score += 5
player.scalar = 50
self.sound.play()
entities_alive.remove(entities_alive[i])
except:
pass
def set_time_between_spawns(self, time_between_spawns):
self.time_between_spawns = time_between_spawns
def draw_enemies(self, dt):
for i in range(len(entities_alive)):
try:
entities_alive[i].draw(self.screen)
entities_alive[i].move(dt)
entities_alive[i].collisions(entities_alive)
except:
pass
def timer(self, dt):
self.time_elapsed += dt
def spawner(self):
if(self.time_elapsed >= self.time_between_spawns):
random_int = random.randint(0, 4)
if(random_int != 0 and self.concurrent_enemys < 3):
entity = Enemy()
self.concurrent_enemys += 1
else:
entity = Snowball()
self.concurrent_enemys = 0
entities_alive.append(entity)
self.time_elapsed = 0
class Particle():
def __init__(self):
self.position = Vector2()
self.position.x = random.randrange(0, 720)
self.position.y = 0
self.size = random.randrange(0, 15)
self.color = (255, 255, 255)
self.speed = random.randrange(20, 50)
def move(self, dt):
self.position.y += 0.01 * dt * self.speed
def draw(self, screen):
pygame.draw.rect(screen, self.color, (self.position.x, self.position.y,self.size,self.size))
def collision(self, particles):
if(self.position.y > 390):
particles.remove(self)
class Main():
global game_state
previous_frame_time = 0
dt = 0
elapsed_time = 0
time_between_spawns = 100
def calculate_deltatime(self):
self.dt = time.time() - self.previous_frame_time
self.dt *= 60
self.previous_frame_time = time.time()
def difficulty(self):
self.elapsed_time += self.dt
if(self.elapsed_time > 1000):
self.time_between_spawns /= 1.45
self.elapsed_time = 0
def handle_inputs(self, keys, event):
if(event.type == pygame.KEYDOWN):
if(event.key == K_a):
keys[0] = True
if(event.key == K_d):
keys[1] = True
if(event.key == K_w):
keys[2] = True
if(event.type == pygame.KEYUP):
if(event.key == K_a):
keys[0] = False
if(event.key == K_d):
keys[1] = False
if(event.key == K_w):
keys[2] = False
def setup_pygame(self, title, width, height):
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption(title)
favicon = pygame.image.load("Data/Textures/Favicon.png").convert_alpha()
pygame.display.set_icon(favicon)
pygame.init()
return screen
def update_score(self, screen, text):
global score_int
global score
score += self.dt / 100
score_int = int(score)
score_text = text.render("SCORE: " + str(score_int),True,(0,0,0))
screen.blit(score_text, (10, 10))
def draw_colliders(self, colliders, screen, color, width, height):
for i in range(len(colliders)):
colliders[i].draw(screen, color, width, height)
def reset_state(self):
self.previous_frame_time = 0
self.dt = 0
self.elapsed_time = 0
self.time_between_spawns = 100
self.score_int = 0
def game(self, screen, font, WIDTH, HEIGHT):
global game_state
WHITE = (255, 255, 255)
BLACK = (0,0,0)
# A D Space
self.previous_frame_time = time.time()
keys = [False, False, False]
player = Player(40, 40, WIDTH, HEIGHT)
colliders = []
#Constant Sprites
foreground = pygame.sprite.Sprite()
foreground.image = pygame.image.load("Data/Textures/Foreground.png").convert_alpha()
foreground.rect = foreground.image.get_rect()
foreground.rect.topleft = 0, HEIGHT - 480
foreground.image = pygame.transform.scale(foreground.image, (720, 480))
foreground_collider = Box_Collider(WIDTH / 2, HEIGHT - 20, 720, 120, "environment")
colliders.append(foreground_collider)
spawner = Spawner(screen)
particles = []
time_elapsed = 0
while(game_state == 1):
screen.fill(WHITE)
for event in pygame.event.get():
if(event.type == pygame.QUIT):
game_state = 2
self.handle_inputs(keys, event)
self.difficulty()
self.calculate_deltatime()
screen.blit(foreground.image, foreground.rect)
time_elapsed += self.dt
if(time_elapsed > 10):
part = Particle()
particles.append(part)
time_elapsed = 0
for i in range(len(particles)):
try:
particles[i].move(self.dt)
particles[i].draw(screen)
particles[i].collision(particles)
except:
pass
player.move(keys, self.dt)
player.draw(screen, BLACK, self.dt, colliders)
spawner.spawner()
spawner.set_time_between_spawns(self.time_between_spawns)
spawner.timer(self.dt)
spawner.draw_enemies(self.dt)
spawner.check_for_player(player, player.scalar)
self.update_score(screen, font)
pygame.display.update()
def menu(self, screen, font, WIDTH, HEIGHT):
global game_state
COLOR = (224, 190, 108)
sound = pygame.mixer.Sound("Data/Sounds/Start.wav")
play_text = font.render("PLAY", True, (255,255,255))
play_text_y_offset = 0
score_text = font.render("HIGH SCORE: " + str(int(max_score)), True, (255,255,255))
tutorial_text = font.render("You're Melting!", True, (255,255,255))
tutorial_text_2 = font.render("Collect Snow And Avoid Moving Rocks!", True, (255,255,255))
direc = 1
while game_state == 0:
screen.fill(COLOR)
screen.blit(play_text, (WIDTH/2 - play_text.get_width() / 2, HEIGHT/2 - play_text.get_height() / 2 + play_text_y_offset))
screen.blit(score_text, (WIDTH/2 - score_text.get_width() / 2, HEIGHT/2 - score_text.get_height() / 2 + 150))
play_text_y_offset = math.sin(time.time() * 5) * 5 - 25
for event in pygame.event.get():
if(event.type == pygame.QUIT):
game_state = 2
if(pygame.mouse.get_pos()[0] < WIDTH/2 + play_text.get_width() / 2 and pygame.mouse.get_pos()[0] > WIDTH/2 - play_text.get_width() / 2):
if(pygame.mouse.get_pos()[1] < HEIGHT/2 + play_text.get_height() / 2 + play_text_y_offset and pygame.mouse.get_pos()[1] > HEIGHT/2 - play_text.get_height() / 2 + play_text_y_offset):
if(event.type == pygame.MOUSEBUTTONDOWN):
game_state = 1
sound.play()
pygame.display.update()
def __init__(self):
global game_state
global score
global score_int
global entities_alive
while game_state != 2:
WIDTH, HEIGHT = 720, 480
screen = self.setup_pygame("Sno Snow", WIDTH, HEIGHT)
font = pygame.font.Font("Data/Fonts/Inter.ttf", 32)
if(game_state == 0):
self.menu(screen, font, WIDTH, HEIGHT)
if(game_state == 1):
self.previous_frame_time = time.time()
self.game(screen, font, WIDTH, HEIGHT)
sound = pygame.mixer.Sound("Data/Sounds/Die.wav")
sound.play()
self.reset_state()
score = 0
score_int = 0
entities_alive.clear()
entities_alive = []
game = Main()