Python
  Home arrow Python arrow A PyGame Working Example, continued
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 
IBM Rational Software Development Conference
 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, continued
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-02-14

    Table of Contents:
  • A PyGame Working Example, continued
  • Setting Things Up
  • Cleaning Up
  • Constructing the Main Loop
  • The Game in Action

  • 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, continued
    (Page 1 of 5 )

    In this fourth article in our series covering the creation of a video game using Python and PyGame, we will code the internals of our game.

    The Game

    Now it's time to begin coding the internals of our game. Create a file called pydodge.py (after all, our game is about dodging objects) to store our game module, and let's jump into some code. First, we'll need to import the required modules:

    import imp
    import gamesprites
    import pygame
    import sys

    First, we import the imp module. The imp module will allow us to import modules in a more dynamic fashion. Since we store our levels as Python classes and will access them by importing them as modules, this functionality is needed. We then import the gamesprites module, which we constructed earlier to store the game's Sprite classes, as well as pygame itself. Finally, sys is imported since we need to make use of sys.exit.

    Next, we'll need to take care of some global variables:

    level = None
    player = None
    playerSprite = None
    objects= None
    background = None
    rows = None
    columns = None
    layout = None
    screen = None

    Don't worry about what each one of these means right now. We'll cover the purpose of each one as it is used.

    Loading Levels

    The first thing that needs to be done is the loading of levels. We'll need to import the specified level file and then extract some basic information from it, such as the object images and the layout of the level as a whole:

    def loadLevel(levelFile):

       # Import the level and extract the data
       global level, player, objects, background, rows, columns,
    layout
       level = imp.find_module(levelFile)
       level = imp.load_module('level', level[0], level[1], level[2])
       level = level.Level()
       player = level.getPlayer()
       objects = level.getObjects()
       background, rows = level.getBackground()
       layout = level.getLayout()
       columns = len(layout[0])

    In the above function, we use the imp module to search for the level by its name. Then, we import it as level. However, since we only need the single Level class that it contains, we re-assign level to an instance of the contained Level class. We then extract the player image and assign it to the player variable, and we get the list of object images and assign it to the objects variable. Next, we get the background image and the number of rows that will be visible at once. Finally, we get the layout list and retrieve the number of columns it calls for.

    More Python Articles
    More By Peyton McCullough


       · Hey, all,This is a continuation of the last PyGame article, and it focuses on...
       · Note that some of the examples don't take full advantage of the API. For...
       · Hey there, great job w/ this tutorial, this is really helping!Just a little note...
     

       

    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 1 hosted by Hostway