-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc.py
executable file
·278 lines (230 loc) · 8.01 KB
/
ccc.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
#!/usr/bin/env python
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
This is version 0.0.4
See bottom of source for changelog
"""
import pygame, sys
from cablecar import *
from pygame.locals import *
"""
Each cable car follows a route defined by their list of nodes. The nodes are the (x, y)
coordinates of the track 'joints'. The longer the section of track the faster the cable
car will go on that section. For example if the nodes were ((100, 100), (200, 200)) the car
will go at a medium speed left and downwards. If the nodes were ((300, 300), (800, 200)) then
the car will travel quite fast to the left and slightly upwards.
The tracks can overlap, but do not add the same coordinates twice for your car as you will
end up in a inescapable loop. The baddies, however, can have their first and last coordinates
the same and it makes an interesting circuit path.
"""
## levels is a list of levels. The game will cycle through them until you win the last one.
levels=[]
## Each level has "your" nodes.
me=Car(((100, 300), (200, 300), (300, 300), (400, 300), (500, 300), (600, 300), (700, 300)))
## And the baddies nodes.
baddies=[]
baddie=Car(((350, 100), (350, 200), (350, 300), (350, 400), (350, 500)), False, True)
baddies.append(baddie)
baddie=Car(((450, 100), (500, 200), (550, 300), (600, 400), (650, 500)), False, True)
baddies.append(baddie)
## The node tuples are added as a tuple !in the right order!
levels.append((me, baddies))
## Each level has "your" nodes.
me=Car(((100, 300), (200, 300), (300, 300), (400, 300), (500, 300), (600, 300), (700, 300)))
## And the baddies nodes.
baddies=[]
baddie=Car(((200, 100), (250, 200), (300, 300), (350, 200), (400, 100)), False, True)
baddies.append(baddie)
baddie=Car(((400, 290), (500, 290), (600, 290), (600, 400), (500, 400), (400, 400), (400, 290)), False, True)
baddies.append(baddie)
## The node tuples are added as a tuple !in the right order!
levels.append((me, baddies))
## Each level has "your" nodes.
me=Car(((100, 300), (200, 300), (300, 300), (400, 400), (500, 400), (600, 300), (700, 300)))
## And the baddies nodes.
baddies=[]
baddie=Car(((200, 200), (200, 300), (200, 400), (300, 400), (400, 400), (400, 300), (400, 200), (300, 200), (200, 200)), False, True)
baddies.append(baddie)
baddie=Car(((450, 320), (550, 320), (650, 420), (650, 520)), False, True)
baddies.append(baddie)
## The node tuples are added as a tuple !in the right order!
levels.append((me, baddies))
## Each level has "your" nodes.
me=Car(((100, 300), (200, 400), (100, 400), (200, 300), (300, 300), (300, 400), (400, 400), (600, 400), (600, 300)))
## And the baddies nodes.
baddies=[]
baddie=Car(((170, 400), (270, 400), (220, 450), (170, 400)), False, True)
baddies.append(baddie)
baddie=Car(((600, 420), (400, 420), (400, 520), (500, 520), (600, 520), (600, 420)), False, True)
baddies.append(baddie)
## The node tuples are added as a tuple !in the right order!
levels.append((me, baddies))
##### END OF LEVEL DEFINING #####
FPS = 24 # frames per second, the general speed of the program
WINDOWWIDTH = 250 # size of window's width in pixels
WINDOWHEIGHT = 250 # size of windows' height in pixels
#WINDOWWIDTH = 800 # size of window's width in pixels
#WINDOWHEIGHT = 600 # size of windows' height in pixels
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
FPSCLOCK = pygame.time.Clock()
BACKGROUND=pygame.image.load('images/backtile.png')
BGH=64
BGW=64
CAR=pygame.image.load('images/car.png')
BAD=pygame.image.load('images/bad.png')
CAR_W=25
CAR_H=50
fontObj = pygame.font.Font('freesansbold.ttf', 16)
CRASH=pygame.mixer.Sound('sounds/crash.ogg')
pygame.mixer.music.load('sounds/music.ogg')
TRACKWIDTH=3
RED=(255, 0, 0)
BLUE=(0, 0, 255)
BLACK=(0, 0, 0)
GREEN=(0, 255, 0)
def crash(me, you):
my_left=me.x-(CAR_W/2)
my_right=me.x+(CAR_W/2)
my_top=me.y
my_bottom=me.y+CAR_H
your_left=you.x-(CAR_W/2)
your_right=you.x+(CAR_W/2)
your_top=you.y
your_bottom=you.y+CAR_H
if my_left<your_right:
if my_right>your_left:
if my_top<your_bottom:
if my_bottom>your_top:
return True
return False
def translate(nodes, pov):
#pov is where I am
out=[]
ox=pov[0]-WINDOWWIDTH/2
oy=pov[1]-WINDOWHEIGHT/2+CAR_H/2
for c in nodes:
x=c[0]-ox
y=c[1]-oy
out.append((x, y))
return out
pygame.display.set_caption ("Crazy Cable Cars")
pygame.mixer.music.play(-1, 0.0)
lives=5
lev=0
def main():
global lives, lev
for level in levels:
if lives>0:
lev=lev+1
me=level[0]
baddies=level[1]
exit=False
while exit==False: #main game loop
for event in pygame.event.get(): # event handling loop
if event.type == QUIT:
exit=True
lives=0
elif event.type==KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit=True
lives=0
elif event.key==112:
me.dir=1
elif event.key==111:
me.dir=-1
else:
print event.key
elif event.type==KEYUP:
me.dir=0
me.move()
for baddie in baddies:
baddie.move()
if me.win==True:
exit=True
for iy in xrange(0, (WINDOWHEIGHT/BGH)+2):
for ix in xrange(0, (WINDOWWIDTH/BGW)+2):
DISPLAYSURF.blit(BACKGROUND, (ix*BGW-(me.x % BGW), iy*BGH-(me.y % BGH)))
pygame.draw.lines(DISPLAYSURF, RED, False, translate(me.path, (me.x, me.y)), TRACKWIDTH)
for baddie in baddies:
pygame.draw.lines(DISPLAYSURF, BLUE, baddie.loop, translate(baddie.path, (me.x, me.y)), TRACKWIDTH)
for baddie in baddies:
DISPLAYSURF.blit(BAD, (baddie.x-(me.x-WINDOWWIDTH/2)-CAR_W/2, baddie.y-(me.y-WINDOWHEIGHT/2+CAR_H/2)))
DISPLAYSURF.blit(CAR, (WINDOWWIDTH/2-CAR_W/2, WINDOWHEIGHT/2-CAR_H/2))
"""pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, 250, 20), 0)
textSurfaceObj = fontObj.render('FPS:'+str(round(FPSCLOCK.get_fps(), 2)), True, GREEN, BLUE)
textRectObj = textSurfaceObj.get_rect()
textRectObj.topleft=(0,0)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)"""
pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, 250, 20), 0)
textSurfaceObj = fontObj.render('Level: '+str(lev)+' Lives: '+str(lives), True, GREEN, BLUE)
textRectObj = textSurfaceObj.get_rect()
textRectObj.topleft=(0,0)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)
for baddie in baddies:
if crash(me, baddie)==True:
CRASH.play()
lives=lives-1
if (lives>0):
me.x=me.path[0][0]
me.y=me.path[0][1]
me.getVectors()
else:
exit=True
pygame.display.update()
FPSCLOCK.tick(FPS)
#import cProfile as profile
#profile.run('main()')
main()
pygame.mixer.music.stop()
exit=True
while exit==False:
#closing bit loop
for event in pygame.event.get(): # event handling loop
if event.type == QUIT:
exit=True
elif event.type==KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit=True
pygame.display.update()
FPSCLOCK.tick(1)
print "End of game"
if lives==0:
print "You lost"
else:
print "You won"
pygame.quit()
sys.exit()
"""
Changelog
2012-08-09
Removed a couple of unused files
Moved sounds and images to their own directory
2012-08-09
version 0.0.4
Added sound and music
OK, enough for one day
2012-08-08
version 0.0.3
Made some proper levels
Made an explanation of how to make your own levels
Improved the background. It now uses a tiled background
Made baddies a different colour
Set the window title :p
Added the concept of 'lives'
2012-08-08
version 0.0.2
Changed the view so it is more like looking through a window, allowing much bigger levels
2012-08-07
version 0.0.1
Initial Release
"""