Python
  Home arrow Python arrow Page 3 - 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 - Cleaning Up
    ( Page 3 of 5 )

    In earlier examples, we never used a very complex background. Because of this, erasing a sprite was simply a matter of filling up its position with a solid color. However, this time, we have a graphical background with many different elements at unique positions. Each time an object moves, we'll need to replace its old position with the corresponding piece of background. Otherwise, things definitely wouldn't look right.

    Thankfully, this is quite easy to do. Recall that when loading the background image, we store it in a variable called background. This object certainly hasn't gone anywhere, and we're free to access its contents—an original copy of the background—at any time. So, to erase an object in our game, we'll simply clip the corresponding rectangle from the original background image and draw it in its proper place on the screen:

    def erase(screen, rect):

       # Get the piece of the original background and copy it to the
    screen
       screen.blit(background.subsurface(rect).copy(), rect)

    The subsurface method simply creates a “Surface object within a Surface object.” Both of the objects share pixels, so by creating a Surface object within our background, we have access to the exact contents of the area we need to access. We simply copy it and blit it to its proper place on the screen. It's a lot easier than it sounds, taking just one line to do.



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