-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
220 lines (142 loc) · 6.35 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
import sys, pygame
from InputManager import *
from car import *
from Player import *
from GhostTrack import *
from GhostCar import *
from Coin import *
import random
import time
pygame.init()
pygame.mixer.init()
STATE_MENU = 0
STATE_GAME = 1
STATE_COUNTDOWN = 2
class HighScoreDisplay:
def __init__(self):
self.BOARD_SIZE = 720
self.size = width, height = self.BOARD_SIZE, self.BOARD_SIZE
self.black = 0, 0, 0
self.screen = pygame.display.set_mode(self.size)
self.roundaboutImage = pygame.image.load("media/roundaboutNew.png")
self.roundaboutImage = pygame.transform.scale(self.roundaboutImage, self.size);
self.roundaboutRect = self.roundaboutImage.get_rect()
pygame.font.init()
self.font = pygame.font.SysFont("Arial", 60)
def run(self):
while 1:
self.screen.fill(self.black)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return
if event.key == pygame.K_q:
sys.exit()
#self.screen.blit(self.roundaboutImage, screenshaker.offset.getTuple(), self.roundaboutRect)
self.screen.blit(self.roundaboutImage, screenshaker.offset.getTuple(), self.roundaboutRect)
yPos = 0
for line in highScore.getString().split("\n"):
textSurface = self.font.render(line, True, (255,255,255))
self.screen.blit(textSurface, (0,yPos), textSurface.get_rect())
yPos += textSurface.get_height()
pygame.display.flip()
class Game:
def __init__(self):
self.BOARD_SIZE = 720
self.size = width, height = self.BOARD_SIZE, self.BOARD_SIZE
self.speed = [0.2, 0.2]
self.black = 0, 0, 0
self.screen = pygame.display.set_mode(self.size)
self.roundaboutImage = pygame.image.load("media/roundaboutNew.png")
#roundabout = pygame.image.load("ball.gif")
self.readyImg = pygame.image.load("media/readyScreen.png")
self.roundaboutImage = pygame.transform.scale(self.roundaboutImage, self.size);
self.readyImg = pygame.transform.scale(self.readyImg, self.size);
self.roundaboutRect = self.roundaboutImage.get_rect()
self.readyBox = self.readyImg.get_rect()
self.inputManager = InputManager()
self.player = Player(0, 200);
self.currentTrack = GhostTrack();
self.currentOffset = 0
self.oldCars = []
self.oldTracks = []
self.oldOffsets = []
self.doneLaps = 0
self.framesToRun = 1000
self.waves = 0
self.state = STATE_MENU
self.explosionSound = pygame.mixer.Sound("media/Explosion.wav")
def restartGame(self):
self.oldTracks.append(self.currentTrack)
self.oldOffsets.append(self.currentOffset)
#Restart the game and spawn some stuff
self.oldCars.append(GhostCar())
for track in self.oldTracks:
track.reset_count()
self.startGame()
def startGame(self):
self.currentOffset = random.random() * math.pi * 2
self.currentTrack = GhostTrack()
self.player.resetCar();
self.player.setAng(self.currentOffset);
self.currentFrame = 0
self.state = STATE_COUNTDOWN
self.startTime = time.time()
self.waves += 1
self.coin = Coin()
def main(self):
while 1:
self.screen.fill(self.black)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
self.inputManager.onEvent(event);
if self.state == STATE_GAME:
self.player.update(self.inputManager);
self.currentTrack.add_pos(self.player.getAng(), self.player.getRad())
self.currentFrame += 1
if self.currentFrame >= self.framesToRun:
self.restartGame()
for i in range(0, len(self.oldCars)):
oldPos = self.oldTracks[i].get_next_pos()
self.oldCars[i].setAng(oldPos[0])
self.oldCars[i].setRad(oldPos[1] + self.oldOffsets[i])
self.oldCars[i].update()
self.oldCars[i].setFuture(self.oldTracks[i].get_future_pos(60))
if self.oldCars[i].checkCollision(self.player):
self.player.shakeSound.stop()
score = self.player.score
self.explosionSound.play()
print "You died, final score: {}".format(score)
pygame.display.quit()
if(score >= highScore.getLowest()):
name = raw_input("New high score, what's your name?\n")
highScore.addScore(name, score)
highScore.save()
return
self.coin.update(self.player)
elif self.state == STATE_COUNTDOWN:
if(time.time() > self.startTime + 1):
self.state = STATE_GAME
for car in self.oldCars:
car.updateGraphics()
self.player.updateGraphics()
self.coin.updateGraphics()
#self.screen.blit(self.roundaboutImage, screenshaker.offset.getTuple(), self.roundaboutRect)
self.screen.blit(self.roundaboutImage, screenshaker.offset.getTuple(), self.roundaboutRect)
self.player.draw(self.screen);
for car in self.oldCars:
car.draw(self.screen)
self.coin.draw(self.screen)
if self.state == STATE_COUNTDOWN:
self.screen.blit(self.readyImg, self.readyBox)
pygame.display.flip()
pygame.mixer.music.load("media/AmericanRoundabout.ogg")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.5)
while True:
game = Game()
game.startGame()
game.main()
scoreDisplay = HighScoreDisplay()
scoreDisplay.run()