timestep.Character
Base class for any moving objects in the game.
Character(x_pos, y_pos, image, rect) -> Character
-
timestep.Character.update
Method to control Character behaviour
-
timestep.Character.draw
Blit the Character's image
The base class for moving game objects. Derived classes should override the Character.update()
method and assign a Character.image
before calling super().__init__(x_pos, y_pos, self.image)
. For example:
class Player(timestep.Character):
# Constructer. Pass in the Character's startomg position.
def __init__(self, x_pos: float, y_pos: float) -> None:
# Create image, fill with colour, create rect
# This could also be an image loaded from the disk.
self.image = pygame.Surface((100, 100))
self.image.fill("red")
self.rect = self.image.get_frect()
# Call the parent class (Character) constructer, passing in the position, image and rect.
super().__init__(x_pos, y_pos, self.image)
update()
Method to control Character behaviour
update(*args, **kwargs) -> None
This method is intended to be overriden with your own behaviour (movement, collisions etc.) for the Character. The only requirement is that you call super().update()
first:
...
def update(self) -> None:
super().update()
# example movement
self.vel.y += gravity
self.vel.x *= self.friction
self.rect.x += self.vel.x
draw()
Blit the Character's image
draw(surface, alpha) -> None
Draws the Character.image
to the screen at the Character's rect position. Should be called only within a timestep.Timestep.render()
method (link). alpha is a float that is automatically passed in (explained in timestep.Timestep.render()
).