Friday, October 27, 2006

Simple interactive pygame

Here is the simplest interactive game written in pygame, I have seen so far:

#import pygame and the system library
import sys, pygame

#initialize
pygame.init()

# set the window properties
size = (400,400)
screen = pygame.display.set_mode(size)

while 1:
# enable window quit button
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

# clean screen
black = (0,0,0)
screen.fill(black)
# draw new circle
color = (50, 220, 100)
radius = 8
pygame.draw.circle(screen, color, pygame.mouse.get_pos(), radius)
# update display
pygame.display.update()
# delay
pygame.time.delay(100)


Pygame is a cross-platform set of Python modules designed for writing games.

The simplest example I could find was Python Pygame Introduction or Help! How Do I Move An Image?, by Pete Shinners which are too long for my taste. To achieve more complex graphical interactions you definitively want to learn how to use sprites.