SunQuest
 
       Python
  Home arrow Python arrow Page 2 - 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  
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, continued
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    A PyGame Working Example, continued - Setting Things Up


    (Page 2 of 5 )

    Before our game is allowed to work, we must initialize PyGame and create the necessary Sprite objects. The first function we'll define is the setup function, which simply initializes PyGame and creates a screen for us to work with:

    def setup():

       global screen
       pygame.init()
       screen = pygame.display.set_mode((background.get_rect().width,
    background.get_rect().height))

    Now that we have a Surface object to draw on, we must blit the background image onto the Surface object. As you know already, this isn't rocket science:

    def loadBackground():

       screen.blit(background, background.get_rect())
       pygame.display.update()

    Finally, we have to create our game's Sprite objects. The first Sprite object we'll have to create is the player's sprite. We'll position the sprite in the bottom left of the player's screen to begin with, which will require a few calculations based on the data we retrieved when loading the level. The player Sprite should also be added to its own group:

    def loadSprites():

       global player, playerSprite

       # Find the position of the player
       colWidth = background.get_rect().width / columns
       xPlayer = colWidth / 2
       rowHeight = background.get_rect().height / rows
       yPlayer = (rowHeight * (rows - 1)) + (rowHeight / 2)

       # Load the player sprite
       playerSprite = gamesprites.Player(player, xPlayer, yPlayer)

       # Create a player sprite group
       player = pygame.sprite.RenderUpdates(playerSprite)

    Next come the object sprites. Each non-zero position in the layout list will need to be converted into a sprite, and we'll need to calculate the exact x-position of each object:

    def loadSprites():

       ...

       # Load each object sprite
       for y in xrange(len(layout)):
          for x in xrange(len(layout[y])):
             if layout[y][x]:
                layout[y][x] = gamesprites.Object(objects[layout[y]
    [x] - 1], (colWidth * (x)) + (colWidth / 2))

    In the above code section, we simply iterate through the list and replace every non-zero element with a corresponding Sprite object. We'll also need to convert each row in the layout list to a group. This way, we can move an entire row of objects at once:

    def loadSprites():

       ...

       # Turn each layout row into a sprite group
       for y in xrange(len(layout)):
          group = pygame.sprite.RenderUpdates()
          for x in xrange(len(layout[y])):
             if layout[y][x]:
                group.add(layout[y][x])
          group.y = 0
          layout[y] = group

    Now, we've reduced our list to a simple list of sprite groups.

    More Python Articles
    More By Peyton McCullough


       · Hey, all,This is a continuation of the last PyGame article, and it focuses on...
       · Note that some of the examples don't take full advantage of the API. For...
       · Hey there, great job w/ this tutorial, this is really helping!Just a little note...
     

       

    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...

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway