[Tutor] my first pygame experiment
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sat, 19 Jan 2002 22:27:33 -0800 (PST)
Hi everyone,
I'm finally starting to play around with pygame, and I really think it
looks great! I thought it might be fun to show my first silly pygame
program:
###
"""A simple hypnotic pulsing screen. Uses Python 2.2 generators.
Danny Yoo (dyoo@hkn.eecs.berkeley.edu)
"""
from __future__ import generators
import pygame
from pygame.locals import *
from random import randrange
import sys
def strobeIter(n, step=1, start=0):
"""Returns a "strobing" iterator. This iterator bounces between
the values [0, n]. Think KIT's red strobing light."""
x = start
direction = 1
delta = step*direction
while 1:
yield x
x = clamp(x+delta, 0, n)
if x in (0, n):
direction = direction * -1
delta = step * direction
def clamp(x, low, high):
"""Clamps down x within the range: [low, high]."""
return max(low, min(x, high))
def handleQuit():
for event in pygame.event.get():
if event.type is QUIT:
sys.exit(0)
elif event.type is KEYDOWN and event.key is K_ESCAPE:
sys.exit(0)
if __name__ == '__main__':
(r, g, b) = (strobeIter(255, randrange(1, 10)),
strobeIter(255, randrange(1, 10)),
strobeIter(255, randrange(1, 10)))
display = pygame.display.set_mode((300, 300))
while 1:
handleQuit()
bgcolor = r.next(), g.next(), b.next()
display.fill(bgcolor)
pygame.display.flip()
###