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

       

    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
    Stay green...Green IT