pygame is a game framework for python. It can be used for creating many 2D games.
Contents
basic-window.py
- Opening up a basic windowrectangle.py
- Drawing a rectangle on the windowfollow.py
- Making a circle follow your cursor.
import pygame | |
pygame.init() | |
# You must call init() at the start of every pygame | |
# project that you make. | |
WIDTH = 500 | |
HEIGHT = 500 | |
# You can change these variables to what you like, | |
# they represent the height and width of your window. | |
display = pygame.display.set_mode((WIDTH, HEIGHT)) | |
print("Created the window.") | |
pygame.display.set_caption("basic-window.py") | |
# This sets the window title, you can change this as well. | |
clock = pygame.time.Clock() | |
stopped = False | |
while not stopped: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
# This event fires if the user clicks the | |
# red X or closes the window some other way | |
# e.g. pressing alt+f4 | |
print("Destroying the window") | |
stopped = True | |
# Set stopped to True to break out of | |
# our main loop. | |
clock.tick(60) | |
# This means this we will loop through the events | |
# 60 times every second. | |
pygame.quit() | |
# Quit our program at the end |
import pygame | |
pygame.init() | |
WIDTH = 500 | |
HEIGHT = 500 | |
display = pygame.display.set_mode((WIDTH, HEIGHT)) | |
print("Created the window.") | |
pygame.display.set_caption("rectangle.py") | |
clock = pygame.time.Clock() | |
# To draw a rectangle we need to create a pygame.Rect object. | |
# pygame.Rect can take in two tuples: | |
# 1. The x and y position (top left is (0, 0)) | |
# 2. The width and height of the rectangle | |
rectangle = pygame.Rect((100, 100), (200, 100)) | |
# To draw the rectangle onto the window we need to use another | |
# function: pygame.draw.rect. This takes in the following parameters: | |
# surface: in our case this is the "display" variable we created earlier | |
# colour: a tuple of the r, g and b values e.g. (255, 0, 0) for red | |
# rect: a pygame.Rect object, in our case this is the "rectangle variable" | |
pygame.draw.rect(display, (255, 0, 0), rectangle) | |
# After doing this we must update the display | |
pygame.display.update() | |
# It is not just rectangles that you can draw but many other shapes | |
# and they are listed below in "shapes.md" | |
stopped = False | |
while not stopped: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
print("Destroying the window") | |
stopped = True | |
clock.tick(60) | |
pygame.quit() | |
A list of shapes that you can draw with pygame.draw
Function: pygame.draw.rect
Parameters:
surface
- the surface to draw on (the variable we called display in the above files)color
- a tuple of the color's rgb values e.g. (255, 0, 0) for redrect
- a pygame.Rect
objectFunction: pygame.draw.circle
Parameters:
surface
- the surface to draw on (the variable we called display in the above files)color
- a tuple of the color's rgb values e.g. (255, 0, 0) for redcenter
- a tuple denoting the x, y coordinates of the centre of the circleradius
- the radius of the circleFunction: pygame.draw.ellipse
Parameters: The same as pygame.draw.rect
but it draws an ellipse instead of a rectangle
Function: pygame.draw.polygon
Parameters:
surface
- the surface to draw on (the variable we called display in the above files)color
- a tuple of the color's rgb values e.g. (255, 0, 0) for redpoints
- a list of tuples denoting the vertices of the polygonFunction: pygame.draw.line
Parameters:
surface
- the surface to draw on (the variable we called display in the above files)color
- a tuple of the color's rgb values e.g. (255, 0, 0) for redstart_pos
- a tuple denoting the x, y coordinates of the starting position of the lineend_pos
- a tuple denoting the x, y coordinates of the ending position of the linewidth
- the width of the lineimport pygame | |
pygame.init() | |
WIDTH = 500 | |
HEIGHT = 500 | |
display = pygame.display.set_mode((WIDTH, HEIGHT)) | |
print("Created the window.") | |
pygame.display.set_caption("follow.py") | |
clock = pygame.time.Clock() | |
def circle(display, position): | |
# This fills the screen with one colour. If we don't do this | |
# then all the circles that were drawn last time will still be there. | |
display.fill((0, 0, 0)) | |
# Draw a circle onto the display. | |
# pygame.draw.circle takes in the display, colour, | |
# position and the radius | |
pygame.draw.circle(display, (255, 0, 0), position, 25) | |
pygame.display.update() | |
stopped = False | |
while not stopped: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
print("Destroying the window") | |
stopped = True | |
if event.type == pygame.MOUSEMOTION: | |
# This event is fired when we move our mouse over the screen | |
# it has a "pos" attribute which denotes where the mouse moved to | |
# we can use this to pass to the function that was created earlier | |
circle(display, event.pos) | |
clock.tick(60) | |
pygame.quit() | |