Python
  Home arrow Python arrow A PyGame Working Example, continued
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, continued
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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, 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
     

       

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek