Drawing shapes

From TRCCompSci - AQA Computer Science
Revision as of 21:16, 21 February 2018 by Admin (talk | contribs)
Jump to: navigation, search

Make sure you are starting with a working pygame project, this will require you to install pygame and copy the code from the page below to make a start:

Basic pygame template

Rectangle

You can declare some variables to use for your rectangle, they require the x coordinate, the y coordinate, the width and the height:

LEFT = 100
TOP = 100
LENGTH = 20
WIDTH = 20
RECTCOORD = [LEFT, TOP, LENGTH, WIDTH]
rect1 = pygame.Rect(RECTCOORD)

This is the same as writing:

rect1 = pygame.Rect([100,100,20,20])

Colour

Before we can draw your rectangle to the screen we will need to define a colour to use:

RED = 255
YELLOW = 230
BLUE = 200
COLOR = (RED, YELLOW, BLUE)

Again this is the same as writing just:

COLOR = (255, 230, 200)

Draw the Rectangle

We can now draw the rectangle to the screen:

pygame.draw.rect(SCREEN, COLOR, rect1, 0)

The 0 will cause the shape to be filled with colour, a value above 0 will draw the outline only and use the number for the line thickness. For example:

pygame.draw.rect(SCREEN, COLOR, rect1, 1)

Update the screen

Now you have drawn something new to the screen we need to update it. We can just update a single object:

pygame.display.update(rect1)

or we can update everything:

pygame.display.update()

Full Example

#Import statements are to enable the code to use the functions from the library
import pygame
import sys
import os

#initialize pygame & window
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

#caption for the game
pygame.display.set_caption("My first game in pygame")

rect1 = pygame.Rect([100,100,20,20])

COLOR = (255, 0, 200)

pygame.draw.rect(SCREEN, COLOR, rect1, 1)

#game loop
while True:
    pygame.display.update()
    for events in pygame.event.get(): #get all pygame events
        if events.type == pygame.QUIT: #if event is quit then shutdown window and program
            pygame.quit()
            sys.exit()

Other Shapes

Circle

To draw a circle you need to specify the X coordinate, the Y coordinate, and the radius:

LEFT = 200
TOP = 200
RADIUS = 20
pygame.draw.circle(SCREEN, COLOR, (LEFT,TOP), RADIUS,0)

This is just the same as writing:

pygame.draw.circle(SCREEN, COLOR, (200,200), 20, 0)

The zero will produce a filled circle, any value above 0 will be used for the thickness of the outline.

Line

To draw a line you need to specify the coordinate for the starting point and the coordinate for the ending point. The thickness is the same as the other shapes drawn above:

START = [100,100]
END = [200,200]
THICK = 10;

pygame.draw.line(SCREEN, COLOR, START, END, 10)

This is the same as:

pygame.draw.line(SCREEN, COLOR, [100, 100], [200, 200], 10)

Ellipse

X = 100
Y = 100
WIDTH = 200
HEIGHT = 50

pygame.draw.ellipse(SCREEN, COLOR, [X, Y, WIDTH, HEIGHT])

This is the same as:

pygame.draw.ellipse(SCREEN, COLOR, [100, 100, 200, 50])

Polygon

P1 = [100, 100]
P2 = [100, 400]
P3 = [400, 300]
POINTS = [P1, P2, P3]

pygame.draw.polygon(SCREEN, COLOR, POINTS, 2)

this is the same as:

pygame.draw.polygon(SCREEN, COLOR, [[100, 100], [100, 400],[400, 300]], 2)