Python
  Home arrow Python arrow PyGame for Game Development: Font and ...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

PyGame for Game Development: Font and Sprites
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 28
    2006-01-24

    Table of Contents:
  • PyGame for Game Development: Font and Sprites
  • Using Images
  • Adding Text
  • Sprites

  • 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

    PyGame for Game Development: Font and Sprites
    (Page 1 of 4 )

    PyGame is a library for Python that allows you to develop computer games. It is also useful for very graphical applications. This article will help you grasp the basics of PyGame.

    Introduction

    Though game development with Python and C++ is becoming more popular (take a look at Civilization IV), game development with Python alone is largely limited to hobbyist use. It is possible, however, with PyGame, a library written around the Simple DirectMedia Layer (SDL). PyGame isn't limited to game development, either. It can also be used in applications that require very graphical interfaces. In this article, we'll look into the basics of how PyGame is used.

    PyGame may be obtained at its website, which also contains links to a number of projects written in PyGame:

    http://pygame.org/

    Download and install PyGame, and we'll be ready to begin.

    Getting Started

    The first thing we'll do with PyGame is open up a PyGame window with a certain size and caption. We'll also fill it up with a background color and keep the window open until the user decides to quit:

    import pygame
    import sys

    # Initialize pygame
    pygame.init()

    # Create the drawing screen
    screen = pygame.display.set_mode((256, 256))

    # Set the caption
    pygame.display.set_caption('Application')

    # Set a background color
    screen.fill((159, 182, 205))

    # Update the screen
    pygame.display.update()

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

    The above application is pretty simple to understand. We start out by initializing PyGame. Then, we create the window and a drawing screen, passing a tuple with the drawing screen dimensions. Next, we use the set_caption method to set a caption. We then fill the screen with a background color, passing a tuple of values for red, green and blue that generate a nice bluish color. When we make a change to the screen, we have to call update to display the change. Finally, we wait for the user to quit the application. The pygame.event.get method returns a list of events when called, and we examine this list for a pygame.QUIT event.

    The pygame.QUIT constant is simply an integer. Each event type—whether it's for a key push or a mouse click—is a unique integer. An Event object returned by pygame.event.get also has other attributes that we can examine, depending on what type of event it is. For example, an Event object that represents a key push, pygame.KEYDOWN, contains a key attribute that contains the key pushed.

    It is important to note exactly what screen is in this example. The screen object is a Surface object, which may be drawn on. Here, we fill it with a color, but note that we could also create a Surface and load an image onto it. Every image you create will be a Surface. Knowing this is important because Surface objects will be the building blocks of your applications.

    More Python Articles
    More By Peyton McCullough


       · Hey, all,I've always though PyGame was pretty interesting. While it's really...
       · The PyWeek challenge invites entrants to write a game in one week fromscratch...
       · I own a Logitech Formula Force GP Racing Wheel and would like to use that in pygame...
       · I've never owned a racing wheel, much less tried to use one with PyGame, but the...
       · hithere is a problem with the last listing:it won't execute properly :(just...
     

       

    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