Python
  Home arrow Python arrow Page 4 - PyGame for Game Development: Sprite Gr...
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 
 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

PyGame for Game Development: Sprite Groups and Collision Detection
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    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:
      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

    PyGame for Game Development: Sprite Groups and Collision Detection - Explaining the Collision Code


    (Page 4 of 4 )

    It seems like a lot of code, but most of it should be familiar to you. We start out by creating the StickMan sprite class and the sprite erasing function. We then create three non-playable and non-moving sprites and add them each to a group, followed by the player's character and its own group. After we draw each sprite, we enter the game loop. Here, we check to see whether the user has pushed a key. If he or she has, we update the player sprite accordingly.

    With the updated positions, we then move on to the collision tests. As I mentioned before, there are three methods that are responsible for this. The first is spritecollide, and it accepts three arguments. The first is a sprite, and the second is a group. If the two collide and the third argument is set to True, then the colliding sprites are removed from the group. The method returns a list of the colliding sprites. The second method is groupcollide, and it accepts two groups and two boolean values. If the first boolean value is set to True, then the colliding sprites in the first group are removed. If the second is set to True, then the colliding sprites in the second group are removed. A dictionary is returned with the sprites of the first group as the keys and the colliding sprites as the values. The last method is spritecollideany. It simply checks to see whether a sprite collides with a group, and it returns a boolean value. Since it doesn't do anything too special, like removing sprites, then it is the fastest.

    Next, we check to see whether the player sprite still belongs to a group and hasn't been removed by a collision method. If it has been removed, then we exit the game. Otherwise, we move the player and update the other sprites if we need to.

    Conclusion

    PyGame provides an easy interface to graphical tasks (as well as other tasks) for use in Python-powered games or even applications. Fonts are simple to load and display text in, and it's easy to load images and display them to a Surface object—the graphical building block of PyGame applications. Moreover, sprites can be easily created and used to simplify the drawing process and provide a more object-oriented approach to it, and sprite groups provide an efficient way to organize sprites and accomplish tasks such as collision detection. If you are looking to build a simple game in Python or an application with a unique interface, consider using PyGame.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello, all,This is ment to be a continuation of the last article, so if you...
       · No, no artist - :), but I a great tutor. Thanks for writing up the into to PyGame....
       · On your last example I had a problem. You create 3 groups and added the three...
       · Hey, Jeff,Try upgrading to the latest version of PyGame. Sorry for the late...
     

       

    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 3 hosted by Hostway