text
stringlengths
1
93.6k
# Blit the image of the current frame onto the surface
self.surface.blit(self.sprites[0], (0, 0))
# Create an empty surface to clean the main surface up after updating
self.empty_surface = pygame.Surface(self.sprites[0].get_size())
self.empty_surface.fill((225, 0, 225))
# Pause the animation
self.playb = True
# Create the reference to the own animation
self.copy = Animation
def play(self):
"""
Plays the animation, meaning surface and frame-number get updated over time
"""
self.playb = True
return self
def pause(self):
"""
Pauses the animation.
"""
self.playb = False
return self
def reset(self):
"""
Resets the animation to the first frame without pausing it.
"""
# Sets the current sprite to 0:
self.current_sprite = 0
# Makes the surface empty:
self.surface.blit(self.empty_surface, (0, 0))
# Blit the image of the new frame onto the surface
self.surface.blit(self.sprites[self.sprite_order[self.current_sprite][0]], (0, 0))
return self
def set_spritenr(self, nr):
"""
With this function, the current frame can be changed manually.
"""
self.current_sprite = nr
return self
def set_frames_per_image(self, frames_per_image):
"""
With this function, the speed of the animation can be changed.
The speed is frames per image, so the framerate is 1/speed.
"""
# Update the animation-speed:
self.sprite_order = frames_per_image
# Make sure self.sprite_order is a list containing the speed for every frame, (convert int to list)
self.format_sprite_order()
return self
def set_colorkey(self, color):
"""
Sets the colorkey of all sprites.
"""
for sprite in self.sprites:
sprite.set_colorkey(color)
return self
def _custom(self):
"""
This function is called at the same time update() is called.
It is used by classes who inherit from this one to add custom behaviour.
"""
pass
def format_sprite_order(self):
if type(self.sprite_order) is list:
if type(self.sprite_order[0]) is tuple:
# sprite_order is already formatted:
self.sprite_order = self.sprite_order
else:
# sprite_order consists of amounts of frames for sprites:
self.sprite_order = \
[(sprite, length) for sprite, length in zip(range(len(self.sprites)), self.sprite_order)]
else:
# sprite_order consists of one amount of frames for every sprite:
self.sprite_order = [(sprite, self.sprite_order) for sprite in range(len(self.sprites))]
def update(self):
"""
This method must be called every frame.
It updates the entire animation-object.
"""
# Execute the custom update method used by inherited classes:
self._custom()
# Make sure frames_per_image is a list:
self.format_sprite_order()
# If animation is currently playing, increase frame_counter
if self.playb:
self.frame_counter += 1