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

Draw Player

Let's draw a spaceship for the player!

When you use a constant or class from another file (like constants.py), remember to import it at the top of your file, for example: from constants import PLAYER_RADIUS.

Assignment

    • PLAYER_RADIUS = 20 (the radius of the player's ship)
    • LINE_WIDTH = 2 (the width of the lines that draw the player's ship)
  1. # in the Player class
    def triangle(self) -> list[pygame.Vector2]:
        forward = pygame.Vector2(0, 1).rotate(self.rotation)
        right = pygame.Vector2(0, 1).rotate(self.rotation + 90) * self.radius / 1.5
        a = self.position + forward * self.radius
        b = self.position - forward * self.radius - right
        c = self.position - forward * self.radius + right
        return [a, b, c]
    
    • The screen object
    • A color (use "white")
    • A list of points (use the list returned by a call to the self.triangle() function)
    • A line width (use the one in your constants.py file)
    • x = SCREEN_WIDTH / 2
    • y = SCREEN_HEIGHT / 2

Draw the player after filling the screen with black, but before flipping the screen.

Run the game for at least a few seconds, so that the log file gets populated.

If everything worked, you should see a black screen with a white triangle in the middle. Progress! Run and submit the CLI tests.