-
Notifications
You must be signed in to change notification settings - Fork 0
/
carRace.py
211 lines (167 loc) · 5.98 KB
/
carRace.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
import pygame,sys, math
from pygame.locals import *
# Game constants
W_HEIGHT = 600
W_WIDTH = 800
FPS = 30
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
SKYBLUE = (0, 240, 255)
GRASS = (50,170,20)
GREY = (127, 127, 127)
BROWN = (150, 110, 40)
def main():
global SCREEN
pygame.init()
SCREEN = pygame.display.set_mode((W_WIDTH,W_HEIGHT))
pygame.display.set_caption("CARS MANIA")
clock = pygame.time.Clock()
Distance = 0.0 # Distance car has travelled around track
Curvature = 0.0 # Current track curvature, lerped between track sections
TrackCurvature = 0.0 # Accumulation of track curvature
TrackDistance = 0.0 # Total distance of track
CarPos = 0.0 # Current car position
PlayerCurvature = 0.0 # Accumulation of player curvature
Speed = 0.0 # Current player speed
Track = [] # Track sections, sharpness of bend, length of section
listLapTimes = [] # List of previous lap times
CurrentLapTime = 0 # Current lap time
# Direction booleans
UP = False
LEFT = False
RIGHT = False
# Define track
Track.append((0.0, 10.0)) # Short section for start/finish line
Track.append((0.0, 200.0))
Track.append((0.5, 200.0))
Track.append((0.0, 400.0))
Track.append((-0.5, 100.0))
Track.append((0.0, 200.0))
Track.append((-0.5, 200.0))
Track.append((0.5, 200.0))
Track.append((0.0, 200.0))
Track.append((0.2, 500.0))
Track.append((0.0, 200.0))
# Calculate total track distance, so we can set lap times
for t in Track:
TrackDistance += t[1]
listLapTimes = ['0','0','0','0','0']
fCurrentLapTime = 0.0
print(listLapTimes)
while True: # main game loop
# Handle control input
CarDirection = 0
timeElapsed = clock.get_time()/1000
#~ print(timeElapsed)
SCREEN.fill(WHITE)
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key in (K_LEFT, K_a) and not gameOver():
LEFT = True
elif event.key in (K_RIGHT, K_d) and not gameOver():
RIGHT = True
elif event.key in (K_UP, K_w) and not gameOver():
UP = True
elif event.key in (K_DOWN, K_s) and not gameOver():
pass
if event.type == KEYUP:
if event.key in (K_LEFT, K_a) and not gameOver():
LEFT = False
elif event.key in (K_RIGHT, K_d) and not gameOver():
RIGHT = False
elif event.key in (K_UP, K_w) and not gameOver():
UP = False
elif event.key in (K_DOWN, K_s) and not gameOver():
pass
elif event.key == 13 and gameOver(): # Press Enter to restart
pass
if (UP):
Speed += 2.0 * timeElapsed
else:
Speed -= 1.0 * timeElapsed
# Car Curvature is accumulated left/right input, but inversely proportional to speed
# i.e. it is harder to turn at high speed
if (LEFT):
PlayerCurvature -= 0.7 * timeElapsed * (1.0 - Speed / 2.0)
CarDirection = -1
if (RIGHT):
PlayerCurvature += 0.7 * timeElapsed * (1.0 - Speed / 2.0)
CarDirection = +1
# If car curvature is too different to track curvature, slow down
# as car has gone off track
if (math.fabs(PlayerCurvature - TrackCurvature) >= 0.8):
Speed -= 5.0 * timeElapsed
# Clamp Speed
if (Speed < 0.0):
Speed = 0.0;
if (Speed > 1.0):
Speed = 1.0;
# Move car along track according to car speed
Distance += (70 * Speed) * timeElapsed
#~ print(Distance)
# Get Point on track
Offset = 0;
TrackSection = 0;
# Lap Timing and counting
CurrentLapTime += timeElapsed
if (Distance >= TrackDistance):
Distance -= TrackDistance
listLapTimes.append('%.3f' %CurrentLapTime)
del(listLapTimes[0])
print(listLapTimes)
CurrentLapTime = 0.0
# Find position on track
while (TrackSection < len(Track) and Offset <= Distance):
Offset += Track[TrackSection][1]
TrackSection += 1
# Interpolate towards target track curvature
TargetCurvature = Track[TrackSection - 1][0]
TrackCurveDiff = (TargetCurvature - Curvature) * timeElapsed * Speed
# Accumulate player curvature
Curvature += TrackCurveDiff
#~ print(Curvature)
# Accumulate track curvature
TrackCurvature += (Curvature) * timeElapsed * Speed
# Draw Sky
pygame.draw.rect(SCREEN, SKYBLUE, (0, 0, W_WIDTH, W_HEIGHT//2))
# Draw Scenery - our hills are a rectified sine wave, where the phase is adjusted by the
# accumulated track curvature
for x in range(W_WIDTH):
HillHeight = int(math.fabs(math.sin(x * 0.01 + TrackCurvature) * 128))
pygame.draw.line(SCREEN, BROWN, (x, W_HEIGHT//2), (x, W_HEIGHT//2 - HillHeight))
# Draw road
for i in range(0, W_HEIGHT//2):
Perspective = i/W_HEIGHT
RoadWidth = 0.04 + Perspective
ClipWidth = RoadWidth * 0.15
MiddlePoint = 0.5 + Curvature * ((1.0 - Perspective) ** 6)
LeftGrass = (MiddlePoint - RoadWidth - ClipWidth) * W_WIDTH
LeftClip = (MiddlePoint - RoadWidth) * W_WIDTH
RightClip = (MiddlePoint + RoadWidth) * W_WIDTH
RightGrass = (MiddlePoint + RoadWidth + ClipWidth) * W_WIDTH
GrassColour = GREEN if (math.sin(20.0 * (1.0 - Perspective)**6 + Distance * 0.1) > 0.0) else GRASS
ClipColour = RED if (math.sin(80.0 * (1.0 - Perspective)**2 + Distance) > 0.0) else WHITE
RoadColour = WHITE if (TrackSection-1) == 0 else GREY
h = i + W_HEIGHT//2
pygame.draw.line(SCREEN, GrassColour, (0, h), (LeftGrass, h))
pygame.draw.line(SCREEN, ClipColour, (LeftGrass, h), (LeftClip, h))
pygame.draw.line(SCREEN, RoadColour, (LeftClip, h), (RightClip, h))
pygame.draw.line(SCREEN, ClipColour, (RightClip, h), (RightGrass, h))
pygame.draw.line(SCREEN, GrassColour, (RightGrass, h), (W_WIDTH, h))
# Draw car
CarPos = PlayerCurvature - TrackCurvature
CarPos = W_WIDTH / 2 + (W_WIDTH * CarPos) / 2
pygame.draw.rect(SCREEN, BLACK, (CarPos, W_HEIGHT-100, 100, 50))
pygame.display.update()
clock.tick(FPS)
def gameOver():
return False
if __name__ == '__main__':
main()