Python
  Home arrow Python arrow Page 5 - 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 - The Game in Action
    ( Page 5 of 5 )

    All that's left now is creating a Python script that will make use of our level and game module. To run our level, simply create a file named playAsteroid.py:

    import pydodge

    pydodge.loadLevel('asteroid')
    pydodge.setup()
    pydodge.loadBackground()
    pydodge.loadSprites()
    pydodge.run()

    Of course, we can always customize our game a bit more. Let's say that we want to use a title screen rather than forcing the user to jump right into the level. Also, let's display either “Level Complete” or “Game Over”:

    import pydodge
    import pygame

    pydodge.loadLevel('asteroid')
    pydodge.setup()
    pydodge.loadBackground()

    # Add a title
    font1 = pygame.font.Font(None, 25)
    text1 = font1.render('PyDodge Asteroid', True, (255, 255, 255))
    textRect1 = text1.get_rect()
    textRect1.centerx = pydodge.screen.get_rect().centerx
    textRect1.y = 100
    pydodge.screen.blit(text1, textRect1)

    # Add "Press <Enter> To Play"
    font2 = pygame.font.Font(None, 17)
    text2 = font2.render('Press <Enter> To Play', True, (255, 255,
    255))
    textRect2 = text2.get_rect()
    textRect2.centerx = pydodge.screen.get_rect().centerx
    textRect2.y = 150
    pydodge.screen.blit(text2, textRect2)

    # Update the screen
    pygame.display.update()

    # Wait for enter to be pressed
    # The user can also quit
    waiting = True
    while waiting:
       for event in pygame.event.get():
          if event.type == pygame.QUIT:
             sys.exit()
          elif event.type == pygame.KEYDOWN:
             if event.key == pygame.K_RETURN:
                waiting = False
                break

    pydodge.loadBackground()
    pydodge.loadSprites()

    # The user has won the game
    if pydodge.run(100, 300):
       text3 = font1.render('Level Complete', True, (255, 255, 255))
       textRect3 = text3.get_rect()
       textRect3.centerx = pydodge.screen.get_rect().centerx
       textRect3.y = 150
       pydodge.screen.blit(text3, textRect3)

    # The user has lost the game
    else:
       text3 = font1.render('Game Over', True, (255, 255, 255))
       textRect3 = text3.get_rect()
       textRect3.centerx = pydodge.screen.get_rect().centerx
       textRect3.y = 150
       pydodge.screen.blit(text3, textRect3)

    pygame.display.update()

    # Wait for the user to quit
    while True:
       for event in pygame.event.get():
          if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN):
             sys.exit()

    Conclusion

    As you can see, creating a functioning game with PyGame is rather easy. Our game module weighs in at around five kilobytes. Using the module, you can also customize games, loading whatever levels you would like and displaying extra messages and what-not.

    From here, try customizing your game even further. You can try adding a menu where the user can select a difficulty level. You can also link multiple levels together and randomize the layout lists in levels. It's up to your imagination.

    Of course, there's a lot more to PyGame than a simple space game like this, so feel free to explore the library and examine one of the many example games available on the PyGame website. Good luck!



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