Menu
CoddyTech

Circles, Lines, Polygons

Part of the The Game Loop section of Coddy's Pygame journey. Lesson 9 of 32.

So far you have drawn rectangles. pygame.draw has more shapes, and they all start the same way: the surface to draw on, then the colour.

pygame.draw.circle(screen, (255, 196, 0), (100, 80), 30)
pygame.draw.line(screen, (240, 240, 240), (0, 200), (480, 200), 3)
pygame.draw.polygon(screen, (60, 200, 120), [(240, 40), (280, 120), (200, 120)])

A circle takes its centre and its radius. Unlike a rectangle, it is placed by its middle, not its top left corner. A radius of 30 makes a circle 60 pixels across.

A line takes its two end points and, last, its width in pixels. Without a width it is 1 pixel thick.

A polygon takes a list of corner points and fills the shape between them, joining the last point back to the first. Three points make a triangle, four a diamond.

challenge icon

Challenge

Easy

Draw a sailboat at sea, after the fill:

  1. The sun: a yellow (255, 196, 0) circle centred at (380, 80) with radius 40.
  2. The sea: a blue (80, 160, 255) line from (0, 280) to (480, 280), 4 pixels thick.
  3. The sail: a red (235, 80, 80) polygon with corners (240, 120), (240, 250) and (160, 250).

Try it yourself

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Sailboat")
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((30, 30, 46))
    # draw the sun, the sea and the sail here
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

All lessons in The Game Loop

Practice on your own: Pygame playground