-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyGameCode
More file actions
352 lines (287 loc) · 11 KB
/
Copy pathPyGameCode
File metadata and controls
352 lines (287 loc) · 11 KB
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
import pygame
import random
pygame.init()
win = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("Everlasted")
exp = 0
font = pygame.font.SysFont('comicsans', 64, False, False)
hpfont = pygame.font.SysFont('comicsans', 32, False, False)
completefont = pygame.font.SysFont('comicsans', 128, True, True)
# self object
class player(object):
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')]
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.walkCount = 0
self.slashdmg = 10
self.isSlash = False
self.health = 100
self.isHit = False
self.hitbox = (self.x + 20, self.y, 30, 50)
self.standing = True
self.left = False
self.right = False
def draw(self, win):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if not (self.standing):
if self.left:
win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
win.blit(self.walkRight[0], (self.x, self.y))
else:
win.blit(self.walkLeft[0], (self.x, self.y))
self.hitbox = (self.x + 20, self.y, 30, 50)
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.vel = 5
self.end = end
self.path = [self.x, self.end]
self.walkCount = 0
self.coldmg = 10
self.isHit = False
self.health = 50
self.hitbox = (self.x + 20, self.y, 30, 50)
self.left = False
self.right = True
def draw(self, win):
self.hitbox = (self.x + 20, self.y, 30, 50)
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.left == False:
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
# for e in enemies:
# if e.isHit == False:
# pygame.draw.rect(win, (255, 0, 0), self.hitbox)
# else:
# pygame.draw.rect(win, (255, 255, 255), self.hitbox)
swordimg = pygame.image.load('Sword.png')
swordimg = pygame.transform.scale(swordimg, (70 ,40))
swordimgatk = pygame.transform.rotate(swordimg, 150)
swordimgr = pygame.transform.flip(swordimg, True, False)
swordimgatkr = pygame.transform.rotate(swordimgr, 210)
class sword(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.hitbox = (self.x, self.y, self.width, self.height)
def draw(self, win):
keys = pygame.key.get_pressed()
pygame.draw.rect(win, (255,0,0), self.hitbox)
if keys[pygame.K_RETURN] and anicool >= 500:
if Elige.right:
win.blit(swordimgatkr, (Elige.x + 50, Elige.y + 5))
if Elige.left:
win.blit(swordimgatk, (Elige.x - 50, Elige.y + 5))
else:
if Elige.right and Elige.isSlash == False:
win.blit(swordimgr, (Elige.x - 5, Elige.y))
if Elige.left:
win.blit(swordimg, (Elige.x + 5, Elige.y))
slashcool = 0
dmgcool = 0
cool = 0
cool2 = 0
enemies = []
bg = pygame.image.load('BG.png')
bg = pygame.transform.scale(bg, (1280, 720))
Elige = player(640, 480, 30, 50)
Sword = sword(640, 480, 50, 20)
enemies.append(enemy(1280, 150000, 30, 50, 450))
enemies.append(enemy(1280, 150000, 30, 50, 450))
for i in range(3):
enemies.append(enemy(random.randint(10, 30), random.randint(0, 650), 50, 50, 450))
# enemies.append(enemy(1000, 250, 50, 50, 450))
def EligeMovement(velocity):
keys = pygame.key.get_pressed()
vel = velocity
if keys[pygame.K_w] and Elige.y > vel:
Elige.y -= vel
if keys[pygame.K_s] and Elige.y < 720 - Elige.height - Elige.vel:
Elige.y += vel
if keys[pygame.K_a] and Elige.x > Elige.vel:
Elige.x -= vel
Elige.left = True
Elige.right = False
Elige.standing = False
elif keys[pygame.K_d] and Elige.x < 1280 - Elige.width - Elige.vel:
Elige.x += vel
Elige.right = True
Elige.left = False
Elige.standing = False
else:
Elige.standing = True
Elige.walkCount = 0
def EnemyMovement(velocity):
vel = velocity
for e in enemies:
if Elige.y < e.y and e.y > vel and not (e.y == Elige.y + (Elige.width - 10)):
e.y -= vel
if Elige.x < e.x and e.x > e.vel and not (e.x == Elige.x + (Elige.width - 10)):
e.x -= vel
e.left = True
e.right = False
if Elige.y > e.y and e.y < 720 - e.height - e.vel and not (e.y + (e.width - 10) == Elige.y):
e.y += vel
if Elige.x > e.x and e.x < 1280 - e.width - e.vel and not (e.x + (e.width - 10) == Elige.x):
e.x += vel
e.left = False
e.right = True
def SwordMovement():
if keys[pygame.K_RETURN]:
if Elige.left == True and Sword.x >= Elige.x - 30:
Sword.x -= 20
if Elige.right == True and Sword.x <= Elige.x + 20:
Sword.x += 20
# IntroScreen
'''
intro = False
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (Elige.x, Elige.y, Elige.width, Elige.height))
pygame.display.update()
'''
counter = 5
def redrawGameWindow():
win.blit(bg, (0, 0))
global counter
Sword.draw(win)
text = font.render('exp: ' + str(exp), 1, (0, 0, 0))
win.blit(text, (1100, 10))
Elige.draw(win)
text = hpfont.render('' + str(Elige.health), 1, (0, 0, 0))
win.blit(text, (Elige.x + 15, Elige.y - 5))
for e in enemies:
e.draw(win)
text = hpfont.render('' + str(e.health), 1, (0, 0, 0))
win.blit(text, (e.x + 20, e.y - 8))
pygame.display.update()
if stageComplete:
complete = completefont.render('Stage Completed', 1, (255, 255, 255))
win.blit(complete, (250, 300))
pygame.display.update()
if Elige.health <= 0 and Elige.y < 1600:
Elige.y += counter * 2
counter += counter
if Elige.health <= 0:
complete = completefont.render('Level Failed', 1, (255, 255, 255))
win.blit(complete, (250, 300))
pygame.display.update()
# enemies2.draw(win)
# text = hpfont.render('' + str(enemies2.health), 1, (0, 0, 0))
# win.blit(text, (enemies2.x + 25, enemies2.y + 15))
# pygame.display.update()
stageComplete = False
anicool = 0
cool3 = 0
# Mainloop
run = True
while run:
if len(enemies) == 2:
stageComplete = True
while stageComplete:
redrawGameWindow()
EligeMovement(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if Elige.health <= 0:
redrawGameWindow()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
redrawGameWindow()
slashcool += pygame.time.get_ticks() - cool
cool = pygame.time.get_ticks()
dmgcool += pygame.time.get_ticks() - cool2
cool2 = pygame.time.get_ticks()
anicool += pygame.time.get_ticks() - cool3
cool3 = pygame.time.get_ticks()
keys = pygame.key.get_pressed()
EligeMovement(1)
EnemyMovement(2)
# if keys[pygame.K_SPACE] and slashcool >= 500:
# EligeMovement(35)
# Elige.isSlash = True
# slashcool = 0
if keys[pygame.K_SPACE] and slashcool >= 300:
EligeMovement(150)
slashcool = 0
else:
EligeMovement(5)
if keys[pygame.K_RETURN] and anicool >= 300:
Elige.isSlash = True
if Elige.right == True:
Sword = sword(Elige.x + 45, Elige.y + 20, 70, 30)
if Elige.left == True:
Sword = sword(Elige.x - 55, Elige.y + 20, 70, 30)
anicool = 0
else:
Elige.isSlash = False
if Elige.isSlash == True:
for e in enemies:
if Sword.hitbox[1] < e.hitbox[1] + e.hitbox[3] and Sword.hitbox[1] + Sword.hitbox[3] > e.hitbox[1]:
if Sword.hitbox[0] + Sword.hitbox[2] > e.hitbox[0] and Sword.hitbox[0] < e.hitbox[0] + \
e.hitbox[2]:
if dmgcool >= 1000:
e.isHit = True
e.health -= Elige.slashdmg
dmgcool = 0
else:
e.health -= 0
else:
for e in enemies:
if Elige.hitbox[1] < e.hitbox[1] + e.hitbox[3] and Elige.hitbox[1] + Elige.hitbox[3] > e.hitbox[1]:
if Elige.hitbox[0] + Elige.hitbox[2] > e.hitbox[0] and Elige.hitbox[0] < e.hitbox[0] + e.hitbox[2]:
if dmgcool >= 1000:
Elige.isHit = True
Elige.health -= e.coldmg
dmgcool = 0
else:
Elige.health -= 0
Elige.isHit = False
for e in enemies:
if e.health <= 0:
enemies.remove(e)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()