Last modified: Jan 07, 2025 By Alexander Williams

Python Pygame Draw Polygon Guide

Drawing shapes is a fundamental part of game development. Pygame, a popular Python library, makes it easy to draw polygons. This guide will show you how to use the pygame.draw.polygon function effectively.

What is Pygame?

Pygame is a set of Python modules designed for writing video games. It includes computer graphics and sound libraries. You can create games and multimedia applications with ease.

Getting Started with Pygame

Before drawing polygons, you need to set up Pygame. Install it using pip if you haven't already. Then, initialize Pygame and create a display surface.


import pygame
pygame.init()

# Set up the display
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame Draw Polygon Example")
    

Drawing a Polygon

To draw a polygon, use the pygame.draw.polygon function. This function takes the surface, color, and a list of points as arguments. The points define the vertices of the polygon.


# Define the points for the polygon
points = [(100, 100), (200, 50), (300, 100), (250, 200), (150, 200)]

# Draw the polygon
pygame.draw.polygon(screen, (255, 0, 0), points)

# Update the display
pygame.display.flip()
    

In this example, a red polygon is drawn on the screen. The polygon has five vertices defined by the points list.

Understanding the Parameters

The pygame.draw.polygon function has three main parameters:

  • Surface: The surface to draw on.
  • Color: The color of the polygon.
  • Points: A list of tuples representing the vertices.

You can also specify an optional width parameter to draw the outline of the polygon.

Example: Drawing a Hexagon

Let's draw a hexagon. A hexagon has six sides, so we need six points. The points are calculated using basic trigonometry.


import math

# Center and radius of the hexagon
center = (400, 300)
radius = 100

# Calculate the points
points = []
for i in range(6):
    angle = 2 * math.pi / 6 * i
    x = center[0] + radius * math.cos(angle)
    y = center[1] + radius * math.sin(angle)
    points.append((x, y))

# Draw the hexagon
pygame.draw.polygon(screen, (0, 255, 0), points)

# Update the display
pygame.display.flip()
    

This code draws a green hexagon centered at (400, 300) with a radius of 100 pixels.

Handling Events

To keep the window open, you need to handle events. Use the pygame.event.get function to process events. Check out our Python Pygame Event Get Guide for more details.


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

Conclusion

Drawing polygons in Pygame is straightforward with the pygame.draw.polygon function. You can create complex shapes by defining the vertices. Experiment with different shapes and colors to enhance your game graphics.

For more drawing techniques, check out our Python Pygame Draw Line Guide and Python Pygame Draw Circle Guide.