-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlocks_And_Objects.py
More file actions
250 lines (200 loc) · 10.5 KB
/
Blocks_And_Objects.py
File metadata and controls
250 lines (200 loc) · 10.5 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
import pygame, random
from Support import ImportFolder
# The parent class for each class in this file
class Tile(pygame.sprite.Sprite): # Used to create a template for each tile, inherit from the parent class 'Sprite'
def __init__(self,pos,size,TileSize,type): # position, tile size
super().__init__() # Run the initialisation routine of pygame's parent class
self.type = type # Set type of tile
self.image = pygame.Surface((size[0],size[1])) # Create a surface with the dimesnions of the given tile size, where size is a tuple with [0] being its x dimension size
self.rect = self.image.get_rect(midbottom = (pos[0]+(size[0]/2), pos[1]+(TileSize))) # Give the rectangle for the surface the same dimensions as the image, where pos is a tuple for the top left
# of the image. I have chosen to force this into being the 'midbottom' as when the class is inherited from
# animated objects, not all objects are the same height and width (e.g spring).
# If deciding to draw the abstract level, the different blocks are colour coded as such
if type == 'Normal':
self.image.fill('Gray')
elif type == 'Damaging':
self.image.fill('Red')
elif type == 'Platform':
self.image.fill('Blue')
# Will reset level upon player death
def ResetLevel(self, XOffset):
self.rect.x -= XOffset
# Will scroll through the level, shifting each tile to the left or right
def update(self,XShift):
self.rect.x += XShift
# Parent class for 'enemy' and the other objects such as 'respawn point'
class AnimatedObject(Tile):
def __init__(self,pos,size,TileSize,AnimSpeed,type,Animations,FullPath): # position, tile size
super().__init__(pos,size,TileSize,type) # Run the initialisation routine of pygame's parent class
self.Animations = Animations # Set arrays for each animation state
self.ImportAssets(FullPath)
self.Status = 'Idle'
self.PreviousStatus = 'Idle'
self.FrameIndex = 0
self.AnimationSpeed = AnimSpeed
self.image = self.Animations['Idle'][self.FrameIndex] # Fill the surface with the animation frame 'idle'
self.rect = self.image.get_rect(midbottom = self.rect.midbottom) # Give the rectangle for the surface the same dimensions as the image
def ImportAssets(self, Path):
# Add each image to the arrays for later use
for Animation in self.Animations.keys():
FullPath = Path + Animation
self.Animations[Animation] = ImportFolder(FullPath)
def update(self, XShift): # Override tile's update method as we must animate the object as well
# Shifting the respawn point, and animating it
self.rect.x += XShift
self.Animate() # Each class has unique animation routines, hence no need to define it and for it to be overriden in each instance of the class
# --- Enemies ---
# Parent Class for each enemy
class Enemy(AnimatedObject):
def __init__(self, AnimationsPath, Speed, SpawnPoint, Size, TileSize):
AnimSpeed = 0.15
type = 'Enemy'
Animations = {'Attack':[], 'Idle':[]}
super().__init__(SpawnPoint, Size, TileSize, AnimSpeed, type, Animations, AnimationsPath)
# Enemy's Attributes
self.Speed = Speed
rand = random.randint(1,2) # Adds a bit of randomness to a line of enemies
if rand == 1: self.FacingRight = True
else: self.FacingRight = False
def Death(self):
self.kill()
def Animate(self):
# Reset frame index when switching animation statuses
if self.Status != self.PreviousStatus:
self.PreviousStatus = self.Status
self.FrameIndex = 0
Animation = self.Animations[self.Status]
self.Animation = Animation
# Loop over frame index
self.FrameIndex += self.AnimationSpeed
if self.FrameIndex >= len(Animation):
self.FrameIndex = 0
# Set enemy's image and rect
AnimFrame = Animation[int(self.FrameIndex)]
if self.FacingRight:
self.image = AnimFrame
else:
self.image = pygame.transform.flip(AnimFrame, True, False) # We want to flip in the x axis, but not the y axis
self.rect = self.image.get_rect(midbottom = self.rect.midbottom)
# First type of enemy
class BlindingSpider(Enemy):
def __init__(self, SpawnPoint, TileSize):
Speed = random.randint(2,3)
Size = (80, 32)
AnimationsPath = 'SpriteSheets/Enemies/Blinding Spider/'
super().__init__(AnimationsPath, Speed, SpawnPoint, Size, TileSize)
# Second type of enemy
class FlowerEnemy(Enemy):
def __init__(self, SpawnPoint, TileSize):
Speed = 0.7
Size =()####################
AnimationsPath = 'SpriteSheets/Enemies/Flower Enemy/'
super().__init__(AnimationsPath, Speed, SpawnPoint, Size, TileSize)
# Third type of enemy
class WheelBot(Enemy):
def __init__(self, SpawnPoint, TileSize):
Speed = random.randint(4,5)
Size = (43,26)
AnimationsPath = 'SpriteSheets/Enemies/Wheel Bot/'
super().__init__(AnimationsPath, Speed, SpawnPoint, Size, TileSize)
# --- Collectable Items ---
# Parent class for each item
class CollectableItem(AnimatedObject):
def __init__(self, pos, size, TileSize, type, ToMove, Path):
Animations = {'Idle':[]}
super().__init__(pos, size, TileSize, 0, type, Animations, Path) # Run the initialisation routine of Animated object (and hence tile)
# Set attributes
InitialYPos = self.rect.y
self.UpperBound = InitialYPos
self.LowerBound = InitialYPos + ToMove
self.MoveUp = False
def Animate(self):
# Object floats up and down, so shift y pos up to a certain point, then back down
self.rect = self.image.get_rect(center = self.rect.center) # Give the rectangle for the surface the same dimensions as the image
YPos = self.rect.y
if self.MoveUp:
self.rect.y -= 1
if YPos < self.UpperBound:
self.MoveUp = False
else:
self.rect.y += 1
if YPos > self.LowerBound:
self.MoveUp = True
# Golden Gear
class GoldenGear(CollectableItem):
def __init__(self, pos, size, TileSize, type, ToMove):
Path = 'SpriteSheets/AnimatedObjects/Golden Gear/'
super().__init__(pos, size, TileSize, type, ToMove, Path) # Run the initialisation routine of Animated object (and hence tile)
# Double Jump
class DoubleJump(CollectableItem):
def __init__(self, pos, size, TileSize, type, ToMove):
Path = 'SpriteSheets/AnimatedObjects/Double Jump/'
super().__init__(pos, size, TileSize, type, ToMove, Path) # Run the initialisation routine of Animated object (and hence tile)
# Dash
class Dash(CollectableItem):
def __init__(self, pos, size, TileSize, type, ToMove):
Path = 'SpriteSheets/AnimatedObjects/Dash/'
super().__init__(pos, size, TileSize, type, ToMove, Path) # Run the initialisation routine of Animated object (and hence tile)
# --- General Animated Objects ---
# Portal
class Portal(AnimatedObject):
def __init__(self, pos, size, TileSize, type):
Animations = {'Idle':[], 'Warp':[]}
Path = 'SpriteSheets/AnimatedObjects/Portal/'
AnimSpeed = 0.15
super().__init__(pos, size, TileSize, AnimSpeed, type, Animations, Path) # Run the initialisation routine of Animated object (and hence tile)
def Animate(self): # Creating the animate function to be used inside the parent class
Animation = self.Animations[self.Status]
self.Animation = Animation
# Loop over frame index
self.FrameIndex += self.AnimationSpeed
if self.FrameIndex >= len(Animation):
self.FrameIndex = 0
# Set image and rect of the respawn point
self.image = Animation[int(self.FrameIndex)]
self.rect = self.image.get_rect(midbottom = self.rect.midbottom)
# Respawn point
class RespawnPoint(AnimatedObject):
def __init__(self, pos, size, TileSize, type):
Animations = {'Idle':[], 'Saving':[], 'Startup':[]}
Path = 'SpriteSheets/AnimatedObjects/Save/'
AnimSpeed = 0.15
super().__init__(pos, size, TileSize, AnimSpeed, type, Animations, Path) # Run the initialisation routine of Animated object (and hence tile)
# Set attributes of respawn point
self.ID = 0 # Where ID is the number of the respawn point
self.Saving = False
def Animate(self):
# If we have swapped to a new status, set animation speed (back) to default and frame index to 0
if self.Status != self.PreviousStatus:
self.PreviousStatus = self.Status
self.FrameIndex = 0
Animation = self.Animations[self.Status]
self.Animation = Animation
# Loop over frame index
self.FrameIndex += self.AnimationSpeed
if int(self.FrameIndex) == len(self.Animation) - 1:
if self.Status == 'Saving':
self.Status = 'Idle'
self.FrameIndex = 0
# Set image and rect of the respawn point
self.image = Animation[int(self.FrameIndex)]
self.rect = self.image.get_rect(midbottom = self.rect.midbottom)
# Spring
class Spring(AnimatedObject):
def __init__(self, pos, size, TileSize, type):
Animations = {'Idle':[], 'Bounce':[]}
Path = 'SpriteSheets/AnimatedObjects/Spring/'
AnimSpeed = 0.3
super().__init__(pos, size, TileSize, AnimSpeed, type, Animations, Path) # Run the initialisation routine of Animated object (and hence tile)
def Animate(self):
Animation = self.Animations[self.Status]
# Add onto frame index
self.FrameIndex += self.AnimationSpeed
# Set image and rect of spring
self.image = Animation[int(self.FrameIndex)]
self.rect = self.image.get_rect(midtop = self.rect.midtop)
if int(self.FrameIndex) == len(Animation) - 1:
self.FrameIndex = 0
self.Status = 'Idle'
self.Animation = self.Animations[self.Status]
self.image = self.Animation[self.FrameIndex]