Python
  Home arrow Python arrow Page 2 - 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 - Preparing a Level
    ( Page 2 of 4 )

    Let's start by making a level and building the game around it. The idea is to create the level out of an object that loads the required images and returns the layout list described in the last section. Before we do that, though, let's create a class from which all level classes can be derived. That way, we can organize things a bit better:

    class Level:

       def getPlayer(self):
          pass
       def getObjects(self):
          pass
       def getLayout(self):
          pass
       def getBackground(self):
          pass

    Save the script as levelbase.py.

    With the actual levels that come from the Level class, our game will be concerned with four methods: getPlayer, getObjects, getLayout and getBackround. The first method will be responsible for returning the player's image. The second method will be responsible for returning a list of the images of the objects involved. The third method will be responsible for returning the layout list. The final method returns the background image, but it will also need to return the number of rows visible on the screen at a time. This way, we can divide up the background and figure out how long each space will be. The width of each space will be determined by measuring how big the first element (which will be a list itself, of course) is inside of the layout list.

    Each element of the layout list will be a number. Zero will represent a blank space, and one and above will represent objects. These objects will be based on images obtained from getObjects, but since the list index will start at zero, we'll have to subtract from each of the elements in getLayout to obtain the proper image. So, to express this in more basic terms, a value of 1 in getLayout would correspond to the first object in getObjects (0), and a value of 2 in getLayout would correspond to the second object in getObjects (1).



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