Python
  Home arrow Python arrow Page 2 - PyGame for Game Development: Sprite Groups and Collision Detection
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

PyGame for Game Development: Sprite Groups and Collision Detection
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-01-31


    Table of Contents:
  • PyGame for Game Development: Sprite Groups and Collision Detection
  • Explaining the Group Class
  • Collision
  • Explaining the Collision Code

  • 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


    PyGame for Game Development: Sprite Groups and Collision Detection - Explaining the Group Class
    ( Page 2 of 4 )

    We start off by creating our StickMan sprite class with __init__ and update methods. Then, we define a function that will be responsible for clearing the previous position of sprites. This will be used later on in the code by the sprite group. Next, we create three sprites and then a sprite group named stickGroup from the Group class. We add our sprites to this group and then define some special attributes that hold the direction the sprites will be moving, the y-position of the sprites and the height of the sprites. Since the sprites will be moving in unison, we can do this. We then create an event that will be added to the event queue every one hundred milliseconds. This will be used to trigger screen updates. In our loop, if the event is found in the event queue, we update the y-position of the sprites (actually, the stickGroup's y attribute, which is later passed to the sprites), making adjustments and changing the direction if needed.

    Now we begin to see the magic of sprite groups. In a single method call, we clear the sprites. We pass screen and our eraseSprite function we defined earlier. Two arguments, the surface to be drawn on and the area that needs to be cleared, are passed to the eraseSprite function. We then update the sprites in another method call, passing the new y-position. This simply calls the update method of each of the sprites. Finally, we blit the sprites and update the changed rectangles.

    Of course, the benefit doesn't end there. Notice how the section of our script that gets the updated rectangles is multiple lines long. This is easy fixed by using the RenderUpdates sprite group class rather than the Group class.

    ...
    stickGroup = pygame.sprite.RenderUpdates()
    ...

    The RenderUpdates class returns a list of updated rectangles when we call its draw method. We can then pass these to the update method:

    ...
    updateRects = stickGroup.draw(screen)
    pygame.display.update(updateRects)
    ...

    We can also get rid of StickMan's old attribute, since it is no longer needed. RenderUpdates finds the old positions automatically and throws them into the list of updated rectangles.

    PyGame also contains a class called GroupSingle that can contain only a single sprite. When a new sprite is added, the old one removed. Normally, a sprite is removed from a group using the remove method. You can also empty the sprite group entirely:

    >>> import pygame
    >>>
    >>> sprite1 = pygame.sprite.Sprite()
    >>> sprite2 = pygame.sprite.Sprite()
    >>>
    >>> group = pygame.sprite.Group()
    >>> group.add(sprite1, sprite2)
    >>>
    >>> group.remove(sprite1)
    >>> print group.sprites()
    [<Sprite sprite(in 1 groups)>]
    >>>
    >>> group.empty()
    >>> print group.sprites()
    []

    If none of PyGame's sprite group classes suit you, it's always possible to subclass an existing class and tweak it to fit your needs. This way, you can modify existing methods and create new ones that are more specific to your application.



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