Difference between revisions of "Drawing shapes"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==Rectangle== You can declare some variables to use for your rectangle, they require the x coordinate, the y coordinate, the width and the height: <syntaxhighlight lang=pytho...")
 
Line 1: Line 1:
 +
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==
 
==Rectangle==
 
You can declare some variables to use for your rectangle, they require the x coordinate, the y coordinate, the width and the height:
 
You can declare some variables to use for your rectangle, they require the x coordinate, the y coordinate, the width and the height:
Line 29: Line 33:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
COLOR = (255, 230, 200)
 
COLOR = (255, 230, 200)
 +
</syntaxhighlight>
 +
 +
We can now draw the rectangle to the screen:
 +
 +
<syntaxhighlight lang=python>
 +
pygame.draw.rect(SCREEN, COLOR, rect1, 0)
 +
</syntaxhighlight>
 +
 +
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:
 +
 +
<syntaxhighlight lang=python>
 +
pygame.draw.rect(SCREEN, COLOR, rect1, 1)
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 21:35, 21 February 2018

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])

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)

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)