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

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

    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

    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!


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





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