0% found this document useful (0 votes)
20 views5 pages

Pygame Manual v0 2

This document presents a tutorial for learning PyGame in practice. It introduces basic concepts of game programming using PyGame in Python and teaches how to create a simple game called "Robot Attack" as a practical example. The tutorial explains basic code structures to organize projects in PyGame and important concepts such as events and basic functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Pygame Manual v0 2

This document presents a tutorial for learning PyGame in practice. It introduces basic concepts of game programming using PyGame in Python and teaches how to create a simple game called "Robot Attack" as a practical example. The tutorial explains basic code structures to organize projects in PyGame and important concepts such as events and basic functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Learning PyGame in practice

by Pedro Vítor Marques Nascimento

IntroductionA little about PyGame}


WelcomeWho always comes here?}

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 Python? Of course. PyGame, in layman's terms, is simply a


complement to Python and only allows you to use some new visual features. Aside from
some specific functions, the rest is all Python and programming logic. That's why I stated that who
if you don't like programming, you will hardly enjoy developing games.
This tutorial will not teach Python, as there are many tutorials available on the internet about it.
language. All my tips will be at the end of the booklet and this includes books, websites, blogs, booklets and
Online Judges.

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.

PyGame StructureBasic organization}


Every game in PyGame will have a basic structure whose purpose is to organize the work environment.
code. It will become cleaner, things will be easier to visualize, and best of all, the risk of getting an error
is smaller. In fact, this is a good tip: the more organized, the fewer errors a code will have. It should have a
physical formula proving this.
Now speaking in terms of PyGame, we can establish an order where in each 'topic'
we will have part of the code organized. If you are using more than one file to execute, you may have the
removal of some "species" such as functions and classes. The default order is this:

#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:

for event in pygame.event.get():


if event.type == QUIT:
pygame.quit()
exit()

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.

The essence of game programmingWhat are events?}


When programming games, you will notice that some things are quite different from programming on the console.
for example. When we work with games, we use events to define what will happen in
fabric.
A clear example of this: The game stays on a black screen until you press the 'space' key. Receive
the information that the key was pressed is an event for the game and in this case, it responds to that.
In a quick algorithm:

screen color = black


while the player does not exit: //this is the main loop
receive events: //receiving events
If the player exits: //event1: the player presses the exit button in the window
close //action if the event happens
The SPACE key was pressed://event2: the player presses SPACE
screen color = white //changes the screen color

Just out of curiosity, in PyGame it would look like this:

(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 //posição inicial do personagem


While the player does not leave:
receive events:
The UP key was pressed:
posY -= 1 //updates the position
The DOWN key was pressed:
posY += 1 //updates the position
The LEFT key was pressed:
posX -= 1 //updates the position
The RIGHT key was pressed:
posX += 1 //updates the position
Draw the character at (posX,posY)

What would it be in PyGame:

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?

Time to say hello!Making YOUR visual program!}


In this session, we will use the simple logic above to say our 'Hello' to the computer. Shall we go?
I will present the idea and we will try to execute it together.

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:

Basic functionsBasic definitions}


screen.blit()
Description: Prints a specific image on the display at a specific position;
Entry: the image and the coordinate;
Output: no output, just displays the image on the screen;
Example: screen.blit(image,(0,0))
pygame.image.get_width()
pygame.image.get_height()
pygame.display.set_mode()
pygame.mixer.Sound()
pygame.Sound.play()
pygame.mixer.set_num_channels()
pygame.image.load().convert()
pygame.image.load().convert_alpha()
pygame.time.Clock()
pygame.mouse.set_visible()
pygame.display.set_caption()
pygame.font.SysFont()
pygame.mouse.get_pressed()
screen.fill((0,0,0))
pygame.display.update()
clock.tick(60)

We make a gameLet's create our game!}

You might also like