Whenever I begin a new Pygame application, I always start with a ‘basic program’ to begin things. Saves me from having to re-type all the simple stuff. Obviously a Pygame program can get far more complex than this, but everything has to start somewhere. Thought I’d post it up for reference.
"""
default.py
www.akeric.com - 2010-09-07
Default, base, initial setup for a pygame program.
In this case, black background, white circle follows mouse position, and
framerate is shown in the title-bar.
"""
#-------------------------------------------------------------------------------
# Imports & Inits
import sys
import pygame
from pygame.locals import *
pygame.init()
#-------------------------------------------------------------------------------
# Constants
VERSION = '1.0'
WIDTH = 512
HEIGHT = 512
FRAMERATE = 60
#-------------------------------------------------------------------------------
# Screen Setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
bgCol = Color('black')
clock = pygame.time.Clock()
moduleName = __file__.split('\\')[-1]
#-------------------------------------------------------------------------------
# Define helper functions, classes, etc...
def spam():
pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, Color('white'), pos, 32)
#-------------------------------------------------------------------------------
# Main Program
def main():
print "Running Python version: %s"%sys.version
print "Running PyGame version: %s"%pygame.ver
print "Running %s version: %s"%(moduleName, VERSION)
looping = True
# Main Loop-----------------------
while looping:
# Maintain our framerate, set caption, clear background:
clock.tick(FRAMERATE)
pygame.display.set_caption("%s - FPS: %.2f" %(moduleName,clock.get_fps()) )
screen.fill(bgCol)
# Detect for events-----------
for event in pygame.event.get():
if event.type == pygame.QUIT:
looping = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
looping = False
# Do stuff!-------------------
spam()
# Update our display:---------
pygame.display.flip()
#-------------------------------------------------------------------------------
# Execution from shell\icon:
if __name__ == "__main__":
# Make running from IDE work better:
sys.exit(main())