-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathentity.py
executable file
·368 lines (272 loc) · 10.4 KB
/
entity.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Pitfall v1.0
By Danilo Lekovic for Game Design 12
entity.py
Class files representing different entities
"""
import pygame as pg
# Using Pygame's built-in vectors
vec = pg.math.Vector2
# Barrel are found above ground
# Player has to jump over these
# Barrels also roll
class Barrel(pg.sprite.Sprite):
# Initialize class
def __init__(self, x, y):
super().__init__()
# Handles animation
self.animationTimer = 0
self.animationIndex = 0
self.animations = \
{'All': [pg.image.load('Sprites/Environment/Barrel/1.png'),
pg.image.load('Sprites/Environment/Barrel/2.png')]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
# Changes sprite
def changeSprite(self, index=None):
if index is None:
index = self.animationIndex
self.pos = vec(self.pos.x, self.pos.y)
self.image = self.animations['All'][index].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(self.pos.x, self.pos.y)
# Updates in main loop
def update(self):
if self.animationIndex == 0:
self.animationIndex = 1
else:
self.animationIndex = 0
self.changeSprite(self.animationIndex)
self.pos.x -= 10
# Ghost are found underground
# Player cannot touch these
# Ghosts also move
class Ghost(pg.sprite.Sprite):
# Initialize class
def __init__(self, x, y):
super().__init__()
# Handle animations
self.animationTimer = 0
self.animationIndex = 0
self.animations = \
{'All': [pg.image.load('Sprites/Environment/Ghost/1.png'),
pg.image.load('Sprites/Environment/Ghost/2.png')]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
# Change sprite
def changeSprite(self, index=None):
if index is None:
index = self.animationIndex
self.pos = vec(self.pos.x, self.pos.y)
self.image = self.animations['All'][index].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(self.pos.x, self.pos.y)
# Update in main loop
def update(self):
self.animationTimer += 1
self.changeSprite(self.animationIndex)
self.pos.x -= 5
if self.animationTimer >= 3:
self.animationTimer = 0
if self.animationIndex == 0:
self.animationIndex = 1
else:
self.animationIndex = 0
# Rats are found underground
# Player cannot touch these
# Rats also move
class Rat(pg.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.animationTimer = 0
self.animationIndex = 0
self.animations = \
{'All': [pg.image.load('Sprites/Environment/Rat/1.png'),
pg.image.load('Sprites/Environment/Rat/2.png')]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
def changeSprite(self, index=None):
if index is None:
index = self.animationIndex
self.pos = vec(self.pos.x, self.pos.y)
self.image = self.animations['All'][index].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(self.pos.x, self.pos.y)
def update(self):
self.animationTimer += 1
self.changeSprite(self.animationIndex)
self.pos.x -= 8
if self.animationTimer >= 3:
self.animationTimer = 0
if self.animationIndex == 0:
self.animationIndex = 1
else:
self.animationIndex = 0
# Snakes are found above ground
# Player cannot touch these
# Snakes don't move
class Snake(pg.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.animationTimer = 0
self.animationIndex = 0
self.animations = \
{'All': [pg.image.load('Sprites/Environment/Snake/1.png'),
pg.image.load('Sprites/Environment/Snake/2.png')]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
def changeSprite(self, index=None):
if index is None:
index = self.animationIndex
self.pos = vec(self.pos.x, self.pos.y)
self.image = self.animations['All'][index].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(self.pos.x, self.pos.y)
def update(self):
self.animationTimer += 1
self.changeSprite(self.animationIndex)
if self.animationTimer >= 5:
self.animationTimer = 0
if self.animationIndex == 0:
self.animationIndex = 1
else:
self.animationIndex = 0
# Pits are formed above ground
# Player cannot touch these
# Pits sink and reopen
class Pit(pg.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.animationTimer = 0
self.animationIndex = 0
# Full animation
self.animations = {'All': [
pg.image.load('Sprites/Environment/Pit/1.png'),
pg.image.load('Sprites/Environment/Pit/2.png'),
pg.image.load('Sprites/Environment/Pit/3.png'),
pg.image.load('Sprites/Environment/Pit/4.png'),
pg.image.load('Sprites/Environment/Pit/5.png'),
pg.image.load('Sprites/Environment/Pit/6.png'),
pg.image.load('Sprites/Environment/Pit/7.png'),
pg.image.load('Sprites/Environment/Pit/8.png'),
pg.image.load('Sprites/Environment/Pit/8.png'),
pg.image.load('Sprites/Environment/Pit/7.png'),
pg.image.load('Sprites/Environment/Pit/6.png'),
pg.image.load('Sprites/Environment/Pit/5.png'),
pg.image.load('Sprites/Environment/Pit/4.png'),
pg.image.load('Sprites/Environment/Pit/3.png'),
pg.image.load('Sprites/Environment/Pit/2.png'),
pg.image.load('Sprites/Environment/Pit/1.png'),
]}
self.pos = vec(x, y)
self.originalPos = self.pos
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
self.originalWidth = self.rect.width
# Disappearing
self.drawing = True
self.disappearingTimer = 0
def changeSprite(self, index=None):
if index is None:
index = self.animationIndex
self.pos = vec(self.pos.x, self.pos.y)
self.image = self.animations['All'][index].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(self.pos.x, self.pos.y)
# Disappears for 40 intervals
def update(self):
self.animationTimer += 1
if not self.drawing:
self.disappearingTimer += 1
if self.disappearingTimer >= 40:
self.drawing = True
self.disappearingTimer = 0
self.animationTimer = 0
self.animationIndex = 8
else:
if self.animationIndex == 7:
self.drawing = False
if self.animationIndex >= len(self.animations['All']) - 1:
self.animationIndex = 0
self.changeSprite(self.animationIndex)
self.pos.x = self.originalPos.x + self.originalWidth / 2 \
- self.rect.width / 2
if self.animationTimer >= 2:
self.animationTimer = 0
if self.animationIndex >= len(self.animations['All']) \
- 1:
self.animationIndex = 0
else:
self.animationIndex += 1
# Walls are found below the ground
# Player can't go through these
class Wall(pg.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.animations = \
{'All': [pg.image.load('Sprites/Environment/Wall.png')]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
# Ladders extend from above the ground to below the ground
# Player can climb these
class Ladder(pg.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.animations = \
{'All': [pg.image.load('Sprites/Environment/EntireLadder.png'
)]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
# Coins are found above and below the ground
# These are currency
class Coin(pg.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.animationTimer = 0
self.animationIndex = 0
self.animations = {'All': [
pg.image.load('Sprites/Environment/Coin/1.png'),
pg.image.load('Sprites/Environment/Coin/2.png'),
pg.image.load('Sprites/Environment/Coin/3.png'),
pg.image.load('Sprites/Environment/Coin/4.png'),
pg.image.load('Sprites/Environment/Coin/5.png'),
pg.image.load('Sprites/Environment/Coin/6.png'),
pg.image.load('Sprites/Environment/Coin/7.png'),
pg.image.load('Sprites/Environment/Coin/8.png'),
]}
self.pos = vec(x, y)
self.image = self.animations['All'][0].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(x, y)
def changeSprite(self, index=None):
if index is None:
index = self.animationIndex
self.image = self.animations['All'][index].convert_alpha()
self.rect = self.image.get_rect()
self.rect.move_ip(self.pos.x, self.pos.y)
def update(self):
self.animationIndex += 1
if self.animationIndex >= len(self.animations['All']) - 1:
self.animationIndex = 0
self.changeSprite(self.animationIndex)
if self.animationTimer >= 2:
self.animationTimer = 0
if self.animationIndex >= len(self.animations['All']) - 1:
self.animationIndex = 0
else:
self.animationIndex += 1