-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainlevel2.py
475 lines (407 loc) · 14.3 KB
/
mainlevel2.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import pygame
import join
import random
import health
#initialisation
pygame.init()
#Caption and display
screen=pygame.display.set_mode((1200,700))
pygame.display.set_caption("Sprites")
#tower
tower_img=pygame.image.load('Asset 24.png')
tower_img=pygame.transform.scale(tower_img,(265*1.1,472*1.1))
destroytower_img=pygame.image.load('Asset 31.png')
destroytower_img=pygame.transform.scale(destroytower_img,(265*1.1,472*1.1))
#tower part
overlay_img=pygame.image.load('part.png')
overlay_img=pygame.transform.scale(overlay_img,(265*1.1,472*1.1))
#counters
flag=0
flag2=0
flag3=0
flag4=0
flag5=0
flag6=0
# hp
healthbar=health.hp(15,35,150,20,300)
#screen
black=(0,0,0)
bg=(0,100,50)
back=pygame.image.load('2level.png')
back=pygame.transform.scale(back,(1200,700))
#Character animation
enemy_1_list=[]
enemy_3_list=[]
enemy_1_steps=6
enemy_3_steps=6
char1_cooldown=150
char3_cooldown=150
frame_char=0
frame3_char=0
enemy1hp=[]
enemy3hp=[]
last_char_update=pygame.time.get_ticks()
last_char3_update=pygame.time.get_ticks()
#Enemy_1
No_of_enemy_1=10
enemy_1_x_axis=[400]
enemy_1_y_axis=[1195]
enemy_rect=[]
for i in range(No_of_enemy_1-1):
enemy_1_y_axis.append(random.randrange(1200,2500,500))
enemy_1_x_axis.append(random.randrange(300,450,40))
enemy_1_image=pygame.image.load('enemy1.2_try.png').convert_alpha()
enemy_1_sheet=join.formed(enemy_1_image)
for x in range(enemy_1_steps):
enemy_1_list.append(enemy_1_sheet.get_image(x,150,200,.6,black))
enemy_1_x_axis.sort()
print(enemy_1_x_axis)
for i in range(No_of_enemy_1):
enemy_rect.append(enemy_1_list[0].get_rect())
enemy_rect[i].x=enemy_1_y_axis[i]
enemy_rect[i].y=enemy_1_x_axis[i]
enemy1hp.append(4)
#Enemy_3
No_of_enemy_3=10
enemy3_rect=[]
enemy_3_y_axis=[1900]
enemy_3_x_axis=[]
enemy_3_image=pygame.image.load('enemy3.2_try.png').convert_alpha()
enemy_3_sheet=join.formed(enemy_3_image)
for i in range(No_of_enemy_3):
enemy_3_x_axis.append(random.randrange(300,450,40))
enemy_3_y_axis.append(random.randrange(2000,3000,500))
enemy_3_x_axis.sort()
print(enemy_3_x_axis)
for x in range(enemy_3_steps):
enemy_3_list.append(enemy_3_sheet.get_image(x,150,200,.6,black))
for i in range(No_of_enemy_3):
enemy3_rect.append(enemy_3_list[0].get_rect())
enemy3_rect[i].x=enemy_3_y_axis[i]
enemy3_rect[i].y=enemy_3_x_axis[i]
enemy3hp.append(8)
#static Character animation
enemy_list=[]
enemy_steps=6
char_cooldown=150
framestats_char=0
laststats_char_update=pygame.time.get_ticks()
#static villians
enemy=1
counter4killcanon=0
enemy_image=pygame.image.load('d.png').convert_alpha()
enemy_sheet=join.formed(enemy_image)
for x in range(enemy_steps):
enemy_list.append(enemy_sheet.get_image(x,500,2000,.6,black))
# static enemy2
enemy3_list=[]
enemy3_steps=6
char_cooldown=150
framestats_char=0
laststats3_char_update=pygame.time.get_ticks()
#static villians
enemy3=1
enemy3_image=pygame.image.load('enemy2_attack.png').convert_alpha()
enemy3_sheet=join.formed(enemy3_image)
for x in range(enemy3_steps):
enemy3_list.append(enemy3_sheet.get_image(x,200,200,.6,black))
#cannon animation
cannon1_list=[]
cannon_steps=5
animation_cooldown=200
frame_cannon=0
cannon_hp=100
cannon1_image=pygame.image.load('cannon.png').convert_alpha()
cannon1_sheet=join.formed(cannon1_image)
for x in range(cannon_steps):
cannon1_list.append(cannon1_sheet.get_image(x,128,652,1.7,black))
#cannon2
cannon2_list=[]
cannon_steps=5
animation_cooldown=200
frame_cannon=0
cannon2_hp=100
cannon2_image=pygame.image.load('cannon.png').convert_alpha()
cannon2_sheet=join.formed(cannon2_image)
for x in range(cannon_steps):
cannon2_list.append(cannon2_sheet.get_image(x,128,652,1.7,black))
#cannon3
cannon3_list=[]
cannon_steps=5
animation_cooldown=200
frame_cannon=0
cannon3_hp=100
cannon3_image=pygame.image.load('cannon.png').convert_alpha()
cannon3_sheet=join.formed(cannon3_image)
for x in range(cannon_steps):
cannon3_list.append(cannon3_sheet.get_image(x,128,652,1.7,black))
#cannon ball
cannonballImg = pygame.image.load('cannon-ball.png')
cannonballX = 210
cannonballY = 500
ball_rect=pygame.Rect(210,500, 16, 16)
#print(ball_rect)
cannonballX_change = 10
cannonballY_change = 0
cannonball_state = "ready"
cannonballdead="dead"
cannonballpower=1
def fire_cannonball(x,y):
global cannonball_state
cannonball_state = "fire"
screen.blit(cannonballImg, (x , y))
#cannon ball2
cannonball2Img = pygame.image.load('cannon-ball.png')
cannonball2X = 210
cannonball2Y = 420
ball2_rect=pygame.Rect(210,420, 16, 16)
cannonball2X_change = 10
cannonball2Y_change = 0
cannonball2_state = "ready"
def fire_cannonball2(x,y):
global cannonball2_state
cannonball2_state = "fire"
screen.blit(cannonball2Img, (x , y))
#cannon ball3
cannonball3Img = pygame.image.load('cannon-ball.png')
cannonball3X = 210
cannonball3Y = 340
ball3_rect=pygame.Rect(210,340, 16, 16)
cannonball3X_change = 10
cannonball3Y_change = 0
cannonball3_state = "ready"
def fire_cannonball3(x,y):
global cannonball3_state
cannonball3_state = "fire"
screen.blit(cannonball3Img, (x , y))
#Create animation main_char
main_char_list=[]
main_char_steps=6
last_update=pygame.time.get_ticks()
animation_cooldown=200
frame=0
main_char_image=pygame.image.load('try3.png').convert_alpha()
main_char_sheet=join.formed(main_char_image)
for x in range(main_char_steps):
main_char_list.append(main_char_sheet.get_image(x,120,2000,.6,black))
count=No_of_enemy_1+No_of_enemy_3
run = True
while run:
screen.fill(bg)
screen.blit(back,(0,0))
towerhp=cannon_hp+cannon2_hp+cannon3_hp
healthbar.hp=towerhp
healthbar.draw(screen)
if towerhp<=0:
cannonball_state="finaldead"
cannonball2_state="finaldead"
cannonball3_state="finaldead"
pygame.draw.rect(screen,(0,255,0),ball_rect,5)
pygame.draw.rect(screen,(0,255,0),ball2_rect,5)
pygame.draw.rect(screen,(0,255,0),ball3_rect,5)
for i in range(No_of_enemy_1):
pygame.draw.rect(screen,(255,0,0),enemy_rect[i],5)
if enemy_rect[i].colliderect(ball_rect):
pygame.draw.rect(screen,(0,0,255),enemy_rect[i],5)
print("fire")
cannonballX=210
ball_rect.x=210
cannonball_state="ready"
enemy1hp[i]-=cannonballpower
if enemy1hp[i]<=0:
count-=1
enemy_1_x_axis[i]=-9000
enemy_rect[i].x=-9000
if enemy_rect[i].colliderect(ball2_rect):
pygame.draw.rect(screen,(0,0,255),enemy_rect[i],5)
cannonball2X=210
ball2_rect.x=210
cannonball2_state="ready"
enemy1hp[i]-=cannonballpower
if enemy1hp[i]<=0:
count-=1
enemy_1_x_axis[i]=-9000
enemy_rect[i].x=-9000
if enemy_rect[i].colliderect(ball3_rect):
pygame.draw.rect(screen,(0,0,255),enemy_rect[i],5)
cannonball3X=210
ball3_rect.x=210
cannonball3_state="ready"
enemy1hp[i]-=cannonballpower
if enemy1hp[i]<=0:
count-=1
enemy_1_x_axis[i]=-9000
enemy_rect[i].x=-9000
for i in range(No_of_enemy_3):
pygame.draw.rect(screen,(255,0,0),enemy3_rect[i],5)
if enemy3_rect[i].colliderect(ball_rect):
pygame.draw.rect(screen,(0,0,255),enemy3_rect[i],5)
print("fire")
cannonballX=210
ball_rect.x=210
cannonball_state="ready"
enemy3hp[i]-=cannonballpower
if enemy3hp[i]<=0:
count-=1
enemy_3_x_axis[i]=-9000
enemy3_rect[i].x=-9000
if enemy3_rect[i].colliderect(ball2_rect):
pygame.draw.rect(screen,(0,0,255),enemy3_rect[i],5)
cannonball2X=210
ball2_rect.x=210
cannonball2_state="ready"
enemy3hp[i]-=cannonballpower
if enemy3hp[i]<=0:
count-=1
enemy_3_x_axis[i]=-9000
enemy3_rect[i].x=-9000
if enemy3_rect[i].colliderect(ball3_rect):
pygame.draw.rect(screen,(0,0,255),enemy3_rect[i],5)
cannonball3X=210
ball3_rect.x=210
cannonball3_state="ready"
enemy3hp[i]-=cannonballpower
if enemy3hp[i]<=0:
count-=1
enemy_3_x_axis[i]=-9000
enemy3_rect[i].x=-9000
current_time=pygame.time.get_ticks()
if current_time-last_update>=animation_cooldown:
frame+=1
frame_cannon+=1
last_update=current_time
if frame>=len(main_char_list):
frame=0
if frame_cannon>=len(cannon1_list):
frame_cannon=0
# static villian
char_time=pygame.time.get_ticks()
if char_time-laststats_char_update>=char_cooldown:
laststats_char_update=char_time
if enemy_1_y_axis[0] <= 275 or counter4killcanon==1:
counter4killcanon=1
framestats_char+=1
if framestats_char>=len(enemy_list):
framestats_char=0
if flag==0:
cannon_hp-=(1*c)
if flag2==0:
cannon2_hp-=(1*c2)
if flag3==0:
cannon3_hp-=(1*c3)
if flag4==0:
cannon_hp-=(1*c4)
if flag5==0:
cannon2_hp-=(1*c5)
if flag6==0:
cannon3_hp-=(1*c6)
#Character display
#enemy1
char_time=pygame.time.get_ticks()
if char_time-last_char_update>=char1_cooldown:
frame_char+=1
last_char_update=char_time
if frame_char>=len(enemy_1_list):
frame_char=0
for i in range(No_of_enemy_1):
if enemy_1_y_axis[i] <= 275:
screen.blit(enemy_list[framestats_char],(80,enemy_1_x_axis[i]))
else:
screen.blit(enemy_1_list[frame_char],(enemy_1_y_axis[i],enemy_1_x_axis[i]))
enemy_1_y_axis[i]-=1.0
if enemy_rect[i].x > 275:
enemy_rect[i].x-=.9
c=0
for i in range(No_of_enemy_1):
if enemy_1_y_axis[i] <= 275 and enemy_1_y_axis[i]>0 and enemy_1_x_axis[i]>=440:
c+=1
c2=0
for i in range(No_of_enemy_1):
if enemy_1_y_axis[i] <= 275 and enemy_1_y_axis[i] >= 0 and enemy_1_x_axis[i] >=370 and enemy_1_x_axis[i] < 440:
c2+=1
c3=0
for i in range(No_of_enemy_1):
if enemy_1_y_axis[i] <= 275 and enemy_1_y_axis[i] >=0 and enemy_1_x_axis[i] >370 and enemy_1_x_axis[i] <=400:
c3+=1
#enemy3
char_time=pygame.time.get_ticks()
if char_time-last_char3_update>=char1_cooldown:
frame3_char+=1
last_char3_update=char_time
if frame3_char>=len(enemy_1_list):
frame3_char=0
for i in range(No_of_enemy_3):
if enemy_3_y_axis[i] <= 275 and enemy_3_y_axis[i] >=0:
screen.blit(enemy3_list[framestats_char],(270,enemy_3_x_axis[i]))
else:
screen.blit(enemy_3_list[frame3_char],(enemy_3_y_axis[i],enemy_3_x_axis[i]))
enemy_3_y_axis[i]-=1.0
if enemy3_rect[i].x > 275:
enemy3_rect[i].x-=.9
c4=0
for i in range(No_of_enemy_3):
if enemy_3_y_axis[i] <= 275 and enemy_3_y_axis[i] >=0 and enemy_3_x_axis[i]>=440:
c4+=1
c5=0
for i in range(No_of_enemy_3):
if enemy_3_y_axis[i] <= 275 and enemy_3_y_axis[i] >=0 and enemy_3_x_axis[i] >=370 and enemy_3_x_axis[i] < 440:
c5+=1
c6=0
for i in range(No_of_enemy_3):
if enemy_3_y_axis[i] <= 275 and enemy_3_y_axis[i] >=0 and enemy_3_x_axis[i] >370 and enemy_3_x_axis[i] <=400:
c6+=1
#image display
if count!=0:
screen.blit(main_char_list[frame],(60,350))
for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
if cannonball_state == "ready":
for i in range(cannon_steps):
screen.blit(cannon1_list[i],(170,400))
fire_cannonball(cannonballX, cannonballY)
if event.key == pygame.K_2:
if cannonball2_state == "ready":
for i in range(cannon_steps):
screen.blit(cannon2_list[i],(170,320))
fire_cannonball2(cannonball2X, cannonball2Y)
if event.key == pygame.K_3:
if cannonball3_state == "ready":
for i in range(cannon_steps):
screen.blit(cannon3_list[i],(170,240))
fire_cannonball3(cannonball3X, cannonball3Y)
# OVEMENT MECHANICS OF CANNONBALL 1
if cannonballX >= 1200:
cannonballX = 210
ball_rect.x=210
cannonball_state = "ready"
if cannonball_state == "fire":
fire_cannonball(cannonballX, cannonballY)
ball_rect.x+=cannonballX_change
cannonballX += cannonballX_change
#MOVEMENT MECHANICS OF CANNONBALL 2
if cannonball2X >= 1200:
cannonball2X = 210
ball2_rect.x=210
cannonball2_state = "ready"
if cannonball2_state == "fire":
fire_cannonball2(cannonball2X, cannonball2Y)
ball2_rect.x+=cannonball2X_change
cannonball2X += cannonball2X_change
#MOVEMENT MECHANICS OF CANNONBALL 3
if cannonball3X >= 1200:
cannonball3X = 210
ball3_rect.x=210
cannonball3_state = "ready"
if cannonball3_state == "fire":
fire_cannonball3(cannonball3X, cannonball3Y)
ball3_rect.x+=cannonball3X_change
cannonball3X += cannonball3X_change
screen.blit(cannon1_list[0],(170,400))
screen.blit(cannon2_list[0],(170,320))
screen.blit(cannon3_list[0],(170,240))
pygame.display.flip()
pygame.display.update()
pygame.quit()