Python
  Home arrow Python arrow A PyGame Working Example: Starting a Game
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

A PyGame Working Example: Starting a Game
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2006-02-07


    Table of Contents:
  • A PyGame Working Example: Starting a Game
  • Preparing a Level
  • Creating a Level
  • Sprite Definitions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    A PyGame Working Example: Starting a Game
    ( Page 1 of 4 )

    In PyGame for Game Development, I showed you the very basics of PyGame's graphical side. However, creating a game with PyGame requires a bit more. All the concepts described before need to be glued together somehow, and new concepts will need to be introduced in order to create a functional game. In this article, we'll do just that by tackling a working example of PyGame's capabilities—a Python-powered game.

    The Game Plan

    Before we jump into Python code, we need to develop the idea behind our game. Nothing complex is needed for our game, since its purpose is just to reinforce the basics of PyGame and demonstrate how to put them to use. With that said, let's move into the idea. The idea is to have the player at the bottom of the screen with the ability to move horizontally. Horizontal movement will be required to dodge incoming objects that will constantly be hurled at the player at fast speeds. Picture the player as a ship having to navigate at high speeds (which will be essentially an illusion, since the player will not be moving vertically; the incoming objects will) and dodge mines or enemy ships.

    However, to give it an added degree of complexity and to make it more dynamic, levels will be stored as Python objects. Each level object will be responsible for loading the required images and laying them out in a layout list. The layout list will contain many more lists as elements that correspond to rows of objects. So, for example, picture a layout list as looking something like this:

    [[0, 0, 0, 0],

     [1, 0, 1, 0],

     [1, 0, 1, 0],

     [1, 0, 1, 0],

     [0, 0, 0, 0]]

    A zero would be mapped to a blank space, and a one would be mapped to an object of some sort. The result would look something like this, to draw a very rough sketch with gridlines for visibility:


    The green block represents the player, and the red blocks represent the objects that the player is required to dodge. We would simply have to move the objects down and see if the player has collided with any of the objects.

    Of course, since the levels would be generated by Python classes that determine what the images are and how they are positioned, levels would not be limited to the mine-dodging scenario I presented earlier. You could have a fish dodging sharks or a spaceship dodging asteroids if you felt inclined to do so.

    Also, it's possible to create random object layouts or images using this method. This way, levels could be different each time they are played.



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek