KEMBAR78
XIX PUG-PE - Pygame game development | PPTX
Pygame - Game
Development
Matheus Melo
XIX PUG-PE
Schedule
 Introduction


 What   is PyGame?

 PyGame   essential Elements

 Developing     games with Pygame
What is Pygame?
 Set   of Python modules for writing games

 Compatible    with SDL

 Free   
Best Features
 No need to OpenGL setup
 Multi-core
 C/Assembler Core Functions
 Portable
 Easy to use
 Comunity
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.
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 )
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
Surface – Color Depth
 256   Colors



 24   bpp RGB
    3 Bytes per color
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
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?
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 )
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
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 ​
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 ()
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
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
Event
 What    is it?

 Thereare two methods to handle the line
 of events
    Event Queued
    Direct consultation of devices
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
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 ()
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
Contact Details
 Matheus Melo
 Work mail:
     matheus.melo@idealizza.com.br
 Personal   mail:
     matheuscmpm@gmail.com
 Twitter:
     @matheuscmpm
 Facebook
     /matheuscmpm

XIX PUG-PE - Pygame game development

  • 1.
  • 2.
    Schedule  Introduction  What is PyGame?  PyGame essential Elements  Developing games with Pygame
  • 3.
    What is Pygame? Set of Python modules for writing games  Compatible with SDL  Free 
  • 4.
    Best Features  Noneed 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 – ImportantFunctions  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 – ColorDepth  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 – ImportantFunctions  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 – ImportantFunctions  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 – EssentialFunctions  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 – EssentialFunctions  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 ofDevices  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
  • 21.
    Contact Details  MatheusMelo  Work mail:  matheus.melo@idealizza.com.br  Personal mail:  matheuscmpm@gmail.com  Twitter:  @matheuscmpm  Facebook  /matheuscmpm