-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
297 lines (260 loc) · 8.8 KB
/
app.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
import pygame
pygame.init()
winWidth = 852
winHeight = 480
win = pygame.display.set_mode((winWidth, winHeight))
pygame.display.set_caption("First Game")
walkRight = [
pygame.image.load('R1.png'),
pygame.image.load('R2.png'),
pygame.image.load('R3.png'),
pygame.image.load('R4.png'),
pygame.image.load('R5.png'),
pygame.image.load('R6.png'),
pygame.image.load('R7.png'),
pygame.image.load('R8.png'),
pygame.image.load('R9.png')
]
walkLeft = [
pygame.image.load('L1.png'),
pygame.image.load('L2.png'),
pygame.image.load('L3.png'),
pygame.image.load('L4.png'),
pygame.image.load('L5.png'),
pygame.image.load('L6.png'),
pygame.image.load('L7.png'),
pygame.image.load('L8.png'),
pygame.image.load('L9.png')
]
bg = pygame.image.load('bg.jpg')
char = pygame.image.load('standing.png')
clock = pygame.time.Clock()
bulletSound = pygame.mixer.Sound('bullet.wav')
hitSound = pygame.mixer.Sound('hit.wav')
music = pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1)
score = 0
class player(object):
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.jumpCount = 10
self.left = False
self.right = False
self.walkCount = 0
self.standing = True
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
def draw(self, win):
if self.walkCount + 1 >= 9:
self.walkCount = 0
if not(self.standing):
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x,self.y))
else:
win.blit(walkLeft[0], (self.x,self.y))
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
def hit(self):
self.x = 60
self.y = 410
self.walkCount = 0
self.isJump = False
self.jumpCount = 10
font1 = pygame.font.SysFont('comicsans', 100)
text = font1.render('-5',1,(255,0,0))
win.blit(text,(winWidth//2 - text.get_width()//2,winHeight//2 - text.get_height()//2))
pygame.display.update()
i = 0
while i < 100:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 301
pygame.quit()
print('dude got hit')
class projectile(object):
def __init__(self,x,y,radius,color,facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
class enemy(object):
walkRight = [
pygame.image.load('R1E.png'),
pygame.image.load('R2E.png'),
pygame.image.load('R3E.png'),
pygame.image.load('R4E.png'),
pygame.image.load('R5E.png'),
pygame.image.load('R6E.png'),
pygame.image.load('R7E.png'),
pygame.image.load('R8E.png'),
pygame.image.load('R9E.png'),
pygame.image.load('R10E.png'),
pygame.image.load('R11E.png')
]
walkLeft = [
pygame.image.load('L1E.png'),
pygame.image.load('L2E.png'),
pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'),
pygame.image.load('L5E.png'),
pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'),
pygame.image.load('L8E.png'),
pygame.image.load('L9E.png'),
pygame.image.load('L10E.png'),
pygame.image.load('L11E.png')
]
def __init__(self,x,y,width,height,end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x,self.end]
self.walkCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 10
self.visible = True
def draw(self,win):
self.move()
if self.visible:
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
win.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
pygame.draw.rect(win, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
pygame.draw.rect(win, (0,128,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
def hit(self):
if self.health > 1:
self.health -= 1
else:
self.visible = False
print('hit')
def redrawGameWindow():
win.blit(bg, (0,0))
text = font.render('Score: ' + str(score), 1, (0,0,0))
win.blit(text, (740,10))
dude.draw(win)
for goblin in goblins:
goblin.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
#main loop
font = pygame.font.SysFont('comicsans',30,True)
dude = player(300, 410, 64, 64)
#goblin = enemy(100, 410, 64, 64, 450)
goblins = [enemy(100, 410, 64, 64, 450)]
shootLoop = 0
bullets = []
run = True
while run:
clock.tick(27)
if shootLoop > 0:
shootLoop += 1
if shootLoop >3:
shootLoop = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for goblin in goblins:
if goblin.visible:
if dude.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and dude.hitbox[1] + dude.hitbox[3] > goblin.hitbox[1]:
if dude.hitbox[0] + dude.hitbox[2] > goblin.hitbox[0] and dude.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]:
dude.hit()
if goblin.health > 5:
goblin.health = 10
else:
goblin.health += 5
if score >= 5:
score -= 5
elif score > 0 and score < 5:
score = 0
for bullet in bullets:
if goblin.visible:
if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
hitSound.play()
goblin.hit()
score += 1
bullets.pop(bullets.index(bullet))
if bullet.x < winWidth and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and shootLoop == 0:
if dude.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(dude.x + dude.width//2, dude.y + dude.height//2, 6, (0,0,0), facing))
bulletSound.play()
shootLoop = 1
if keys[pygame.K_LEFT] and dude.x > dude.vel:
dude.x -= dude.vel
dude.left = True
dude.right = False
dude.standing = False
elif keys[pygame.K_RIGHT] and dude.x < winWidth - dude.width:
dude.x += dude.vel
dude.right = True
dude.left = False
dude.standing = False
else:
dude.standing = True
dude.walkCount = 0
if not(dude.isJump):
if keys[pygame.K_UP]:
dude.isJump = True
dude.walkCount = 0
else:
if dude.jumpCount >= -10:
neg = 1
if dude.jumpCount < 0:
neg = -1
dude.y -= (dude.jumpCount ** 2) // 2 * neg
dude.jumpCount -= 1
else:
dude.isJump = False
dude.jumpCount = 10
redrawGameWindow()
pygame.quit()