Python
  Home arrow Python arrow Page 3 - A PyGame Working Example: Starting a G...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 - 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


       · 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 2 hosted by Hostway