SunQuest
 
       Python
  Home arrow Python arrow Page 4 - 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 
Moblin 
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: 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
     
    IBM developerWorks
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    A PyGame Working Example: Starting a Game - Sprite Definitions


    (Page 4 of 4 )

    Next, we'll need to define the classes for player sprites as well as object sprites. Since the player sprite has no ability to move vertically, it will only have to be moved by its x-position. So, the player class's update method will only need to accept the amount its x-position needs to move. Likewise, because objects will have a static x-position, the object class's update method will only need to accept the amount to update the y-position. Also, our game will move objects by their center positions, not by their upper-left corners. We'll use centerx and centery rather than x and y.

    import pygame

    class Player(pygame.sprite.Sprite):

       def __init__(self, image, xCenter, yCenter):

          pygame.sprite.Sprite.__init__(self)

          self.screen = pygame.display.get_surface().get_rect()
          self.image = image
          self.rect = self.image.get_rect()
          self.rect.centerx = xCenter
          self.rect.centery = yCenter

       def update(self, xAmount):

          self.rect = self.rect.move([xAmount, 0])

          # Check to see if we have gone offscreen
          if (self.rect.x < 0) or (self.rect.x >= self.screen.width):
             self.rect = self.rect.move([-xAmount, 0])

    class Object(pygame.sprite.Sprite):

       def __init__(self, image, xCenter):

          pygame.sprite.Sprite.__init__(self)

          self.image = image
          self.rect = self.image.get_rect()
          self.rect.centerx = xCenter

       def update(self, yAmount):

          self.rect = self.rect.move([0, yAmount])

    Save the script as gamesprites.py. We'll import it as a module when we need to create the sprites for our game. The Player class accepts three arguments when it is initialized: the player image, its center x-position and its center y-position. Note that the player image will already be loaded onto a Surface object by the level, so there's no need to load it again. The Object class only accepts two arguments, the object's image and the center x-position. Since all objects will start at the top of the screen, there's no need to accept any y-position. The update methods simply accept the amount the sprites need to move, with each class moving in a different direction, as described before. Additionally, the Player class checks to see if the sprite has gone off the screen. If it has, we undo any movement to put it back on the screen.

    The Internals

    The functional code of the game will make up its own module, and related Python instructions will be grouped into the module's methods. This is so we can customize the game later on by importing the module, calling whatever methods we need to call, and passing whatever level we want to play rather than modifying the internals of the game. Using this method of organizing the game, it's possible to add extra features such as a menu rather than limiting it to one game. This also allows us to create custom “Game Over” or success screens rather than relying on a generic one or modifying the internals to create a more specific one. We could also create either a linear or semi-dynamic campaign by going this route.


    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.

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