10. 03.

10 Things a Beginner Should Know About Pygame (english version)

  1. Before you start, learn Python. For those who already code in another language, I give you my word: It’s a freaking easy task. Satisfaction guaranteed or your money back.
  2. Display is where action happens. The pygame.display module controls the game window. set_mode() defines window size and the get_surface() returns a reference to the Surface (see next topic) that represents the game window. A very important concept to keep in mind when using Pygame is that changes made to display aren’t immediately seen on screen. To do that, you need to update screen by calling flip() or update(). The difference between these two methods is that the former updates the whole screen and the latter updates only the screen areas passed as parameters.
  3. Surface vs. Rect.Objects from the Surface class represent fixed size images (width x height). blit() draws a Surface (image) inside another and is the main method of the Surface class. Rect class defines objects that represent and handle rectangular coordinates on the screen. If you need to set a relationship between these two classes, remember the Surface.get_rect() method. It returns an object from Rect class representing the coordinates of a given image.
  4. Is Sprite a thing you can drink? The pygame.sprite module has classes and functions that controls game dynamics. Sprite is the base class used to represent visible objects in our games. These objects are grouped in objects from the Group and its descendants (RenderUpdates, OrderedUpdates a GroupSingle). So, before doing geometric tricks when in need to check if an object touches another, don’t forget: the spritecollide() method is your friend.
  5. Understand the event loop. In order to work well, a Pygame application needs to keep itself observing the several events that occur during execution, like pressed keys, mouse clicks and others. Thus, we need to implement and infinite loop, breaking it only when the application is explicitly finished.
  6. Mute cinema? As in any movie, a good game needs to have sounds. In this case, pygame.mixer is the module in charge. The Sound class objects load and play sounds from OGG or WAV (uncompressed) files. Once the object is created, play() methods plays the sound.
  7. And what about text? To show text characters on screen we can count with the help of the pygame.font module. Text is rendered as a new object of the Surface class. Font class loads a TrueType font from a file, while its sister SysFont does it simply passing the name of one of the fonts currently available on OS. Once the object with a loaded font is created, you just need to call render() passing the string containing the text to get a Surface with the rendered text.
  8. Try to write object oriented code. Pygame is easy, but code for any game with a minimum of complexity may easily become large and difficult to mantain. Identify the game elements that have attributes in common and use the concept of inheritance.
  9. Read the Line By Line Chimp tutorial. Before you start, make sure to keep some time to follow the chimp tutorial rigorously. It’s a very short text and shows most basic concepts and just the ones that really matter. In fact, I think I should put this tip on top of the list :)
  10. Study the source code of a complete game. This particular example has an incremental approach, in which you can notice the changes in game according to the need to add new elements to evolve the game.

Leia também


Tags: , , ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Sobre o autor

Rodrigo Amaral Rodrigo Amaral
Desenvolvedor de Software, Brasil
View Rodrigo Amaral's profile on LinkedIn

PyConBrasil 2008

PyConBrasil 2008

Assine os feeds RSS

Categorias

Links

Meta