klenwell information services : PygameDemo01

Pygame Basic Demo Script

return to Pygame

# -*- coding: utf-8 -*-
"""
    Demo 1: Pygame
   
    Simple pygame window with basic shapes, text, and caption.

    USAGE
    python 01_pygame.py [profile]
"
""
#
# IMPORTS
#
# StdLib Imports
import sys
import time

# Pygame
import pygame
from pygame.color import THECOLORS as RGB


#
# Globals / Constants
#


#
# API
#  
class GameOver(Exception): pass


#
# Simulation Class
#
class Simulation(object):
   
    screen_width = 300
    screen_height = 300
    seconds = 10.0
    fps = 30
   
    def __init__(self):
        # init game and window
        pygame.init()
        self.window = pygame.display.set_mode(
            (Simulation.screen_width, Simulation.screen_height))
       
        # clock management
        self.frame = 0
        self.start_at = time.time()
        self.max_frame = Simulation.fps * Simulation.seconds
        self.clock = pygame.time.Clock()
       
        # text
        self.font = pygame.font.Font(None, 36)
        self.text_image = self.create_text_image("Welcome to Pygame")
        self.game_over_text_image = self.create_text_image("game over")
   
    def update(self):
        # tick clock
        self.frame += 1
        self.clock.tick(Simulation.fps)
       
        # handle events
        self.handle_events()
       
        # game over?
        if self.frame >= self.max_frame:
            raise GameOver("time is up")
       
    def paint(self):
        self.update_caption()
       
        if self.duration < 5.0:
            self.display_text()
        else:
            self.display_game_over()
           
        pygame.display.flip()
   
    def run(self):
        while True:
            self.update()
            self.paint()
           
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                raise GameOver("user requested game over")
   
    def create_text_image(self, text):
        # render text
        alias = 1
        color = RGB["yellow"]
        rendered_font = self.font.render(text, alias, color)

        # create text image
        text_image = pygame.Surface(
            (self.screen_width, self.screen_height))

        # center text in box
        text_frame = rendered_font.get_rect()
        text_frame.center = text_image.get_rect().center

        # blit rendered_font to textbox
        text_image.blit(rendered_font, text_frame)
       
        return text_image
   
    def display_text(self):
        screen_pos = self.text_image.get_rect()
        screen_pos.center = self.window.get_rect().center
        self.window.blit(self.text_image, screen_pos)
   
    def display_game_over(self):
        screen_pos = self.game_over_text_image.get_rect()
        screen_pos.center = self.window.get_rect().center
        self.window.blit(self.game_over_text_image, screen_pos)
           
    def update_caption(self):
        pygame.display.set_caption("Frame: %d / FPS: %.4f" % (
            self.frame, self.frame / self.duration))
           
    @property
    def duration(self):
        return time.time() - self.start_at
 

#
# Main
#
def main():
    try:
        sim = Simulation()
        sim.run()
    except GameOver, e:
        print "[%s] %s frames complete in %.4fs" % (e, sim.frame, sim.duration)
        time.sleep(2)
        pygame.quit()
        sys.exit(0)

if __name__ == "__main__":
    main()