Python
  Home arrow Python arrow Page 3 - 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? 
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 - Creating a Level
    ( Page 3 of 4 )

    Now, we're ready to create a level for our game. Name this asteroid.py:

    import levelbase
    import pygame

    class Level(levelbase.Level):

       def getPlayer(self):
          return pygame.image.load('ship.gif')
       def getObjects(self):
          return [pygame.image.load('asteroid.gif')]
       def getLayout(self):
          return [[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],\
                  [0, 1, 1, 0, 0, 0, 0, 1, 1, 0],\
                  [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\
                  [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],\
                  [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\
                  [1, 1, 1, 0, 0, 0, 1, 1, 0, 0],\
                  [0, 0, 0, 0, 1, 0, 0, 0, 0, 1],\
                  [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],\
                  [1, 1, 0, 0, 0, 0, 1, 0, 0, 0],\
                  [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]]
       def getBackground(self):
          return pygame.image.load('background.gif'), 5

    As you can see, we simply return the required images, along with the layout list and the number of rows that will be visible at once. This particular level goes with one of the ideas I described above: a spaceship dodging asteroids. Here are the images that the level references:

    ship.gif


    asteroid.gif


    background.gif


    Notice how the background is not solid, but, rather, it's an image. Because of this, we can't simply erase a sprite by drawing a solid-colored Surface object over it. Instead, we have to get the original background image that the sprite replaced and then draw it over the sprite. Thankfully, this is very simple, and we'll look at how it's done later on.



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT