Python
  Home arrow Python arrow Page 3 - PyGame for Game Development: Font and Sprites
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? 
Google.com  
PYTHON

PyGame for Game Development: Font and Sprites
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 31
    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:
      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


    PyGame for Game Development: Font and Sprites - Adding Text
    ( Page 3 of 4 )

    PyGame also makes it easy to insert text into applications. The pygame.font module is responsible for this, and it can be done in just a few lines of Python:

    import pygame
    import sys

    pygame.init()
    screen = pygame.display.set_mode((256, 256))
    pygame.display.set_caption('Application')
    screen.fill((159, 182, 205))

    # Create a font
    font = pygame.font.Font(None, 17)

    # Render the text
    text = font.render('Powered by Python and PyGame', True, (255,
    255, 255), (159, 182, 205))

    # Create a rectangle
    textRect = text.get_rect()

    # Center the rectangle
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery

    # Blit the text
    screen.blit(text, textRect)

    pygame.display.update()

    while True:
       for event in pygame.event.get():
          if event.type == pygame.QUIT:
             sys.exit()

    We start by calling the Font method, passing two arguments. The first argument is the filename of the text to use, but since we pass None, a generic font is used. The second argument is simply the size of the font, which we set to 17. We must then render the text we want using the font. To do this, we pass four arguments to render. The first argument is, obviously, the text that is to be rendered. The second is a boolean value that indicate whether or not antialiasing is to be used. The third argument is the color of the font, and the fourth argument is the background color. If the fourth argument is missing, the background becomes transparent. However, this is slow, and since we know what color the background will be already, we can just pass the color as an argument. Next, we create a rectangle and use centerx and centery to center it in the screen. Finally, we blit the text.

    PyGame also supports some text effects—bold text and italicized text. Bolding or italicizing a font involves calling a single method:

    ...
    font.set_italic(True)
    font.set_bold(True)
    ...

    It's possible to search for a specific font on the user's computer, and it's also possible to get a list of the user's fonts. We can also specify a list of fonts and have PyGame return the first match. This way, your application can fall back on another font if the best font isn't available:

    import pygame
    import random
    import sys

    pygame.init()
    screen = pygame.display.set_mode((256,256))
    pygame.display.set_caption('Application')
    screen.fill((159, 182, 205))

    # Search for Verdana and create a Font object
    verdana = pygame.font.match_font('Verdana')
    verdanaFont = pygame.font.Font(verdana, 13)

    # Search for a font that does not exist and create a Font object
    doesNotExist = pygame.font.match_font('doesNotExist')
    dNEFont = pygame.font.Font(doesNotExist, 13)

    # Search for a font that does not exist, followed by Arial, and create a Font object
    arial = pygame.font.match_font('doesNotExist,Arial')
    arialFont = pygame.font.Font(arial, 13)
    print arial, arialFont

    # Get a list of fonts
    fonts = pygame.font.get_fonts()

    # Create some random fonts
    # get_fonts() merely returns a list of names, so we'll have to
    match them
    font1 = pygame.font.Font(pygame.font.match_font(random.choice
    (fonts)), 13)
    font2 = pygame.font.Font(pygame.font.match_font(random.choice
    (fonts)), 13)
    font3 = pygame.font.Font(pygame.font.match_font(random.choice
    (fonts)), 13)

    # Render text in verdana, doesNotExist, arial and the random
    fonts
    text1 = verdanaFont.render(str(verdana), 1, (255, 255, 255),
    (159, 182, 205))
    text2 = dNEFont.render(str(doesNotExist), 1, (255, 255, 255),
    (159, 182, 205))
    text3 = arialFont.render(str(arial), 1, (255, 255, 255), (159,
    182, 205))
    text4 = font1.render('Random', 1, (255, 255, 255), (159, 182,
    205))
    text5 = font2.render('Random', 1, (255, 255, 255), (159, 182,
    205))
    text6 = font3.render('Random', 1, (255, 255, 255), (159, 182,
    205))

    # Make some rectangles
    rect1 = text1.get_rect()
    rect2 = text2.get_rect()
    rect3 = text3.get_rect()
    rect4 = text4.get_rect()
    rect5 = text5.get_rect()
    rect6 = text6.get_rect()

    # Position the rectangles
    rect1.y, rect1.centerx = 0, screen.get_rect().centerx
    rect2.y, rect2.centerx = 20, screen.get_rect().centerx
    rect3.y, rect3.centerx = 40, screen.get_rect().centerx
    rect4.y, rect4.centerx = 60, screen.get_rect().centerx
    rect5.y, rect5.centerx = 80, screen.get_rect().centerx
    rect6.y, rect6.centerx = 100, screen.get_rect().centerx

    # Blit everything
    screen.blit(text1, rect1)
    screen.blit(text2, rect2)
    screen.blit(text3, rect3)
    screen.blit(text4, rect4)
    screen.blit(text5, rect5)
    screen.blit(text6, rect6)

    pygame.display.update()

    while True:
       for event in pygame.event.get():
          if event.type == pygame.QUIT:
             sys.exit()

    Here, we simply search for some fonts and then get three random fonts, displaying a string of text in each font. Notice how None is returned if a font does not exist. This is why it's a good idea to either specify a list of fonts or include the fonts you'll be using (provided that you are allowed to).



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek