Beginners Python Cheat Sheet PCC Pygame PDF
Beginners Python Cheat Sheet PCC Pygame PDF
starts an event loop and a loop that continually refreshes Useful rect attributes
Once you have a rect object, there are a number of attributes that
the screen.
are useful when positioning objects and detecting relative positions
An empty game window of objects. (You can find more attributes in the Pygame
documentation.)
import sys
# Individual x and y values:
import pygame as pg
screen_rect.left, screen_rect.right
screen_rect.top, screen_rect.bottom
Pygame is a framework for making games using def run_game():
screen_rect.centerx, screen_rect.centery
Python. Making games is fun, and its a great way to # Initialize and set up screen.
screen_rect.width, screen_rect.height
expand your programming skills and knowledge. pg.init()
Pygame takes care of many of the lower-level tasks screen = pg.display.set_mode((1200, 800))
# Tuples
in building games, which lets you focus on the pg.display.set_caption("Alien Invasion")
screen_rect.center
aspects of your game that make it interesting. screen_rect.size
# Start main loop.
while True: Creating a rect object
# Start event loop. You can create a rect object from scratch. For example a small rect
Pygame runs on all systems, but setup is slightly different for event in pg.event.get(): object thats filled in can represent a bullet in a game. The Rect()
if event.type == pg.QUIT: class takes the coordinates of the upper left corner, and the width
on each OS. The instructions here assume youre using and height of the rect. The draw.rect() function takes a screen
Python 3, and provide a minimal installation of Pygame. If sys.exit()
object, a color, and a rect. This function fills the given rect with the
these instructions dont work for your system, see the more given color.
detailed notes at http://ehmatthes.github.io/pcc/. # Refresh screen.
pg.display.flip() bullet_rect = pg.Rect(100, 100, 3, 15)
Pygame on Linux color = (100, 100, 100)
$ sudo apt-get install python3-dev mercurial run_game() pg.draw.rect(screen, color, bullet_rect)
libsdl-image1.2-dev libsdl2-dev Setting a custom window size
libsdl-ttf2.0-dev The display.set_mode() function accepts a tuple that defines the
$ pip install --user screen size. Many objects in a game are images that are moved around
hg+http://bitbucket.org/pygame/pygame the screen. Its easiest to use bitmap (.bmp) image files, but
screen_dim = (1200, 800)
screen = pg.display.set_mode(screen_dim) you can also configure your system to work with jpg, png,
Pygame on OS X and gif files as well.
This assumes youve used Homebrew to install Python 3.
Setting a custom background color
$ brew install hg sdl sdl_image sdl_ttf Colors are defined as a tuple of red, green, and blue values. Each Loading an image
$ pip install --user value ranges from 0-255. ship = pg.image.load('images/ship.bmp')
hg+http://bitbucket.org/pygame/pygame bg_color = (230, 230, 230)
Getting the rect object from an image
Pygame on Windows screen.fill(bg_color)
Find an installer at ship_rect = ship.get_rect()
https://bitbucket.org/pygame/pygame/downloads/ or
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame that matches Positioning an image
your version of Python. Run the installer file if its a .exe or .msi file. Many objects in a game can be treated as simple With rects, its easy to position an image wherever you want on the
If its a .whl file, use pip to install Pygame: rectangles, rather than their actual shape. This simplifies screen, or in relation to another object. The following code
positions a ship object at the bottom center of the screen.
> python m pip install --user code without noticeably affecting game play. Pygame has a
pygame-1.9.2a0-cp35-none-win32.whl rect object that makes it easy to work with game objects. ship_rect.midbottom = screen_rect.midbottom