We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Sprites

In our game, asteroids are visually represented as circles, and the player is a triangle. However, detecting collisions between circles and triangles is hard. To avoid this problem, we can cheat a bit: the player will secretly be a circle (for the purposes of collision detection).

The red circle won't be visible in the game; we just need to know it exists.

Throughout this project, we will provide some of the code for you, like the class below. We want you to focus on OOP concepts not the game physics. As such, we've pre-written some of the code you'll need, like the CircleShape class below.

Assignment

In Pygame, there's a base Sprite class that represents visual objects.

Let's create a CircleShape class that inherits from Sprite to represent objects in our game that are treated as circles (even if they aren't, like the player's ship).

Create a new circleshape.py file and paste in the following code:

import pygame


# Base class for game objects
class CircleShape(pygame.sprite.Sprite):
    containers: tuple[pygame.sprite.Group, ...]

    def __init__(self, x: float, y: float, radius: float) -> None:
        # we will be using this later
        if hasattr(self, "containers"):
            super().__init__(*self.containers)
        else:
            super().__init__()

        self.position: pygame.Vector2 = pygame.Vector2(x, y)
        self.velocity = pygame.Vector2(0, 0)
        self.radius = radius

    def draw(self, screen: pygame.Surface) -> None:
        # must override
        pass

    def update(self, dt: float) -> None:
        # must override
        pass

CircleShape extends the Sprite class to store 3 additional attributes specific to our game:

Later we'll write subclasses of CircleShape and override the draw and update methods with the logic for each particular game object.

Run and submit the CLI tests from the root of the project.