Professional Documents
Culture Documents
Colour
Colours in pyGame are represented by tuples that state the amount of Red, Green, and Blue are in them.
Each value is an integer from 0 to 255 indicating how much of each colour goes into the mix.
e.g.
(0, 0, 255)
- This means 0% Red 0% Green and 100% Blue. The result is, of course, basic blue.
0, 0, 255
- This is another way of defining a tuple
If you can't mix colours in your head, either
A) Use the pallet in paint to define custom colours
B) look at the file "RGB Color Codes.htm" to find a colour you like
# pygame1.py
# the basic template to draw a picture that is not moving
import pygame
pygame.init()
pygame.display.flip()
#
#
#
#
#
pygame.time.wait(3000)
pygame.quit()
# pygame2a.py
# draw a face using a few of the pygame draw methods
import pygame
pygame.init()
RED = (255, 0, 0)
BLACK = (0,0,0)
SIZE = (800, 600)
screen = pygame.display.set_mode(SIZE)
# Draws a red face
pygame.draw.circle(screen, RED, (200,100), 50)
pygame.draw.circle(screen, BLACK, (185,70), 5)
pygame.draw.circle(screen, BLACK, (215,70), 5)
pygame.draw.line(screen, BLACK, (185,85), (215, 85))
pygame.display.flip()
pygame.time.wait(3000)
pygame.quit()
# Left eye
# Right eye
# Mouth
Once you start coming up with your own creative art, its handy to use functions to define the code for
each of your pictures (this will also come into handy when we start to animate our work). Defining a
function is a way of creating our own methods (such as len, range, etc) in Python. Once these functions
are created we can call them whenever we want.
def drawface():
pygame.draw.circle(screen, RED, (200,100), 50)
pygame.draw.circle(screen, BLACK, (185,70), 5)
pygame.draw.circle(screen, BLACK, (215,70), 5)
pygame.draw.line(screen, BLACK, (185,85), (215, 85))
# Left eye
# Right eye
# Mouth
drawface()
To get us one step closer to animating, we can add parameters to allow you to draw the face wherever you
want on the screen. Notice that another benefit of defining our own function is that we can call it multiple
times without having to copy and paste code.
def drawface(screen,x,y):
pygame.draw.circle(screen, RED, (50+x,50+y), 50)
pygame.draw.circle(screen, BLACK, (35+x,20+y), 5)
# Left eye
pygame.draw.circle(screen, BLACK, (65+x,20+y), 5)
# Right eye
pygame.draw.line(screen, BLACK, (35+x,35+y), (65+x, 35+y)) # Mouth
drawface(screen,300,100)
drawface(screen,0,0)
drawface(screen,70,300)
Notice that Ive edited some of my coordinates so that if I type 0,0 as my parameters, it will allow the face
to be drawn in the top left hand corner. Ive also included the screen as one of the parameters. Although
this is not needed, it will allow me expand my work to have multiple windows (say for
future programs).
Graphics Exercises
1. Write a program that simulates a game of tic-tac-toe that the user can watch the
computer play.
2. Write a program that defines a function for drawing a 3D box with
parameters that allows the box to be placed anywhere on the screen
(similar to the face).
3. Draw a character (a person, a superhero, a car) on the screen and be
colourful and creative with your drawing. You can make use of
multiple functions to draw the single character so that you can allow for
multiple poses, etc. For example, to draw your person, you might want
to have a function to draw the body and multiple functions for the face to allow for different facial
expressions). Feel free to experiment and to have fun with this!