-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.py
412 lines (349 loc) · 15.5 KB
/
engine.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
#Main engine
import pygame
pygame.init()
screen = pygame.display.set_mode((1280, 720))
blackFadeScreen = pygame.Surface((1280,720))
#ui = pygame.Surface(())
ui = pygame.image.load('graphics/charUI.png')
ui = ui.convert_alpha()
ui = pygame.transform.scale(ui,(275,90))
#blackFadeScreen.fill((255,255,255))
#blackFadeScreen.setAlpha()
done = False
framerate = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
hasKey= False
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.image.load('graphics/character.png')
self.surf = pygame.transform.scale(self.surf,(76,112))
self.animation_speed = 10
self.animation_speed = self.animation_speed
self.rect = self.surf.get_rect()
self.rect.move_ip(600,400) #player spawn point
self.walk_up_ani = [pygame.transform.scale(pygame.image.load('graphics/player_walking_upF1.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_upF2.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_upF3.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_upF4.png'),(92,124))]
self.walk_left_ani = [pygame.transform.scale(pygame.image.load('graphics/player_walking_leftF1.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_leftF2.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_leftF3.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_leftF4.png'),(92,124))]
self.walk_right_ani = [pygame.transform.scale(pygame.image.load('graphics/player_walking_rightF1.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_rightF2.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_rightF3.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_rightF4.png'),(92,124))]
self.walk_down_ani = [pygame.transform.scale(pygame.image.load('graphics/player_walking_downF1.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_downF2.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_downF3.png'),(92,124)),
pygame.transform.scale(pygame.image.load('graphics/player_walking_downF4.png'),(92,124))]
######## this converts all the colors or something which increases performance############
for i in range(0,4):
self.walk_up_ani[i] = self.walk_up_ani[i].convert_alpha()
self.walk_left_ani[i] = self.walk_left_ani[i].convert_alpha()
self.walk_right_ani[i] = self.walk_right_ani[i].convert_alpha()
self.walk_down_ani[i] = self.walk_down_ani[i].convert_alpha()
#self.noi = 4
self.current_frame = 0
self.time = 0
self.isCasting = False
self.manaTime = 0
def update(self, pressed_keys):
###########auto mana regen over time####################
self.manaTime += 1
if (self.manaTime % 60 == 0 and self.mana < 34):
self.mana += self.mana_regen
######### walking up animation ##############
if pressed_keys[pygame.K_UP]:
self.leftspeed = 4
self.rightspeed = 4
self.downspeed = 4
self.direction = 1
self.rect.move_ip(0, -self.upspeed)
self.time += 1
if (self.time % 12 == 0):
if(self.current_frame>2):
self.current_frame = 0
else:
self.current_frame+=1
self.surf = self.walk_up_ani[self.current_frame]
######### walking down animation ##############
elif pressed_keys[pygame.K_DOWN]:
self.leftspeed = 4
self.rightspeed = 4
self.upspeed = 4
self.direction = 3
self.rect.move_ip(0, self.downspeed)
self.time += 1
if (self.time % 12 == 0):
if(self.current_frame>2):
self.current_frame = 0
else:
self.current_frame+=1
self.surf = self.walk_down_ani[self.current_frame]
######### walking left animation ##############
elif pressed_keys[pygame.K_LEFT]:
self.downspeed = 4
self.rightspeed = 4
self.upspeed = 4
self.direction = 4
self.rect.move_ip(-self.leftspeed, 0)
self.time += 1
if (self.time % 12 == 0):
if(self.current_frame>2):
self.current_frame = 0
else:
self.current_frame+=1
self.surf = self.walk_left_ani[self.current_frame]
######### walking right animation ##############
elif pressed_keys[pygame.K_RIGHT]:
self.leftspeed = 4
self.downspeed = 4
self.upspeed = 4
self.direction = 2
self.rect.move_ip(self.rightspeed, 0)
self.time += 1
if (self.time % 12 == 0):
if(self.current_frame>2):
self.current_frame = 0
else:
self.current_frame+=1
self.surf = self.walk_right_ani[self.current_frame]
elif pressed_keys[pygame.K_2]:
if (self.mana >= 5 and self.isCasting==False):
#cast spell here
self.isCasting = True
self.mana = self.mana - 5
#########this makes it so you only cast once per keypress##########
elif event.type == pygame.KEYUP:
if event.key == pygame.K_2:
self.isCasting = False
speed = 4
leftspeed = 4
rightspeed =4
downspeed =4
upspeed =4
x = 640
y = 350
health = 34#34 is max health, or 170pixels/5
mana = 20
mana_regen = 1
direction = 1
class Goblin:
x = 0
y = 0
health = 3
class Wall(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(x,y,width,height)
class Room1:
roomimage = pygame.image.load('graphics/room2.png')
roomimage = roomimage.convert()
roomimage = pygame.transform.scale(roomimage,(1280,720))
wallLeft1 = Wall(0,0,100,720)
wallLeft2 = Wall(0,0,0,0)
wallRight1 = Wall(1180,0,100,240)
wallRight2 = Wall(1180,420,100,160)
wallTop1 = Wall(0,0,570,50)
wallTop2 = Wall(720,0,560,50)
wallBottom1 = Wall(0,600,1280,60)
wallBottom2 = Wall(0,0,0,0)
wallMiddle = Wall(300,0,180,330)
wallMiddle2 = Wall(0,0,0,0)
wallMiddle3 = Wall(0,0,0,0)
topDoor = Wall(570,0,150,10)
bottomDoor = Wall(0,0,0,0)
rightDoor = Wall(1200,240,100,180)
leftDoor = Wall(0,0,0,0)
class Room2:
def __init__(self):
self.roomimage = pygame.image.load('graphics/room4.png')
self.roomimage = self.roomimage.convert()
self.roomimage = pygame.transform.scale(self.roomimage,(1280,720))
wallLeft1 = Wall(0,0,100,720)
wallLeft2 = Wall(0,0,0,0)
wallRight1 = Wall(1180,0,100,240)
wallRight2 = Wall(1180,420,100,160)
wallTop1 = Wall(0,0,1280,50)
wallTop2 = Wall(0,0,0,0)
wallBottom1 = Wall(0,600,570,50)
wallBottom2 = Wall(720,600,560,50)
wallMiddle = Wall(310,300,160,130)
wallMiddle2 = Wall(790,300,160,130)
wallMiddle3 = Wall(0,0,0,0)
topDoor = Wall(0,0,0,0)
bottomDoor = Wall(570,620,150,40)
rightDoor = Wall(1180,240,100,180)
leftDoor = Wall(0,0,0,0)
class Room3:
def __init__(self):
self.roomimage = pygame.image.load('graphics/room5.png')
self.roomimage = self.roomimage.convert()
self.roomimage = pygame.transform.scale(self.roomimage,(1280,720))
wallLeft1 = Wall(0,0,100,240)
wallLeft2 = Wall(0,420,100,160)
wallRight1 = Wall(1180,0,100,720)
wallRight2 = Wall(0,0,0,0)
wallTop1 = Wall(0,0,570,50)
wallTop2 = Wall(720,0,560,50)
wallBottom1 = Wall(0,600,1280,60)
wallBottom2 = Wall(0,0,0,0)
wallMiddle = Wall(0,0,0,0)
wallMiddle2 = Wall(0,0,0,0)
wallMiddle3 = Wall(0,0,0,0)
topDoor = Wall(570,0,150,10)
bottomDoor = Wall(0,0,0,0)
rightDoor = Wall(0,0,0,0)
leftDoor = Wall(0,240,70,180)
class Room4:
def __init__(self):
self.roomimage = pygame.image.load('graphics/room6.png')
self.roomimage = self.roomimage.convert()
self.roomimage = pygame.transform.scale(self.roomimage,(1280,720))
wallLeft1 = Wall(0,0,100,240)
wallLeft2 = Wall(0,420,100,160)
wallRight1 = Wall(1180,0,100,720)
wallRight2 = Wall(0,0,0,0)
wallTop1 = Wall(0,0,570,50)
wallTop2 = Wall(720,0,560,50)
wallBottom1 = Wall(0,600,570,50)
wallBottom2 = Wall(720,600,560,50)
wallMiddle = Wall(0,0,0,0)
wallMiddle2 = Wall(0,0,0,0)
wallMiddle3 = Wall(0,0,0,0)
topDoor = Wall(570,0,150,10)
bottomDoor = Wall(570,620,150,40)
rightDoor = Wall(0,0,0,0)
leftDoor = Wall(0,240,70,180)
class Bossroom:
def __init__(self):
self.roomimage = pygame.image.load('graphics/bossroom3.png')
self.roomimage = self.roomimage.convert()
self.roomimage = pygame.transform.scale(self.roomimage,(1280,720))
wallLeft1 = Wall(0,0,100,720)
wallLeft2 = Wall(0,0,0,0)
wallRight1 = Wall(1180,0,100,720)
wallRight2 = Wall(0,0,0,0)
wallTop1 = Wall(0,0,1280,50)
wallTop2 = Wall(0,0,0,0)
wallBottom1 = Wall(0,600,570,50)
wallBottom2 = Wall(720,600,560,50)
wallMiddle = Wall(0,200,460,110)
wallMiddle2 = Wall(800,200,500,110)
wallMiddle3 = Wall(460,100,400,110)
topDoor = Wall(0,0,0,0)
bottomDoor = Wall(570,620,150,40)
rightDoor = Wall(0,0,0,0)
leftDoor = Wall(0,0,0,0)
#use this to update the current room when changing rooms
#def updateCurrentRoom(room):
#currentroom = room
def changeRooms(roomName):
timer = 0
global currentroom
currentroom = roomName
#blackFadeScreen.set_alpha(0)
#screen.blit(blackFadeScreen,(0,0))
#while timer < 2550:
#timer+=1
#print(timer)
#blackFadeScreen.set_alpha(timer%10)
#blackFadeScreen.set_alpha(100)
#screen.blit(blackFadeScreen,(0,0))
fadeAlpha = 1
def checkCollision(self, sprite1, sprite2):
col = pygame.sprite.collide_rect(sprite1, sprite2)
return(col)
player = Player()
firstroom = Room1()
secondroom = Room2()
thirdroom = Room3()
fourthroom = Room4()
bossroom = Bossroom()
currentroom = firstroom
sprites_alive = pygame.sprite.Group()
sprites_walls = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
## add monster sprites to this group to draw them to the screen
sprites_alive.add(player)
sprites_walls.add(firstroom.wallLeft1)
all_sprites.add(sprites_alive,sprites_walls)
fade = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
enemies=[Goblin(-5,114)]
for e in enemies:
e.update()
pressed_keys = pygame.key.get_pressed()
screen.blit(currentroom.roomimage,(0,0)) #draws the current map/room
pygame.draw.rect(screen,(255,0,0), (102,25,player.health*5,30),0) #draws the health bar
pygame.draw.rect(screen,(0,0,255), (102,68,player.mana*5,30),0) #draws the mana bar
screen.blit (ui,(10,10))#draws the ui to the screen
for entity in sprites_alive: #this draws the sprites in the sprites_alive group (player and monsters)
screen.blit(entity.surf, entity.rect)
###############These if statements check for collision with player and walls and if true sets the players speed to 0###############
if (checkCollision(pygame.sprite.Sprite, player,currentroom.wallLeft1) or checkCollision(pygame.sprite.Sprite, player,currentroom.wallLeft2)):
player.leftspeed =0
if (checkCollision(pygame.sprite.Sprite, player,currentroom.wallRight1) or checkCollision(pygame.sprite.Sprite, player,currentroom.wallRight2)):
player.rightspeed =0
if (checkCollision(pygame.sprite.Sprite, player,currentroom.wallTop1) or checkCollision(pygame.sprite.Sprite, player,currentroom.wallTop2)):
player.upspeed = 0
if (checkCollision(pygame.sprite.Sprite, player,currentroom.wallBottom1) or checkCollision(pygame.sprite.Sprite, player,currentroom.wallBottom2)):
player.downspeed = 0
if (checkCollision(pygame.sprite.Sprite, player, currentroom.wallMiddle) or checkCollision(pygame.sprite.Sprite, player, currentroom.wallMiddle2) or checkCollision(pygame.sprite.Sprite, player, currentroom.wallMiddle3)):
if (player.direction == 1):
player.upspeed =0
player.rect.move_ip(0, 1)
if (player.direction==2):
player.rightspeed = 0
player.rect.move_ip(-1, 0)
if (player.direction==3):
player.downspeed = 0
player.rect.move_ip(0, -1)
if (player.direction ==4):
player.leftspeed =0
player.rect.move_ip(1, 0)
###############These check for collision with the doors and loads new rooms and moves player#####################
if (checkCollision(pygame.sprite.Sprite, player, currentroom.topDoor)):
if (currentroom == firstroom):
changeRooms(secondroom)
player.rect.move_ip(0,500)
elif (currentroom == thirdroom):
changeRooms(fourthroom)
player.rect.move_ip(0,500)
elif (currentroom == fourthroom and player.hasKey == True):
changeRooms(bossroom)
player.rect.move_ip(0,500)
elif (currentroom == fourthroom and player.hasKey == False):
player.upspeed = 0
if (checkCollision(pygame.sprite.Sprite, player, currentroom.bottomDoor)):
if (currentroom == secondroom):
changeRooms(firstroom)
elif (currentroom == fourthroom):
changeRooms(thirdroom)
elif (currentroom == bossroom):
changeRooms(fourthroom)
player.rect.move_ip(0,-500)
if (checkCollision(pygame.sprite.Sprite, player, currentroom.rightDoor)):
if (currentroom == firstroom):
changeRooms(thirdroom)
elif (currentroom == secondroom):
changeRooms(fourthroom)
player.rect.move_ip(-1030,0)
if (checkCollision(pygame.sprite.Sprite, player, currentroom.leftDoor)):
if (currentroom == thirdroom):
changeRooms(firstroom)
elif (currentroom == fourthroom):
changeRooms(secondroom)
player.rect.move_ip(1020,0)
player.update(pressed_keys) ###this calls the update method in player which checks for keypresses and handles movement/attacks
#use the following for collision detection between player and enemies
#if pygame.sprite.spritecollideany(player, enemies):
#player takes damage and is pushed back?
##(102, 25) for health bar
##(102, 68) for mana bar
pygame.display.flip()
framerate.tick(60) #sets the framerate to 60 fps