Introduction to PyGame python programming

I was willing to write a few posts about using pygame programming. So now I’m going to do a series of posts on this topic (I’m still not sure how many I will), so this serves as an introduction and index (i.e. Part 1 of this tutorial).

First, what is pygame?

The first thing that pygame is a multimedia library (which works on the SDL libraries) that allow the creation of video games in two dimensions in a simple way.

SDL (Simple DirectMedia Layer) is a set of libraries that provide basic functions for 2D operations management, sound effects, music, and image management charge (and are important because pygame works on them) drawn. Basically it is similar to OpenGL or DirectX (only 2D part) library, designed to be easier to use than these other (besides OpenGL alone is responsible for the graphics, however SDL includes other things such as sounds, – ))

As I said earlier using pygame we will develop some games. Do not expect a huge games with spectacular 3D graphics and need powerful computers to be used. Rather, what we will get 2D games are similar to those of consoles like supernintendo, notebooks like the GBA, NintendoDS (without touch part), etc. To give you an idea of how games are programmed with pygame, some games like Frets on Fire , SolarWolf or pydance use pygame to develop. You can find more examples in pygame site

pygame programming

Returning to the theme Pygame is responsible for managing the most difficult part in programming a video game, or is responsible for loading and display images (in formats such as PNG, BMP, PCX, TGA, …), sound (Ogg, MP3, MIDI, …), video, game windows and monitors the input devices (such as mouse, keyboard and joystick) in a fairly simple manner. So basically one has to worry about programming the game itself

Prerequisites (Recommended)

  • Having installed python and pygame (obviously). Both are available ( pygame download )  for various systems (such as Windows, GNU / Linux, Mac, etc.) so that the games can be created on any platform.  I have previously written article for installing Python and first Hello world program
  • Basic knowledge of python, at least as defined functions and classes
  • Basic knowledge of mathematics and physics, which is useful when scheduling games in general (with what you learned as a child is more than enough). Basically it is something of classical physics as the concepts of velocity, acceleration, etc. and math concepts as points, coordinates in space, etc.

First step

The first thing you have to do is import the pygame programming module, which is done simply as:

import pygame
from pygame. locals import *

The second line is optional (but highly recommended), and is used to import a number of constants that have pygame (like the keyboard keys)

Now in general our code in pygame have a structure like this:

#! / Usr / bin / env python
# - * - Coding: utf-8 - * -
 
# Import module
import pygame
from pygame. locals import *
# And other modules used
 
# ----------------------------------------------
# Constant as wide and long display, etc.
# ----------------------------------------------
 
# ----------------------------------------------
# Classes and functions used (I'll explain in the next section)
# ----------------------------------------------
 
def main ():
    pygame.init ()
    # The main class or function that creates or runs the game
    # Contains mainly the game loop (the soul of this)
 
if __name__ = = "__main__" :
    main ()

If you look at line 18 makes a pygame.init () , this is to boot pygame and need to run before you start using pygame (why was included at the beginning of the main function of the game). If you do not like include the pygame.init ()function in the game is another good place to include it immediately after the if __name__ == “__main__”: (before calling the function main () )

And now what?

Continue reading the following entries on the subject, which explains how to create windows, characters, insert sounds, etc. Which they are:

Part 2: Creating a window, loading images and moving into the window : In this second part we will see:

  • How to create a window
  • the main loop of a video game (or knowing when to end the game)
  • Use a picture as background
  • display an image on the screen and move it

Part 3: Creating a video game (the classic pong) : In this part we will:

  • Create a function to load images
  • How to create sprites (sprites are characters, items, etc. in the game)
  • synchronization in video games (that are frames per second?)
  • control a sprite with the keyboard
  • control a sprite with the mouse
  • collisions between elements (sprites)
  • Artificial intelligence (or lack thereof)
  • sound reproduction (some condition to be met)

Part 4: managing text : The name says it all, an explanation of how to display text on the screen.

I will update the links for Part 2, 3 & 4 as soon as i post them
Please check back or subscribe for updates
and Have fun ! :D

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top