Pygame is a set of Python modules for developing video games. It provides tools for creating surfaces, handling events, playing sounds, and more. The presentation introduces essential Pygame elements like surfaces, rectangles, display, and events. Surfaces represent images and allow manipulating pixels. Rectangles are used for collision detection. The display module handles the screen. Events represent user inputs. Pygame provides a simple and portable way to create games in Python.
3. What is Pygame?
Set of Python modules for writing games
Compatible with SDL
Free
4. Best Features
No need to OpenGL setup
Multi-core
C/Assembler Core Functions
Portable
Easy to use
Comunity
5. Problems
Python is slow
But you can do something to turn this out.
You write too much
Isn’t a Game Engine, it’s a library.
6. Essential Elements
Surface
What is surface?
2D and 3D planes.
Eg:
tamanho = ( 640 , 480 )
cor = ( 255 , 0 , 0 ) # v e rme lho
superficie = Surface ( tamanho )
superficie . set_at ( ( 10 , 20 ) , cor )
superficie . fill ( cor , ( 11 , 21 , 50 , 50 ) )
tela = pygame . display . set_mode ( tamanho )
7. Surface – Important Functions
fill( color, area)
Fill a area with the selected color
get_at( position )
Returns the color that was in the setted
position
set_at( position, color)
Set the color on that position
8. Surface – Color Depth
256 Colors
24 bpp RGB
3 Bytes per color
9. Surface - Transparency
ColorKey
One color is chosen to be transparent
Image Alpha
Can have his Translucency between 0 and
255
Can be used together with ColorKey
Per-Pixel Alpha
Has a associated Alpha component.
Cannot be used with another transparency
10. What is Blit ?
Convert the current Surface Color Depth
to the next Surface Color Depth
It is a very costly function
What is the alternative?
11. Rect
What is it?
Eg:
r = Rect ( ( 10 , 10 , 50 , 100 ) )
print r.top , r. bottom # 1 0 1 1 0
print r.left , r. right # 1 0 6 0
print r.midtop , r. midleft # ( 3 5 , 1 0 ) ( 1 0 , 6 0 )
print r. center # ( 3 5 , 6 0 )
c1 = r. collidepoint ( 30 , 40 )
c2 = r. colliderect ( ( 0 , 0 , 100 , 200 ) )
r2 = r. inflate ( 10 , 10 )
r2. move_ip ( 5 , 5 )
12. Rect – Important Functions
clamp (area)
Returns a new rectangle inside this area
clip(area)
Returns a new rectangle with his area cliped to
fill the new area
collidepoint(x, y)
Check if the point is in the rectangle.
colliderect(area)
Check if the area toch the current rectangle
13. Rect – Important Functions
contains(area)
Check if the area is in the rectangle.
inflate(x,y)
Returns a new rectangle with the dimensions of
the current rectangle increased according to
the passed value
move(x,y)
Returns a new rectangle with the positions of
current rectangle driven by passed values
14. Display
What is it?
Eg:
modos = pygame . display . list_modes ()
tela = pygame . display . set_mode ( modos [ 0 ] )
rect = pygame . Rect ( 0 , 0 , 10 , 10 )
pygame . display . set_caption ( " Teste do PyGame " )
while tela . get_rect (). contains ( rect ):
tela . fill ( ( 0 , 0 , 0 ) )
tela . fill ( ( 255 , 0 , 0 ) , rect )
rect . move_ip ( 10 , 10 )
pygame . display . flip ()
15. Display – Essential Functions
flip()
Update all the screen content
get_surface()
Return the current surface
list_modes()
List possible resolutions/dimensions
set_caption(title)
Change screen title
16. Display – Essential Functions
set_mode(size)
Setup the screen with the passed size
toggle_fullscreen()
Toggle to fullscreen
update(rect_list)
Update certain areas of the screen
depending of the passed list of rectangles
17. Event
What is it?
Thereare two methods to handle the line
of events
Event Queued
Direct consultation of devices
18. Event Queued
from pygame . locals import *
for event in pygame . event .get ():
if event . type == QUIT :
sys . exit ()
elif event . type == KEYDOWN :
print event .key
19. Direct Consultation of Devices
from pygame . locals import *
while not ( pygame . mouse . get_pressed ()[ 0 ]
or pygame .key . get_pressed ()[ K_SPACE ] ):
pygame . event . pump ()
20. References
Python: http://www.python.org/
PyGame: http://www.pygame.org/
A Newbie Guide to pygame:
http://www.pygame.org/docs/tut/newbieguide.html
Introdução ao Pygame:
http://www.pygame.org/docs/tut/intro/intro.html
Dicas de Performance para Python: http:
//www.python.org/moin/PythonSpeed/Performance
Tips
Introdução ao Módulo Sprite do PyGame:
http://www.pygame.org/docs/tut/SpriteIntro.html