-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.py
425 lines (369 loc) · 15.8 KB
/
game.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
import pygame, pygame.gfxdraw, math, random
pygame.init()
pygame.font.init()
SCREEN_WIDTH, SCREEN_HEIGHT = 500,700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Color Switch")
path = "H:\\Documents\\Programming\\Python\\test2\\"
#pygame.display.set_icon(pygame.image.load(path+"color_switch.png"))
#retry = pygame.image.load(path+"color_switch_retry.png")
#retry = pygame.transform.flip(retry, True, False)
PURPLE = (140, 19, 251)
RED = (255, 0, 128)
TEAL = (53, 226, 242)
YELLOW = (246, 223, 14)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
clock = pygame.time.Clock()
obstacles = list()
stars = list()
colorswitches = list()
MENU, GAMEPLAY, PAUSE, GAMEOVER = range(4)
gamestate = MENU
score = 0
highscore = 0
font = pygame.font.Font(pygame.font.get_default_font(), 24)
menu_font = pygame.font.Font(pygame.font.get_default_font(), 60)
class Camera:
def __init__(self):
self.x = 0
self.y = 0
cam = Camera()
class Obstacle:
def __init__(self, surface, x=250, y=150, rad=220, angle = 0, vel = 1):
self.x = x
self.y = y
self.rad = rad
self.angle = angle
self.surface = surface
self.vel = vel
self.thickness = 25
def update(self):
x, y = (self.x-float(self.rad/2)-cam.x, self.y-float(self.rad/2)-cam.y)
if(y >= SCREEN_HEIGHT):
obstacles.remove(self)
print("obstacle was removed")
return
self.angle+=self.vel
if(self.angle > 360):
self.angle-=360
elif(self.angle <= 0):
self.angle+=360
def draw(self):
#pygame.gfxdraw.arc(self.surface, self.x, self.y, 100, 0, 180, (255,255,255))
x, y = (self.x-float(self.rad/2)-cam.x, self.y-float(self.rad/2)-cam.y)
x, y = int(x), int(y)
thick = self.thickness
pygame.draw.arc(self.surface, PURPLE , (x, y, self.rad, self.rad), math.radians(0+self.angle) ,math.radians(90+self.angle), thick)
pygame.draw.arc(self.surface, PURPLE , (x, y+1, self.rad, self.rad), math.radians(0+self.angle) ,math.radians(90+self.angle), thick)
pygame.draw.arc(self.surface, YELLOW , (x, y, self.rad, self.rad), math.radians(90+self.angle) , math.radians(180+self.angle), thick)
pygame.draw.arc(self.surface, YELLOW , (x, y+1, self.rad, self.rad), math.radians(90+self.angle) ,math.radians(180+self.angle), thick)
pygame.draw.arc(self.surface, TEAL , (x, y, self.rad, self.rad), math.radians(180+self.angle) ,math.radians(270+self.angle), thick)
pygame.draw.arc(self.surface, TEAL , (x, y+1, self.rad, self.rad), math.radians(180+self.angle) ,math.radians(270+self.angle), thick)
pygame.draw.arc(self.surface, RED , (x, y, self.rad, self.rad), math.radians(270+self.angle) ,math.radians(360+self.angle), thick)
pygame.draw.arc(self.surface, RED , (x, y+1, self.rad, self.rad), math.radians(270+self.angle) ,math.radians(360+self.angle), thick)
#pygame.gfxdraw.aacircle(self.surface, int(self.x), int(self.y), int(self.rad/2), (20,20,20))
pygame.gfxdraw.aacircle(self.surface, int(self.x-cam.x), int(self.y-cam.y), int(self.rad/2)+1, (20,20,20))
pygame.gfxdraw.aacircle(self.surface, int(self.x-cam.x), int(self.y-cam.y), int(self.rad/2), (20,20,20))
pygame.gfxdraw.aacircle(self.surface, int(self.x-cam.x), int(self.y-cam.y), int(self.rad/2)-thick-1, (20,20,20))
pygame.gfxdraw.aacircle(self.surface, int(self.x-cam.x), int(self.y-cam.y), int(self.rad/2)-thick, (20,20,20))
class Star:
def __init__(self, surface, x, y):
self.x = x
self.y = y
self.w = 10
self.h = 10
self.surface = surface
self.color = WHITE
self.dead = False
self.dead_counter = 0
def update(self):
if(self.dead and self.dead_counter < 40):
self.dead_counter+=1
elif(self.dead):
stars.remove(self)
def draw(self):
x,y = self.x-cam.x,self.y-cam.y
if(not self.dead):
points = ((x,y-16),(x-7,y-5), (x-20,y-3), (x-11,y+8), (x-13, y+21), (x, y+16), (x+13, y+21), (x+11, y+8), (x+20, y-3), (x+7,y-5))
pygame.gfxdraw.aapolygon(self.surface, points, self.color)
pygame.gfxdraw.filled_polygon(self.surface, points, self.color)
else:
self.surface.blit(font.render("+1", True, (255-self.dead_counter*5, 255-self.dead_counter*5, 255-self.dead_counter*5)), (x-10,y-self.dead_counter))
def draw_pie(x, y, rad, s_angle, e_angle, color):
points = [(x,y)]
for n in range(s_angle, e_angle+1):
tx = x + int(rad*math.cos(math.radians(n)))
ty = y + int(rad*math.sin(math.radians(n)))
points.append((tx, ty))
points.append((x,y))
if(len(points)>2):
pygame.gfxdraw.aapolygon(screen, points, color)
pygame.gfxdraw.filled_polygon(screen, points, color)
# pygame.draw.polygon(screen, RED, points)
def random_color():
rand = random.randint(0,3)
if(rand == 0):
return PURPLE
elif(rand == 1):
return RED
elif(rand == 2):
return TEAL
elif(rand == 3):
return YELLOW
class ColorSwitch:
def __init__(self, surface, x, y, color = random_color()):
self.x = x
self.y = y
self.surface = surface
self.rad = 22
self.color = color
def draw(self):
x, y = int(self.x-cam.x), int(self.y-cam.y)
draw_pie(x, y, self.rad, 0, 90, RED)
draw_pie(x, y, self.rad, 90, 180, TEAL)
draw_pie(x, y, self.rad, 180, 270, YELLOW)
draw_pie(x, y, self.rad, 270, 360, PURPLE)
pygame.gfxdraw.aacircle(self.surface, x, y,self.rad-1, (20,20,20))
pygame.gfxdraw.aacircle(self.surface, x, y,self.rad, (20,20,20))
class ExplosionBall:
def __init__(self, surface, x=250, y=400):
self.x = x
self.y = y
#self.rad = 3
self.rad = random.randint(2,5)
self.surface = surface
self.vel = [random.uniform(-20,20),random.uniform(-20,20)]
self.color = random_color()
def draw(self):
x, y = int(self.x-cam.x), int(self.y-cam.y)
pygame.gfxdraw.aacircle(self.surface, x, y, self.rad, self.color)
pygame.gfxdraw.filled_circle(self.surface, x, y, self.rad, self.color)
def update(self):
X,Y = 0,1
self.vel[Y] += 0.5
self.x += self.vel[X]
if(self.x >= SCREEN_WIDTH or self.x <= 0):
self.vel[X] = -self.vel[X]
self.y += self.vel[Y]
class Ball:
def __init__(self, surface, x=250, y=400):
self.x = x
self.y = y
self.rad = 10
self.surface = surface
self.vel = 0
self.color = random_color()
self.dead = False
self.dead_counter = 0
self.explosion_balls = []
def collision_detection(self):
global score
x, y = self.x-cam.x, self.y-cam.y
for star in stars:
if(star.y+16 >= self.y):
star.color = BLACK
if(not star.dead):
score+=1
star.dead = True
for obstacle in obstacles:
if(obstacle.y+int(obstacle.rad/2) >= self.y and obstacle.y+int(obstacle.rad/2)-25 <= self.y):
if(self.color != YELLOW and obstacle.angle > 90 and obstacle.angle <= 180):
self.die()
elif(self.color != PURPLE and obstacle.angle > 180 and obstacle.angle <= 270):
self.die()
elif(self.color != RED and obstacle.angle > 270 and obstacle.angle <= 360):
self.die()
elif(self.color != TEAL and obstacle.angle <= 90):
self.die()
#elif(self.surface==screen):
#self.die()
elif(obstacle.y-(obstacle.rad/2)+25 >= self.y-self.rad and obstacle.y-(obstacle.rad/2) <= self.y):
if(self.color != RED and obstacle.angle > 90 and obstacle.angle <= 180):
self.die()
elif(self.color != TEAL and obstacle.angle > 180 and obstacle.angle <= 270):
self.die()
elif(self.color != YELLOW and obstacle.angle > 270 and obstacle.angle <= 360):
self.die()
elif(self.color != PURPLE and obstacle.angle <= 90):
self.die()
elif(self.y>=SCREEN_HEIGHT):
self.die()
#elif(self.surface==screen):
#self.die()
'''elif(self.color ==RED or self.color ==TEAL or self.color==YELLOW or self.color==PURPLE):
if(self.surface==screen and gamestate ==GAMEPLAY):
self.die();'''
for cs in colorswitches:
if(cs.y >= self.y-self.rad*2):
self.color = cs.color
colorswitches.remove(cs)
def die(self):
self.dying_counter = 0
self.dead = True
for i in range(50):
temp = ExplosionBall(self.surface, self.x, self.y)
self.explosion_balls.append(temp)
def update(self):
if(not self.dead):
self.vel -= 0.5
self.y -= self.vel
if(cam.y >= self.y-SCREEN_HEIGHT/2):
cam.y = self.y-SCREEN_HEIGHT/2
self.collision_detection()
elif(self.dead and self.dead_counter <= 80):
self.dead_counter+=1
for xball in self.explosion_balls:
xball.update()
else:
global score, highscore, gamestate
gamestate = GAMEOVER
if(score > highscore):
highscore = score
def draw(self):
x = int(self.x-cam.x)
y = int(self.y-cam.y)
if(self.y > 10000):
self.y = 9000
if(not self.dead):
pygame.gfxdraw.aacircle(self.surface, x, y, self.rad, self.color)
pygame.gfxdraw.filled_circle(self.surface, x, y, self.rad, self.color)
elif(self.dead_counter <= 80):
dc = self.dead_counter
#pygame.gfxdraw.aacircle(self.surface, x, y, self.rad-dc, self.color)
#pygame.gfxdraw.filled_circle(self.surface, x, y, self.rad-dc, self.color)
for ball in self.explosion_balls:
ball.draw()
# ball = Ball(screen)
color_switch = ColorSwitch(screen, SCREEN_WIDTH/2, 250)
def restart():
global cam, ball, obstacles, score, stars
cam = Camera()
ball = Ball(screen)
del stars[:]
del obstacles[:]
del colorswitches[:]
for i in range(20):
# o_type = random.randint(0,1)
o_type = 0
if(o_type == 0):
temp = Obstacle(screen, SCREEN_WIDTH/2, -600*i)
obstacles.append(temp)
temp_colorswitch = ColorSwitch(screen, SCREEN_WIDTH/2, -600*i+300)
elif(o_type == 1):
temp = Obstacle(screen, SCREEN_WIDTH/2, -600*i, 300,45,1)
temp2 = Obstacle(screen, SCREEN_WIDTH/2, -600*i, temp.rad-temp.thickness*2-5, 180+45, -1)
obstacles.append(temp)
obstacles.append(temp2)
t = random.randint(0,1)
if(t == 0):
col = RED
else:
col = YELLOW
temp_colorswitch = ColorSwitch(screen, SCREEN_WIDTH/2, -600*i+300, col)
elif(o_type == 2):
temp = Obstacle(screen, SCREEN_WIDTH/2-100, -600*i, 200, 45, 1)
temp2 = Obstacle(screen, SCREEN_WIDTH/2+100, -600*i, 200, 45, -1)
obstacles.append(temp)
obstacles.append(temp2)
temp_colorswitch = ColorSwitch(screen, SCREEN_WIDTH/2, -600*i+300, TEAL)
temp_star = Star(screen, SCREEN_WIDTH/2, -600*i)
stars.append(temp_star)
colorswitches.append(temp_colorswitch)
score = 0
def handle_events():
global gamestate
for e in pygame.event.get():
if(e.type == pygame.QUIT):
return False
if(e.type == pygame.KEYDOWN):
if(e.key == pygame.K_ESCAPE):
return False
elif(e.key == pygame.K_SPACE):
if(gamestate == GAMEPLAY):
ball.vel = 8
#elif(gamestate==GAMEPLAY and )
elif(gamestate == GAMEOVER):
restart()
gamestate = GAMEPLAY
elif(gamestate == MENU):
restart()
gamestate = GAMEPLAY
return True
def draw_ui():
screen.blit(font.render(str(score), True, WHITE), (10, 10))
if(highscore >= 1):
w = SCREEN_WIDTH/5
for i in range(6):
pygame.draw.line(screen, WHITE, (w*i-20-cam.x, -600*(highscore-1)-cam.y), (w*i+20-cam.x, -600*(highscore-1)-cam.y), 10)
x, y = int(SCREEN_WIDTH/2), int(SCREEN_HEIGHT/2)
menu_obstacle = Obstacle(screen, x, y,200, 45)
menu_obstacle2 = Obstacle(screen, x, y,250, 45+180,-1)
menu_obstacle3 = Obstacle(screen, x, y,310, 45+90)
menu_obstacle.thickness = 15
menu_obstacle2.thickness = 20
title_obstacle = Obstacle(screen, 210, 65, 50, 90)
title_obstacle2 = Obstacle(screen, 310, 65, 50, 0, -1)
title_obstacle.thickness = 7
title_obstacle2.thickness = 7
def draw_menu():
screen.blit(menu_font.render("C L R", True, WHITE), (130, 40))
screen.blit(menu_font.render("SWITCH", True, WHITE), (130, 100))
menu_obstacle.update()
menu_obstacle2.update()
menu_obstacle3.update()
title_obstacle.update()
title_obstacle2.update()
menu_obstacle.draw()
menu_obstacle2.draw()
menu_obstacle3.draw()
pygame.draw.circle(screen, (70,70,70), (x,y), 80)
points = ((x-20, y-40), (x-20, y+40), (x+35, y))
pygame.gfxdraw.aapolygon(screen, points, WHITE)
pygame.gfxdraw.filled_polygon(screen, points, WHITE)
pygame.gfxdraw.aacircle(screen, x, y, 155, (20,20,20))
pygame.gfxdraw.aacircle(screen, x, y, 156, (20,20,20))
pygame.gfxdraw.aacircle(screen, x, y, 130, (20,20,20))
title_obstacle.draw()
title_obstacle.y-=1
title_obstacle.draw()
title_obstacle.y+=1
pygame.gfxdraw.aacircle(screen, 210, 65, 27, (20,20,20))
title_obstacle2.draw()
title_obstacle2.y-=1
title_obstacle2.draw()
title_obstacle2.y+=1
#ball.draw()
def draw_game_over():
#screen.blit(font.render("GAME OVER", True, WHITE), (SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2))
screen.blit(font.render("S C O R E", True, WHITE), (SCREEN_WIDTH/2-50, 120))
screen.blit(menu_font.render(str(score), True, WHITE), (SCREEN_WIDTH/2-10, 150))
screen.blit(font.render("B E S T S C O R E", True, WHITE), (SCREEN_WIDTH/2-100, 250))
screen.blit(menu_font.render(str(highscore), True, WHITE), (SCREEN_WIDTH/2-10, 290))
#screen.blit(retry, (int(SCREEN_WIDTH/2-retry.get_height()/2), int(SCREEN_HEIGHT*7/12)))
while(handle_events()):
clock.tick(80)
screen.fill((20,20,20))
if(gamestate == MENU):
draw_menu()
elif(gamestate == GAMEPLAY):
for obstacle in obstacles:
obstacle.update()
ball.update()
for star in stars:
star.update()
for obstacle in obstacles:
if(obstacle.y+obstacle.rad/2-cam.y >= 0 and obstacle.y-obstacle.rad/2-cam.y <= SCREEN_HEIGHT):
obstacle.draw()
for star in stars:
if(star.y+13-cam.y >= 0 and star.y-13-cam.y <= SCREEN_HEIGHT):
star.draw()
for cs in colorswitches:
if(cs.y+cs.rad-cam.y >= 0 and cs.y-cs.rad-cam.y <= SCREEN_HEIGHT):
cs.draw()
ball.draw()
draw_ui()
elif(gamestate == GAMEOVER):
draw_game_over()
pygame.display.flip()
pygame.quit()