-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.py
165 lines (144 loc) · 5.91 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
#initialize the screen
import pygame, math, sys, level2, time
from pygame.locals import *
def level1():
pygame.init()
screen = pygame.display.set_mode((1024, 768))
#GAME CLOCK
clock = pygame.time.Clock()
font = pygame.font.Font(None, 75)
win_font = pygame.font.Font(None, 50)
win_condition = None
win_text = font.render('', True, (0, 255, 0))
loss_text = font.render('', True, (255, 0, 0))
pygame.mixer.music.load('My_Life_Be_Like.mp3')
t0 = time.time()
class CarSprite(pygame.sprite.Sprite):
MAX_FORWARD_SPEED = 10
MAX_REVERSE_SPEED = 10
ACCELERATION = 2
TURN_SPEED = 10
def __init__(self, image, position):
pygame.sprite.Sprite.__init__(self)
self.src_image = pygame.image.load(image)
self.position = position
self.speed = self.direction = 0
self.k_left = self.k_right = self.k_down = self.k_up = 0
def update(self, deltat):
#SIMULATION
self.speed += (self.k_up + self.k_down)
if self.speed > self.MAX_FORWARD_SPEED:
self.speed = self.MAX_FORWARD_SPEED
if self.speed < -self.MAX_REVERSE_SPEED:
self.speed = -self.MAX_REVERSE_SPEED
self.direction += (self.k_right + self.k_left)
x, y = (self.position)
rad = self.direction * math.pi / 180
x += -self.speed*math.sin(rad)
y += -self.speed*math.cos(rad)
self.position = (x, y)
self.image = pygame.transform.rotate(self.src_image, self.direction)
self.rect = self.image.get_rect()
self.rect.center = self.position
class PadSprite(pygame.sprite.Sprite):
normal = pygame.image.load('images/race_pads.png')
hit = pygame.image.load('images/collision.png')
def __init__(self, position):
super(PadSprite, self).__init__()
self.rect = pygame.Rect(self.normal.get_rect())
self.rect.center = position
def update(self, hit_list):
if self in hit_list: self.image = self.hit
else: self.image = self.normal
pads = [
PadSprite((0, 10)),
PadSprite((600, 10)),
PadSprite((1100, 10)),
PadSprite((100, 150)),
PadSprite((600, 150)),
PadSprite((100, 300)),
PadSprite((800, 300)),
PadSprite((400, 450)),
PadSprite((700, 450)),
PadSprite((200, 600)),
PadSprite((900, 600)),
PadSprite((400, 750)),
PadSprite((800, 750)),
]
pad_group = pygame.sprite.RenderPlain(*pads)
class Trophy(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/trophy.png')
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = position
def draw(self, screen):
screen.blit(self.image, self.rect)
trophies = [Trophy((285,0))]
trophy_group = pygame.sprite.RenderPlain(*trophies)
# CREATE A CAR AND RUN
rect = screen.get_rect()
car = CarSprite('images/car.png', (10, 730))
car_group = pygame.sprite.RenderPlain(car)
#THE GAME LOOP
while 1:
#USER INPUT
t1 = time.time()
dt = t1-t0
deltat = clock.tick(30)
for event in pygame.event.get():
if not hasattr(event, 'key'): continue
down = event.type == KEYDOWN
if win_condition == None:
if event.key == K_RIGHT: car.k_right = down * -5
elif event.key == K_LEFT: car.k_left = down * 5
elif event.key == K_UP: car.k_up = down * 2
elif event.key == K_DOWN: car.k_down = down * -2
elif event.key == K_ESCAPE: sys.exit(0) # quit the game
elif win_condition == True and event.key == K_SPACE: level2.level2()
elif win_condition == False and event.key == K_SPACE:
level1()
t0 = t1
elif event.key == K_ESCAPE: sys.exit(0)
#COUNTDOWN TIMER
seconds = round((20 - dt),2)
if win_condition == None:
timer_text = font.render(str(seconds), True, (255,255,0))
if seconds <= 0:
win_condition = False
timer_text = font.render("Time!", True, (255,0,0))
loss_text = win_font.render('Press Space to Retry', True, (255,0,0))
#RENDERING
screen.fill((0,0,0))
car_group.update(deltat)
collisions = pygame.sprite.groupcollide(car_group, pad_group, False, False, collided = None)
if collisions != {}:
win_condition = False
timer_text = font.render("Crash!", True, (255,0,0))
car.image = pygame.image.load('images/collision.png')
loss_text = win_font.render('Press Space to Retry', True, (255,0,0))
seconds = 0
car.MAX_FORWARD_SPEED = 0
car.MAX_REVERSE_SPEED = 0
car.k_right = 0
car.k_left = 0
trophy_collision = pygame.sprite.groupcollide(car_group, trophy_group, False, True)
if trophy_collision != {}:
seconds = seconds
timer_text = font.render("Finished!", True, (0,255,0))
win_condition = True
car.MAX_FORWARD_SPEED = 0
car.MAX_REVERSE_SPEED = 0
pygame.mixer.music.play(loops=0, start=0.0)
win_text = win_font.render('Press Space to Advance', True, (0,255,0))
if win_condition == True:
car.k_right = -5
pad_group.update(collisions)
pad_group.draw(screen)
car_group.draw(screen)
trophy_group.draw(screen)
#Counter Render
screen.blit(timer_text, (20,60))
screen.blit(win_text, (250, 700))
screen.blit(loss_text, (250, 700))
pygame.display.flip()