Python
  Home arrow Python arrow A PyGame Working Example: Starting a G...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
PYTHON

A PyGame Working Example: Starting a Game
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · Hey, all,I wrote this article to demonstrate how to apply the PyGame concepts...
       · Hey, great article! It introduced me to some new ideas that I'm excited to try out....
       · First, your articles are super helpful! Also, I was wanting to write a hex-based...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway