-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
373 lines (314 loc) · 10.9 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
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
import pygame
pygame.init()
font = pygame.font.Font("freesansbold.ttf",128)
win = pygame.display.set_mode((1400,700))
run = True
clock = pygame.time.Clock()
bg = pygame.image.load('sky.jpg')
bg = pygame.transform.scale(bg,(1400,700))
lb = pygame.image.load('longblock.png')
lb = pygame.transform.scale(lb,(757,70))
sb = pygame.image.load('block2.png')
sb = pygame.transform.scale(sb,(160,80))
bi = pygame.transform.scale(pygame.image.load('bullllet.png'),(10,10))
chars = [pygame.transform.scale(pygame.image.load('c'+str(i+1)+'.png'),(57,76)) for i in range(3)]
chars.append(pygame.transform.scale(pygame.image.load('c4.png'),(84,76)))
puimglist = [pygame.transform.scale(pygame.image.load(["heart","fire","shield"][i]+".png"),(35,35)) for i in range(3)]
puimglist[1] = pygame.transform.scale(puimglist[1],(85,85))
lw = 757
lh = 70
sw = 160
sh = 80
bullets0 = []
bullets1 = []
fire0 = False
fire1 = False
gameover = False
pygame.mixer.music.load('backgroundmusic.mp3')
class Char:
def __init__(self,x,y,state,xmomentum,ymomentum,a,facing,jump,color,hp,armor):
self.x=x
self.y=y
self.state=state
self.xmomentum=xmomentum
self.ymomentum=ymomentum
self.a=a
self.facing=facing
self.k = 0
self.jump=jump
self.color = color
self.hp = hp
self.armor = armor
def draw(self,win):
self.x += self.xmomentum
self.y += self.ymomentum
if self.state==0:
ci = win.blit([pygame.transform.flip(chars[0],True,False),chars[0]][self.facing],(self.x,self.y))
elif self.state==1:
ci = win.blit(chars[1+round(self.a)%2],(self.x,self.y))
self.a+=1/15
elif self.state==2:
ci = win.blit(pygame.transform.flip(chars[1+round(self.a)%2],True,False),(self.x,self.y))
self.a+=1/15
elif self.state==3:
ci = win.blit([pygame.transform.flip(chars[3],True,False),chars[3]][self.facing],(self.x,self.y))
self.k += 1
if self.k ==10:
self.state=0
if b1.colliderect(ci):
if (self.y+76-10 <= 590):
if self.ymomentum >= 0:
self.y = 590-76
self.ymomentum = 0
self.jump = False
if (self.y >= 590 + lh):
self.y = 590+lh
self.ymomentum = 0
if (self.x+57<=340+5):
self.x = 340-57
if (self.x>=340+lw-5):
self.x = 340+lw
elif b2.colliderect(ci):
if (self.y + 76 - 10 <= 240):
if self.ymomentum >= 0:
self.y = 240 - 76
self.ymomentum = 0
self.jump = False
if (self.y >= 240 + lh-15):
self.y = 240+lh
self.ymomentum = 0
if (self.x+57<=340+5):
self.x = 340-57-5
if (self.x>=340+lw-5):
self.x = 340+lw+5
elif b3.colliderect(ci):
if (self.y + 76 - 10 <= 410):
if self.ymomentum >= 0:
self.y = 410 - 76
self.ymomentum = 0
self.jump = False
if (self.y >= 410 + sh-10):
self.y = 410+sh
self.ymomentum = 0
if (self.x+57<=100+5):
self.x = 100-57
if (self.x>=100+sw-5):
self.x = 100+sw
elif b4.colliderect(ci):
if (self.y + 76 - 10 <= 410):
if self.ymomentum >= 0:
self.y = 410 - 76
self.ymomentum = 0
self.jump = False
if (self.y >= 410 + sh-10):
self.y = 410+sh
self.ymomentum = 0
if (self.x+57<=1190+5):
self.x = 1190-57
if (self.x>=1190+sw-5):
self.x = 1190+sw
for bullet in [bullets1,bullets0][self.color]:
if bullet.b.colliderect(ci)&(self.armor==False):
[bullets1,bullets0][self.color].remove(bullet)
self.hp -= 10
global putype, pu
if pu:
if puimg.colliderect(ci):
if putype == 0:
self.hp += 30
if self.hp>100:
self.hp = 100
if putype == 1:
if self.armor is False:
self.hp -= 30
if putype ==2:
if self.armor is False:
self.armor = True
global f
f = [0 for i in range(2)]
f[self.color] = 180
pu = False
if self.armor==True:
pygame.draw.rect(win,(240,240,160),(self.x+32-25+(self.facing-1)*8, self.y-25,50*f[self.color]/180,5))
f[self.color] -= 1
if f[self.color] == 0 :
self.armor= False
if self.y>=700:
self.hp = 0
if self.hp <=0:
global losecolor
losecolor = [(0,0,255),(255,0,0)][self.color]
global gameover
gameover = True
pygame.draw.rect(win,[(255,0,0),(0,0,255)][self.color],(self.x+64/2-50/2+(self.facing-1)*(8),self.y-15,self.hp/2,5))
def fire(self):
self.k = 0
[bullets0,bullets1][self.color].append(Bullet((self.x-2,self.x+84)[self.facing],self.y+20,self.facing))
class Bullet:
def __init__(self,x,y,facing):
self.x=x
self.y=y
self.facing=facing
self.b = 0
def drawbullet(self,win):
self.x+=(self.facing*2-1)*10
self.b = win.blit(bi,(self.x,self.y))
c = Char(500,160,0,0,0,0,1,0,0,100,False)
c2 = Char(700,500,0,0,0,0,1,0,1,100,False)
h = 0
n = 0
bgx = 0
import random
global pu
pu = False
global newvar
newvar=480
def game():
global coordinates
global pu,newvar
if pu==False:
pur = random.randint(1,4000)
if pur <30:
newvar = 480
global putype
putype = random.randint(0,2)
puloc = random.randint(0,3)
pu = True
coordinates = [(random.randint(340,340+lw-35-50*(putype==1)),590-43-40*(putype==1)),
(random.randint(340,340+lw-35-50*(putype==1)),240-43-40*(putype==1)),
(random.randint(100,100+sw-35-50*(putype==1)),410-43-40*(putype==1)),
(random.randint(1190,1190+sw-35-50*(putype==1)),410-43-40*(putype==1))][puloc]
if pu:
newvar -= 1
if newvar == 0:
pu = False
global puimg
puimg = win.blit(puimglist[putype],(coordinates))
global fire0
if fire0 is True:
global h
h += 1
if h == 10:
h = 0
fire0 = False
global fire1
if fire1 is True:
global n
n += 1
if n == 10:
n = 0
fire1 = False
global b1,b2,b3,b4
b1 = win.blit(lb, (340, 590))
b2 = win.blit(lb, (340, 240))
b3 = win.blit(sb, (100, 410))
b4 = win.blit(sb, (1190, 410))
for event in pygame.event.get():
if event.type == pygame.QUIT:
global run
run = False
if event.type == pygame.KEYDOWN:
# FOR RIGHT PLAYER
if event.key == pygame.K_RIGHT:
c.state = 1
c.facing = 1
c.xmomentum = 5
if event.key == pygame.K_LEFT:
c.state = 2
c.facing = 0
c.xmomentum = -5
if event.key == pygame.K_RETURN:
if not fire0:
fire0 = True
c.state = 3
c.fire()
if event.key == pygame.K_UP:
if c.jump is False:
c.jump = True
c.ymomentum = -15
# FOR WASD PLAYER
if event.key == pygame.K_d:
c2.state = 1
c2.facing = 1
c2.xmomentum = 5
if event.key == pygame.K_a:
c2.state = 2
c2.facing = 0
c2.xmomentum = -5
if event.key == pygame.K_SPACE:
if not fire1:
fire1 = True
c2.state = 3
c2.fire()
if event.key == pygame.K_w:
if c2.jump is False:
c2.jump = True
c2.ymomentum = -15
if event.type == pygame.KEYUP:
# FOR RIGHT
if event.key == pygame.K_RIGHT:
if c.xmomentum > 0:
c.xmomentum = 0
c.state = 0
if event.key == pygame.K_LEFT:
if c.xmomentum < 0:
c.xmomentum = 0
c.state = 0
# FOR WASD
if event.key == pygame.K_d:
if c2.xmomentum > 0:
c2.xmomentum = 0
c2.state = 0
if event.key == pygame.K_a:
if c2.xmomentum < 0:
c2.xmomentum = 0
c2.state = 0
if c.ymomentum <= 5:
c.ymomentum += 1
if c2.ymomentum <= 5:
c2.ymomentum += 1
if c.jump & (c.ymomentum <= 0):
c.ymomentum += -0.5
if c2.jump & (c2.ymomentum <= 0):
c2.ymomentum += -0.5
for bullet in bullets0:
bullet.drawbullet(win)
for bullet in bullets1:
bullet.drawbullet(win)
# ENDING STUFF
c.draw(win)
c2.draw(win)
def gameoverfunc():
retrytext = font.render("RETRY?", True, losecolor)
txt = win.blit(retrytext, (400, 300))
for event in pygame.event.get():
if event.type==pygame.QUIT:
global run
run = False
if event.type==pygame.MOUSEBUTTONDOWN:
if txt.collidepoint(event.pos[0],event.pos[1]):
global gameover,c,c2,h,n,bgx,bullets0,bullets1,fire0,fire1
gameover = False
c = Char(500, 160, 0, 0, 0, 0, 1, 0, 0, 100,False)
c2 = Char(700, 500, 0, 0, 0, 0, 1, 0, 1, 100,False)
h = 0
n = 0
bgx = 0
bullets0 = []
bullets1 = []
fire0 = False
fire1 = False
pygame.mixer.music.play(-1)
while run:
win.blit(bg, (bgx, 0))
win.blit(bg, (bgx + 1400, 0))
bgx -= 2
if bgx <= -1400:
bgx = 0
if gameover == False:
game()
else:
gameoverfunc()
clock.tick(60)
pygame.display.update()
pygame.quit()