Pygame Manual v0 2
Pygame Manual v0 2
This tutorial focuses on introducing some functions of PyGame for beginners. If you have already had
contact with some form of game programming you will realize that using Python makes everything easier and
fast.
The first game we will create is what I call 'Robot Attack', which is a great example of
how to create a 2D game without going too deep into the theory of a language. We will use functions,
classes, music, animations and, of course, events. It will be a great opportunity to learn or remember
old knowledge already shelved.
One thing you will easily notice is that to program games you must above all
to enjoy programming. Yes, it seems obvious, but sometimes it is not clear to everyone that developing games is
very similar to creating a program. If you don't enjoy creating programs, you will hardly get excited about it.
game creation.
Anyway, gather your breath, focus your enthusiasm, and prepare a cup of coffee, because it's
When it's time to start getting your hands dirty... No! First, let's clear up some doubts...
Do I need to know Physics? Yes, but it doesn't have to be very advanced knowledge for the most part.
of projects. How to program a ball bouncing on a ramp if you don't know the law of reflection? The good
The news is that we will always have good old Google to help us with specific doubts. Please, do not
despair.
#LIBRARY IMPORTS
DEFINITIONS OF IMAGES, AUDIO, ETC
INITIALIZATIONS
#CONVERSIONS
#CLASSES
#FUNCTIONS
#MAIN LOOP
#OTHER LOOPS
An example of code following such a structure is below. I will not explain it now, especially since this does not
This is the purpose of the chapter. I just want to show a good way to organize the code.
#IMPORTS
import pygame
from pygame.locals import *
from sys import exit
#DEFINITIONS
sushiplate.jpg
fugu.png
INITIALIZATIONS
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
#MAIN LOOP
while True:
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x -= mouse_cursor.get_width() / 2
y -= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
Can you tell the truth, did you get scared by the code? At first, it may seem strange and
intimidator, but calm down, this is just your beginning, with time you will get the hang of it and will already know every line
from the head.
(0,0,0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == K_SPACE:
(255, 255, 255)
screen.fill(color of the screen)
Let's go! It's not that complicated, is it? The game stays in the 'while True' loop until it receives orders from
leave. While inside, every time it goes through that 'for event in pygame.event.get()' it will collect
the changes and will try to detect events from that.
This logic applies to EVERYTHING in game programming. Another example, this one cooler:
posX,posY = 0,0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == K_UP:
posY -= 1
if event.type == K_DOWN:
posY += 1
if event.type == K_LEFT:
posX -= 1
if event.type == K_RIGHT:
posX += 1
screen.fill(0,0,0)
screen.blit(character, (posX, posY))
Don't force yourself to understand everything about PyGame, I just put this here to start introducing some.
definitions and questions in your mind. Why is there a 'K_' in front of the name of the keys? What is it
'screen.blit'? Why did you use screen.fill? CALM DOWN! Everything will be answered soon.
Now that you have understood part of the essence of the events, how about we send a 'Hello' to the
computer?
Idea: A 'game' where when you press space a screen appears saying 'Hello, World!'.
Then, when you press the directional buttons, this same screen subtly shifts in the chosen direction.
mushrooms.
Written logic: While the game is open, it will wait for events of the type 'key press'. If
for 'space', it shows a blank screen with the words 'Hello World'; if it's 'up', 'left', 'right' or
"down" the program moves the surface in the chosen direction. When the directional button is released, the
image returns to the center.
Portuguese
Screen = 640px by 480px, images in 32 bits, fixed size screen
background1.png
aloMundo.png
x,y = 0,0 // initial value
While the game is open:
Receive events:
If the event is 'EXIT':
If it is of the type 'KEY DOWN': // button pressed
If the button is 'SPACE':
If the button is "UP":
y=y-5
If the button is "DOWN":"
y=y+5
If the button is 'LEFT':
x=x-5
If the button is "RIGHT":
x=x+5
If it's of the type 'KEY UP': //released the button
If the button is 'UP':
y=0
If the button is "LOW":
y=0
If the button is 'LEFT':
x=0
If the button is 'RIGHT':
x=0
Print the image n on the screen from the coordinate x,y
Update the screen;
Written in PyGame: