Skip to content

Instantly share code, notes, and snippets.

@1oonie
Last active June 28, 2021 14:05
Show Gist options
  • Save 1oonie/fbde312cd68670f4dc993644011fd63c to your computer and use it in GitHub Desktop.
Save 1oonie/fbde312cd68670f4dc993644011fd63c to your computer and use it in GitHub Desktop.
A little explanation of how to use the basics of pygame

pygame

pygame is a game framework for python. It can be used for creating many 2D games.

Contents

  • basic-window.py - Opening up a basic window
  • rectangle.py - Drawing a rectangle on the window
  • follow.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()

Shapes

A list of shapes that you can draw with pygame.draw

Rectangle

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 red
  • rect - a pygame.Rect object

Circle

Function: 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 red
  • center - a tuple denoting the x, y coordinates of the centre of the circle
  • radius - the radius of the circle

Ellipse

Function: pygame.draw.ellipse

Parameters: The same as pygame.draw.rect but it draws an ellipse instead of a rectangle

Polygon

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 red
  • points - a list of tuples denoting the vertices of the polygon

Line

Function: 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 red
  • start_pos - a tuple denoting the x, y coordinates of the starting position of the line
  • end_pos - a tuple denoting the x, y coordinates of the ending position of the line
  • width - the width of the line
import 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment