Pygame Drawing and Animation

Pygame Drawing and Animation Overview

Animation and user interactivity are the two requirements for Game apps. Pygame enables the programmer to develop 2-D games with animation. Interactivity enables the player to interact with the game via Keypress, mouse clicks, or a joystick. The player interacts with the game using Events. See the Pygame Events chapter.

Animation is used in programs for game development, website development, and many more apps. On this page, we will do simple animation using a soccer ball.

Because games app and animation is visual, graphics programming is an excellent way for kids to learn coding.  After kids learn simple graphics programming, they can go on to other languages that are more suitable for fast animation.

Before we start on pygame animation, let us introduce a Pygame module called pygame.draw.

Pygame Draw Module

We learned from the previous chapter that the statement below imports all pygame modules including functions related to graphics, sound, drawing, etc.

>>>import pygame

There is a module inside pygame named pygame.draw module. When you import pygame, you automatically import pygame.draw module. The Pygame.draw module provides several functions for drawing different shapes onto a Surface object.

More information on Pygame.draw module is at the link below:

a href=”https://pygame.org/docs/ref/draw.html“>

Pygame Draw Module Primitives

The pygame.draw module can draw various shapes on the Surface object. Pygame.draw module shapes are called drawing primitives. These primitives are:

  • Lines
  • Rectangles
  • Circles
  • ellipses
  • individual pixels are often called drawing primitives.

Drawing Shape Functions Arguments

The function that draws any of the above shapes requires the arguments from the list below.

  • First argument: Which Surface object to draw the shape on
  • Second argument: Color of the shape
  • Third argument: Where on the Surface to draw the shape, size, point list, center, radius, etc.)
  • Fourth argument: Width of the shape’s outline lines

If the fourth argument (width) is not specified, the shape is filled with the color. 

If, however, the width argument is specified, the shape is not filled with color.  The outline of the shape has the color specified in the second argument. 

We will learn how the pygame.draw module is used to draw shapes, fill the shapes with colors, and set the width and color of the shape’s outline.

Let us now see what functions are included in the pygame.draw module.

Pygame.draw Module Functions

We will use the following Pygame.draw module functions in the programs on this page.

Draw a Line:

Pygame.draw.line(surface, color, (coordinates of one end, coordinates of the other end), width)

Draw a Circle:

Pygame.draw.circle(surface, color, (center coordinates, radius), width)

Draw a Polygon:

Pygame.draw.polygon(surface, color, point list, width)

Draw a Rectangle:

Pygame.draw.rect(surface, color, (left-top corner coordinates, right-bottom corner coordinates), width)

Draw an Ellipse:

Pygame.draw.ellipse(surface, color, (left-top corner coordinates), (right-bottom corner coordinates), width)

For the rectangle shape, the left-top and bottom-right corners are a tuple of four integers (x1, y1, x2, y2), where x1,y1 are the coordinates of the top-left corner and x2,y2 are the coordinates of the bottom-right corner of the rectangle.

For the ellipse shape, the left-top and bottom-right corners are a tuple of four integers (x1, y1, x2, y2), where x1,y1 are the coordinates of the top-left corner and x2,y2 are the coordinates of the bottom-right corner of the rectangle that encloses the ellipse.

PROGRAM EXAMPLE: DRAWING SHAPES IN PYGAME

Program name: pygameShapesDraw.py

import pygame
pygame.init()
from pygame.locals import *
screen = pygame.display.set_mode((400, 320))
# Define colors
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
GRAY = (127, 127, 127)
WHITE = (255, 255, 255)
background = WHITE
clock = pygame.time.Clock()
screen.fill(background) # Fill background WHITE
while True:
    pygame.draw.rect(screen, BLUE, (10, 60, 70, 100))
    # Rectangle left-top corner (120, 60); 
# right-bottom corner ((70,100); color fill = BLUE
    pygame.draw.circle(screen, GREEN, (100, 60), 50)
    # Circle center (100,60); radius 50; Fill GREEN
    pygame.draw.circle(screen, MAGENTA, (100, 220), 50, 4)
    # Circle center (100, 220), radius = 50, outline magenta;
# outline width = 4
    pygame.draw.ellipse(screen, YELLOW, (180, 20, 200, 100))
    # Ellipse bounded by rectangle left-top corner (180,20)
    # right-bottom corner (200, 100); fill = YELLOW
    pygame.draw.line(screen, RED, (200, 120), (300, 200), 10)
    # Line end 1 = (200,120); end 2 = (300,200); width = 4;
# color = RED
    pygame.display.update()
    clock.tick(60)


pygame.quit()

The following is program output.

Pygame.draw module to draw a line, a rectangle, a circle with color fill, a circle with no color fill, and an ellipse.
DRAW VARIOUS SHAPES USING Pyhame.Draw Module
Verified by MonsterInsights