-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rider.py
761 lines (673 loc) · 36.7 KB
/
Rider.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#PROJECT
import pygame
import random
from pygame.math import Vector2
from Classes import *
from Settings import *
from os import path
from math import atan2, degrees, pi, sin, cos, radians
import time
class Game(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
pygame.init()
self.turnDown = False
self.clock = pygame.time.Clock()
self.running = True
self.load_data()
def load_data(self):
#Load high score
self.dir = path.dirname(__file__)#Locate game directory
with open(path.join(self.dir, 'highscore.txt'), 'rb') as f:
try:
self.highscore = int(f.read())
except:
self.highscore = 0
def play_Smusic(self):
song_queue = ["TheFatRat - Unity.mp3", "The Cranberries - Zombie (Lost Sky Remix).mp3",
"Monody.mp3", "Xenogenesis.mp3"]
pygame.mixer.music.load(random.choice(song_queue))
pygame.mixer.music.play()
#pygame.mixer.music.set_volume(0.0)
def straightPath(self):
# Creates first path block which others build from.
self.my_Path = Path(self, 175, 200)
# After each object is created, it is added to the path group and the
# all sprites group in order to facilitate detecting collisions,
# and so that they can be drawn easily, all together onto the screen.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# The for loop will iterate 250 times creating 250 path objects.
for count in range(250):
# The width of each path is added to the previous x coordinate
# so that the next object appears exactly on the right of the
# previous object, forming a level line.
self.my_Path = Path(self, self.my_Path.rect.x + self.my_Path.width, self.my_Path.rect.y)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
def manualCurveDown(self):
# Creates 5 path objects going down at a -1/2 gradient.
for count in range(5):
self.my_Path = Path(self, self.my_Path.rect.x + 2, self.my_Path.rect.y + 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates 4 path objects going down at a -1/3 gradient.
for count in range(4):
self.my_Path = Path(self, self.my_Path.rect.x + 3, self.my_Path.rect.y + 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates 3 path objects going down at a -1/4 gradient.
for count in range(3):
self.my_Path = Path(self, self.my_Path.rect.x + 4, self.my_Path.rect.y + 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates 5 level path objects at the bottom of the curve.
for count in range(5):
self.my_Path = Path(self, self.my_Path.rect.x + 1, self.my_Path.rect.y)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
def manualCurveUp(self):
# Creates 5 level path objects at the bottom of the curve.
for count in range(5):
self.my_Path = Path(self, self.my_Path.rect.x + 1, self.my_Path.rect.y)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates 3 path objects going up at a 1/4 gradient.
for count in range(3):
self.my_Path = Path(self, self.my_Path.rect.x + 4, self.my_Path.rect.y - 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates 4 path objects going up at a 1/3 gradient.
for count in range(4):
self.my_Path = Path(self, self.my_Path.rect.x + 3, self.my_Path.rect.y - 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates 5 path objects going up at a 1/2 gradient.
for count in range(5):
self.my_Path = Path(self, self.my_Path.rect.x + 2, self.my_Path.rect.y - 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
def pathGap(self):
# The for loop will iterate 50 times creating 125 path objects.
for count in range(50):
# Creates a new path object directly on the right of the previous
# object by adding the width to the x coordinate. It also adds
# the height of the object to the y coordinate to make each object
# appear slightly above the previous object, forming a ramp.
self.my_Path = Path(self, self.my_Path.rect.x + self.my_Path.rect.width, self.my_Path.rect.y - 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Adds 60 pixels to the previous path object, creating a gap.
self.my_Path = Path(self, self.my_Path.rect.x + 60, self.my_Path.rect.y)
# The for loop iterates 30 times creating a short straight path,
# giving the player something to land on.
for count in range(30):
self.my_Path = Path(self, self.my_Path.rect.x + self.my_Path.width, self.my_Path.rect.y)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
def downRamp(self):
# Creates jump just before downwards ramp.
for count in range(30):
self.my_Path = Path(self, self.my_Path.rect.x + self.my_Path.rect.width, self.my_Path.rect.y - 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# The for loop will iterate 125 times creating 125 path objects.
for count in range(125):
# Creates a new path object directly on the right of the previous
# object by adding the width to the x coordinate. It also subtracts
# the height of the object to the y coordinate to make each object
# appear slightly lower than the previous object, forming a ramp.
self.my_Path = Path(self, self.my_Path.rect.x + self.my_Path.rect.width, self.my_Path.rect.y + 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
def upRamp(self):
# The for loop will iterate 125 times creating 125 path objects.
for count in range(125):
# Creates a new path object directly on the right of the previous
# object by adding the width to the x coordinate. It also adds
# the height of the object to the y coordinate to make each object
# appear slightly above the previous object, forming a ramp.
self.my_Path = Path(self, self.my_Path.rect.x + self.my_Path.rect.width, self.my_Path.rect.y - 1)
# Adds each object to the sprite groups.
self.path_group.add(self.my_Path)
self.all_sprites_group.add(self.my_Path)
# Creates downwards compatible path components in a random order.
def createPath_d(self):
# Towards the beginning the path should be easy with no gaps.
if self.my_Path.rect.x < 5000:
# Stores the compatible path components in a list.
paths = [self.manualCurveDown, self.manualCurveUp, self.downRamp]
# Chooses a random item from the list and executes the selected method.
random.choice(paths)()
# After 5000 pixels, more gaps should be introduced.
else:
# Stores the compatible path components in a list.
paths = [self.manualCurveDown, self.manualCurveUp, self.downRamp, self.pathGap]
# Chooses a random item from the list and executes the selected method.
random.choice(paths)()
# Creates upwards compatible path components in a random order.
def createPath_u(self):
# Towards the beginning the path should be easy with no gaps.
if self.my_Path.rect.x < 3000:
# Stores the compatible path components in a list.
paths = [self.manualCurveDown, self.manualCurveUp, self.upRamp]
# Chooses a random item from the list and executes the selected method.
random.choice(paths)()
# After 3000 pixels, more gaps should be introduced.
else:
# Stores the compatible path components in a list.
paths = [self.manualCurveDown, self.manualCurveUp, self.upRamp, self.pathGap]
# Chooses a random item from the list and executes the selected method.
random.choice(paths)()
def createCollectables(self):
xpos = self.my_Path.rect.x#random.randrange(spaceInFront) + 310
#xpos = 300
ypos = random.randrange(self.my_Path.rect.y - 45)#random.randrange(HEIGHT - 10)
# Stores the collectables in a list.
collectable_list = [Spin, TurboBoost, IcePath, Giant, Small, Sports, Random]
# Gets a collectable chosen at random from the list.
collectable = random.choice(collectable_list)
# Instantiates the random collectable.
self.my_Collectable = collectable(self, xpos, ypos)
# Adds the collectables to the appropriate groups.
if collectable == Spin:
self.spin_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
elif collectable == TurboBoost:
self.turbo_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
elif collectable == IcePath:
self.ice_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
elif collectable == Giant:
self.giant_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
elif collectable == Small:
self.small_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
elif collectable == Sports:
self.sports_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
elif collectable == Random:
self.random_collectable_group.add(self.my_Collectable)
self.all_sprites_group.add(self.my_Collectable)
def new_game(self):
#self.play_Smusic() #Plays the shuffled song
self.score = 0
self.flip_score = 0
self.degs = 0
self.insx = 180 #Instructions appear each round
# Create a sprite group of all sprites
self.all_sprites_group = pygame.sprite.Group()
# Create a sprite group of the paths
self.path_group = pygame.sprite.Group()
# Create a sprite group for each collectable.
self.spin_collectable_group = pygame.sprite.Group()
self.turbo_collectable_group = pygame.sprite.Group()
self.ice_collectable_group = pygame.sprite.Group()
self.giant_collectable_group = pygame.sprite.Group()
self.small_collectable_group = pygame.sprite.Group()
self.sports_collectable_group = pygame.sprite.Group()
self.random_collectable_group = pygame.sprite.Group()
# Timers for collectables when not in use.
self.start_spin_time = 9999999999
self.start_turbo_time = 9999999999
self.start_ice_time = 9999999999
self.start_giant_time = 9999999999
self.start_small_time = 9999999999
self.start_sports_time = 9999999999
self.start_random_time = 9999999999
# Creates list for displaying accurate collectables.
self.active_collectables_list = []
# Boolean variables for collectables.
self.turbo = False
self.ice = False
self.random = False
# Instantiates the player class which will act as two wheels.
self.my_Player1 = Player(self, 280, 50)
self.my_Player2 = Player(self, 295, 50)
# Adds the player objects to the sprite group.
self.all_sprites_group.add(self.my_Player1)
self.all_sprites_group.add(self.my_Player2)
# Instantiated here and in update.
self.player1_path_collision_group = pygame.sprite.spritecollide(self.my_Player1, self.path_group, False)
self.player2_path_collision_group = pygame.sprite.spritecollide(self.my_Player2, self.path_group, False)
# Instantiates the Playerbody class which will hold the car image.
self.my_Playerbody = Playerbody(self)
# Adds object to sprite group.
self.all_sprites_group.add(self.my_Playerbody)
# CREATEs collision collctable groups
#self.player_spin_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.spin_collectable_group, True)
#self.player_turbo_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.turbo_collectable_group, True)
#self.player_ice_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.ice_collectable_group, True)
#self.player_giant_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.giant_collectable_group, True)
#self.player_small_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.small_collectable_group, True)
#self.player_sports_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.sports_collectable_group, True)
#self.player_random_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.random_collectable_group, True)
###DELETE IF NEEDED
#self.right_collision_group = pygame.sprite.spritecollide(self.my_Player.rect.bottomleft, self.path_group, False)
# SPAWN PATH
# Initial straight path segment is called so player starts
# level and comfortably always.
self.straightPath()
# Will create 100 random path components.
for count in range(100):
# If path is too low, path will know to start building downwards.
if self.my_Path.rect.y <= 200:
# turnDown will be true until path becomes too low.
self.turnDown = True
# If path is too high, path will know to start building upwards.
elif self.my_Path.rect.y >= 350:
# turnDown will be false until path becomes too high.
self.turnDown = False
# When turnDown is true, path should go downwards.
if self.turnDown == True:
# Spawns random downwards components.
self.createPath_d()
else:
# Spawns random upwards components.
self.createPath_u()
# Ever 3 path components create a collectable.
if count % 3 == 0 and self.my_Path.rect.y > 45:
self.createCollectables()
#Runs game?
self.run()
def run(self):
self.playing = True
while self.playing == True:
self.clock.tick(60)
self.events()
self.update()
self.draw()
def update(self):
# Update each sprite each loop
self.all_sprites_group.update()
#print(self.my_Playerbody.rot)
#### Basically, Playerbody's rect enlarges to create rotation appearance (thats why we set colourkey).
#### Therefore the center of Playerbody becomes higher and car starts floating if you rotate before landing
#### at the start. To combat this, we worked out the height of the rect's center as it increases with rotation,
#### u, and subtract this so that the center is consant and car does not appear to be floating.
###########rotated_in_air = radians(self.my_Playerbody.rot)
##########print(rotated_in_air)
##########u = (42 * math.sin(rotated_in_air) + 26 * math.cos(rotated_in_air)) // 2
#print(u)
###########self.my_Playerbody.rect.center = ((x_coordinate_of_center + 10),(y_coordinate_of_center + u - 8))
###self.my_Playerbody.rect.x = (self.my_Player1.rect.x)###(x_coordinate_of_center)
###self.my_Playerbody.rect.y = (y_coordinate_of_center - 5)
# Find difference of x and y between both wheels
# in order to find the gradient.
dx = self.my_Player1.rect.x - self.my_Player2.rect.x
# I subtract the second wheel from the first here as the
# y coordinates are flipped in pygame
dy = self.my_Player2.rect.y - self.my_Player1.rect.y
# Finds the angle in radians between both wheels.
rads = atan2(dy,dx)
# Converts angle to degrees.
self.degs = degrees(rads)
#print(self.degs)
#ROLLL DOWN HILL IF ANGLE
#if degs > 120 and degs < 180:
#if self.degs == 180:
#self.my_Player1.g_Vel = 1
# self.my_Path.acc = PLAYER_
# print("TEST")
#if player,rect.x + 20 != mypath
#IF BOTTOM RIGHT DOESNT HAVE COLLISION PLAYER CAN JUMP
# Path and player collisions
#for foo in self.path_group:
# self.collision = self.my_Player.rect.collidepoint(foo.rect.topright)
#if self.collision != 0:
# print(self.collision)
#self.collision = pygame.sprite.collide_rect(self.my_Player, self.my_Path)
#print(self.collision)
# PLAYER-PATH COLLISIONS
# Creates a group of the path objects the player collides with.
# False is the last argument to indicate that the path objects should
# not destroy on collision.
self.player1_path_collision_group = pygame.sprite.spritecollide(self.my_Player1, self.path_group, False)
self.player2_path_collision_group = pygame.sprite.spritecollide(self.my_Player2, self.path_group, False)
###DELETE IF NEEDED
#self.right_collision_group = pygame.sprite.spritecollide(self.my_Player.rect.bottomleft, self.path_group, False)
#if self.my_Player.rect.collidepoint(self.my_Player.rect.bottomleft):
# print("test")
# If the back wheel and path objects collide...
if self.player1_path_collision_group:
# The back wheel's gravity velocity becomes 0.
self.my_Player1.g_Vel = 0
# For every object in the collision group...
for foo in self.player1_path_collision_group:
# The bootom edge of the wheel should remain on the top
# edge of the path objects.
self.my_Player1.rect.bottom = foo.rect.top
#####else: #If no collision...
#self.my_Path.acc = 0 #Player shouldn't accelerate when not colliding:
#####if self.my_Player1.change_in_y < 0: #if no collision + going up...
#self.my_Player.current_y = 0
#self.my_Player.last_y = 0 #makes change_in_y positive.
#####self.my_Player1.g_Vel = self.my_Path.vel #-8 #player should go up.
#####self.my_Player2.g_Vel = self.my_Path.vel
#Player should move down quicker
#if change in y > 0 allow movement if just collided?
# If the front wheel and path objects collide...
if self.player2_path_collision_group:
# The front wheel's gravity velocity becomes 0.
self.my_Player2.g_Vel = 0
# For every object in the collision group...
for foo in self.player2_path_collision_group:
# The bootom edge of the wheel should remain on the top
# edge of the path objects.
self.my_Player2.rect.bottom = foo.rect.top
# If the front wheel is not colling with the path...
else:
# If the back wheel has upwards velocity...
if self.my_Player1.change_in_y < 0:
# Both wheels' gravity velocity should be equal to their velocity.
self.my_Player1.g_Vel = self.my_Path.vel
self.my_Player2.g_Vel = self.my_Path.vel
# If both wheels on ground...
if self.player1_path_collision_group and self.player2_path_collision_group:
# Rotates body to give same gradient as wheels.
self.my_Playerbody.new_image = pygame.transform.rotate(self.my_Playerbody.image, (self.degs - 180))
# Makes centre position equal to previous centre position. (new_image has
# already been updated in Playerbody class to have the same centre).
self.my_Playerbody.rect = self.my_Playerbody.new_image.get_rect()
#x_coordinate_of_center = (self.my_Player1.rect.x + self.my_Player2.rect.x) // 2
#y_coordinate_of_center = (self.my_Player1.rect.y + self.my_Player2.rect.y) // 2
# If player on its back then round ends.
if self.my_Playerbody.rot > 90 and self.my_Playerbody.rot < 270:
self.playing = False
# Makes the car's initial air rotation the same as the path's last gradient.
self.my_Playerbody.rot = 180 + self.degs
if self.my_Playerbody.rot > 90 and self.my_Playerbody.rot < 270:
self.flip_score += 1
#print("test")
# Calculates and stores centre point of wheels the wheels.
x_coordinate_of_center = (self.my_Player1.rect.x + self.my_Player2.rect.x) // 2
y_coordinate_of_center = (self.my_Player1.rect.y + self.my_Player2.rect.y) // 2
# Set the center of the body to the middle point between both wheels.
self.my_Playerbody.rect.center = ((x_coordinate_of_center + 10),(y_coordinate_of_center + 8))
# PLAYER-COLLECTABLE COLLISIONS
# SPIN
# Creates the player-spin collision group.
self.player_spin_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.spin_collectable_group, True)
# If the player has collided with the collectable...
if self.player_spin_collectable_collision_group:
# Record current time.
self.start_spin_time = time.time()
# Set rotation speed to 25.
self.my_Playerbody.rot_speed = 25
# If SPIN! is not already being displayed then it should be.
if "SPIN!" not in self.active_collectables_list:
self.active_collectables_list.append("SPIN!")
# When 10 seconds have passed.
if time.time() > (self.start_spin_time + 10):
# Make rotation speed back to normal.
self.my_Playerbody.rot_speed = ROT_SPEED
# Reset the timer.
self.start_spin_time = 9999999999
# Remove from display.
self.active_collectables_list.remove("SPIN!")
# TURBO
# Creates the player-turbo collision group.
self.player_turbo_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.turbo_collectable_group, True)
# If the player has collided with the collectable...
if self.player_turbo_collectable_collision_group:
# Record current time.
self.start_turbo_time = time.time()
# Set the global boolean variable for turbo equal to True
# so that it can be accessed in other parts of the program.
self.turbo = True
# If TURBO! is not already being displayed then it should be.
if "TURBO!" not in self.active_collectables_list:
self.active_collectables_list.append("TURBO!")
# When 10 seconds have passed.
if time.time() > (self.start_turbo_time + 10):
# Set turbo to False.
self.turbo = False
# Reset the timer.
self.start_turbo_time = 9999999999
# Remove from display.
self.active_collectables_list.remove("TURBO!")
# ICE
# Creates the player-ice collision group.
self.player_ice_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.ice_collectable_group, True)
# If the player has collided with the collectable...
if self.player_ice_collectable_collision_group:
# Record current time.
self.start_ice_time = time.time()
# Set the global boolean variable for ice equal to True
# so that it can be accessed in other parts of the program.
self.ice = True
# Display collectable in use to player.
if "ICE!" not in self.active_collectables_list:
self.active_collectables_list.append("ICE!")
# When 10 seconds have passed.
if time.time() > (self.start_ice_time + 10):
self.ice = False
# Reset the timer
self.start_ice_time = 9999999999
# Remove display once collectable finished.
self.active_collectables_list.remove("ICE!")
# GIANT
# Creates the player-giant collision group.
self.player_giant_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.giant_collectable_group, True)
# If the player has collided with the collectable...
if self.player_giant_collectable_collision_group:
# Record current time.
self.start_giant_time = time.time()
# Change the image of the Playerbody sprite to a giant car.
self.my_Playerbody.image = pygame.image.load("giant_car.png")
# Display collectable in use to player.
if "GIANT!" not in self.active_collectables_list:
self.active_collectables_list.append("GIANT!")
# When 10 seconds have passed.
if time.time() > (self.start_giant_time + 10):
# Change the Playerbody sprite image back to its original one.
self.my_Playerbody.image = pygame.image.load("car.png")
# Reset the timer.
self.start_giant_time = 9999999999
# Remove display once collectable finished.
self.active_collectables_list.remove("GIANT!")
# SMALL
# Creates the player-small collision group.
self.player_small_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.small_collectable_group, True)
# If the player has collided with the collectable...
if self.player_small_collectable_collision_group:
# Record current time.
self.start_small_time = time.time()
# Change the image of the Playerbody sprite to a small car.
self.my_Playerbody.image = pygame.image.load("small_car.png")
# Display collectable in use to player.
if "SMALL!" not in self.active_collectables_list:
self.active_collectables_list.append("SMALL!")
# When 10 seconds have passed.
if time.time() > (self.start_small_time + 10):
# Change the Playerbody sprite image back to its original one.
self.my_Playerbody.image = pygame.image.load("car.png")
# Reset the timer.
self.start_small_time = 9999999999
# Remove display once collectable finished.
self.active_collectables_list.remove("SMALL!")
# SPORTS CAR
# Creates the player-sports collision group.
self.player_sports_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.sports_collectable_group, True)
# If the player has collided with the collectable...
if self.player_sports_collectable_collision_group:
# Record current time.
self.start_sports_time = time.time()
# Change the image of the Playerbody sprite to a sports car.
self.my_Playerbody.image = pygame.image.load("sports_car.png")
# Display collectable in use to player.
if "SPORTS!" not in self.active_collectables_list:
self.active_collectables_list.append("SPORTS!")
# When 10 seconds have passed.
if time.time() > (self.start_sports_time + 10):
self.my_Playerbody.image = pygame.image.load("car.png")
# Reset the timer.
self.start_sports_time = 9999999999
# Remove display once collectable finished.
self.active_collectables_list.remove("SPORTS!")
# RANDOM
# Creates the player-random collision group.
self.player_random_collectable_collision_group = pygame.sprite.spritecollide(self.my_Playerbody, self.random_collectable_group, True)
# If the player has collided with the collectable...
if self.player_random_collectable_collision_group:
# Record current time.
self.start_random_time = time.time()
# Generate a random number between 1 and 6.
random_number = random.randint(1, 6)
if random_number == 1:
# Activates spin collectable.
self.my_Playerbody.rot_speed = 25
elif random_number == 2:
# Activates turbo collectable.
self.turbo = True
elif random_number == 3:
# Activates ice collectable.
self.ice = True
elif random_number == 4:
# Activates giant collectable.
self.my_Playerbody.image = pygame.image.load("giant_car.png")
elif random_number == 5:
# Activates small collectable.
self.my_Playerbody.image = pygame.image.load("small_car.png")
elif random_number == 6:
# Activates sports collectable.
self.my_Playerbody.image = pygame.image.load("sports_car.png")
# Display collectable in use to player.
if "RANDOM!" not in self.active_collectables_list:
self.active_collectables_list.append("RANDOM!")
# When 10 seconds have passed.
if time.time() > (self.start_random_time + 10):
# Set all collectables back to normal.
self.my_Playerbody.rot_speed = ROT_SPEED
self.turbo = False
self.ice = False
self.my_Playerbody.image = pygame.image.load("car.png")
self.my_Playerbody.image = pygame.image.load("car.png")
self.my_Playerbody.image = pygame.image.load("car.png")
# Reset the timer.
self.start_random_time = 9999999999
# Remove display once collectable finished.
self.active_collectables_list.remove("RANDOM!")
# SCORE
self.score = self.my_Path.score + self.flip_score
if self.score > self.highscore: #New high score
#self.draw_stats(50, 50, "NEW HIGH SCORE")
self.highscore = self.score
with open(path.join(self.dir, "highscore.txt"), 'w') as f:
f.write(str(self.highscore))
#Instructions
self.insx += self.my_Path.vel #x pos of info will move appropriately
#vel is negative as path moves left so you add it
def events(self):
# -- User inputs here
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
self.playing = False
def draw(self):
# -- Screen background is BLACK
screen.fill(BLACK)
# -- Draw here
#self.all_sprites_group.draw(screen)
self.path_group.draw(screen) #Is first block appearing fine?
# Draws collectable groups
self.spin_collectable_group.draw(screen)
self.turbo_collectable_group.draw(screen)
self.ice_collectable_group.draw(screen)
self.giant_collectable_group.draw(screen)
self.small_collectable_group.draw(screen)
self.sports_collectable_group.draw(screen)
self.random_collectable_group.draw(screen)
#self.all_sprites_group.draw(screen)
#pygame.draw.line(screen, WHITE, (0,0), (200,200), 5)
#pygame.draw.arc(screen, WHITE,[80,80,80,80], 0.5, 0.5, 10)
#Draw player which can rotate
#screen.blit(self.my_Player1.new_image, self.my_Player1.rect)
#screen.blit(self.my_Player2.new_image, self.my_Player2.rect)
screen.blit(self.my_Playerbody.new_image, self.my_Playerbody.rect)
#screen.blit(self.my_Player.image, self.my_Player.rect)
# Draw score and high score
self.draw_stats((WIDTH // 2 - 80), 50, "SCORE: %d" % self.score, 30, WHITE)
self.draw_stats((WIDTH - 150), 0, "HIGH SCORE: %d" % self.highscore, 20, WHITE)
# Draw instructions which move with path.
self.draw_stats(self.insx, 220, "PRESS SPACE TO MOVE", 25, WHITE)
self.draw_stats(self.insx, 250, "PRESS SPACE TO FLIP", 25, WHITE)
self.draw_stats(self.insx, 280, "DON'T LAND UPSIDE DOWN!", 25, WHITE)
# Draw which collectables are being used.
#self.draw_stats(30, 30, self.current_collectable_1, 30, YELLOW)
#self.draw_stats(30, 55, self.current_collectable_2, 30, YELLOW)
#self.draw_stats(30, 80, self.current_collectable_3, 30, YELLOW)
#self.draw_stats(30, 105, self.current_collectable_4, 30, YELLOW)
#self.draw_stats(30, 130, self.current_collectable_5, 30, YELLOW)
if len(self.active_collectables_list) > 0:
self.draw_stats(100, 30, self.active_collectables_list[0], 30, YELLOW)
if len(self.active_collectables_list) > 1:
self.draw_stats(100, 55, self.active_collectables_list[1], 30, YELLOW)
if len(self.active_collectables_list) > 2:
self.draw_stats(100, 80, self.active_collectables_list[2], 30, YELLOW)
if len(self.active_collectables_list) > 3:
self.draw_stats(100, 105, self.active_collectables_list[3], 30, YELLOW)
if len(self.active_collectables_list) > 4:
self.draw_stats(100, 130, self.active_collectables_list[4], 30, YELLOW)
if len(self.active_collectables_list) > 5:
self.draw_stats(100, 155, self.active_collectables_list[5], 30, YELLOW)
if len(self.active_collectables_list) > 6:
self.draw_stats(100, 180, self.active_collectables_list[6], 30, YELLOW)
# -- flip display to reveal new position of objects
pygame.display.flip()
# Draws text onto the screen.
def draw_stats(self, x, y, stats, size, colour):
# Select the font to use, size, bold, italics.
font = pygame.font.SysFont('Calibri', size, True, False)
# Render the text. "True" means anti-aliased text.
# Note: This line creates an image of the letters,
# but does not put it on the screen yet.
text = font.render(str(stats), True, colour)
# Puts the image of the text on the screen at x,y.
screen.blit(text, (x, y))
# Display collectable in use to player.
#def displayCollectables(self, collectable):
#if collectable not in self.active_collectables_list:
#if self.current_collectable_1 == "":
#self.current_collectable_1 = collectable
#elif self.current_collectable_2 == "":
#self.current_collectable_2 = collectable
#elif self.current_collectable_3 == "":
#self.current_collectable_3 = collectable
#elif self.current_collectable_4 == "":
#self.current_collectable_4 = collectable
#elif self.current_collectable_5 == "":
#self.current_collectable_5 = collectable
# Endif
#def removeCollectables(self, collectable):
#if self.current_collectable_1 == collectable:
#self.current_collectable_1 = ""
#elif self.current_collectable_2 == collectable:
# self.current_collectable_2 = ""
#elif self.current_collectable_3 == collectable:
# self.current_collectable_3 = ""
#elif self.current_collectable_4 == collectable:
# self.current_collectable_4 = ""
#elif self.current_collectable_5 == collectable:
# self.current_collectable_5 = ""
#Endproc
#End Game Class
my_Game = Game()
while my_Game.running == True:
my_Game.new_game()
pygame.quit()