#!/usr/local/bin/python ## # Brian's Theme (Object-oriented version) # --------------------------------------- # This version copyright (c) 1996-1998 by Lachlan Patrick. # Written using GraphApp. # # This is a complex example not for beginners. It makes use of # the timer functions, colour, windows and keyboard call-backs. ## from graphapp import * import os, sys, string import time, random ## # Define the class: ## class BriansTheme: def __init__ (this): this.width = 400 this.height = 240 this.banner_height = 40 this.banner = "0-9 for step size, Q to stop, R,G,B,C,M,Y,K for colours." this.win = None this.step_size = 3 this.step_location = 0 this.origin = pt(0,0) this.colour = Blue ## # Class functions: ## def rand(this, max): return int(random.random() * max) def change_location(this): this.origin.x = rand(this.width) this.origin.y = rand(this.height) this.step_location = 0 def handle_mouse(this, w, buttons, xy): this.origin = xy this.step_location = 0 redraw(this.win) def handle_keydown(this, w, key): if (key in string.digits): this.step_size = ord(key) - ord('0') elif ((key == 'q') or (key == 'Q') or (key == ESC)): exitapp() else: key = string.lower(key) if (key == 'r'): this.colour = Red elif (key == 'g'): this.colour = Green elif (key == 'b'): this.colour = Blue elif (key == 'c'): this.colour = Cyan elif (key == 'm'): this.colour = Magenta elif (key == 'y'): this.colour = Yellow elif (key == 'k'): this.colour = Black def draw_window(this, w, r): if (this.banner_height > 0): r.y = this.height r.height = this.banner_height setcolour(Black) drawtext(r, Center + VCenter, this.banner) def resize_window(this, w, r): this.banner_height = getheight(SystemFont) * 2 + 8 if (r.height < 4 * this.banner_height): # too small for banner this.banner_height = 0 this.height = r.height - this.banner_height this.width = r.width this.change_location() ## create and display the window def init_window(this): this.banner_height = getheight(SystemFont) * 2 + 8 this.win = newwindow("Brian's Theme", rect(0,0,this.width,this.height + this.banner_height), StandardWindow | Centered ) setmouseup(this.win, this.handle_mouse) setkeydown(this.win, this.handle_keydown) setredraw(this.win, this.draw_window) setresize(this.win, this.resize_window) show(this.win) def draw_and_step(this, data): drawto(this.win) setcolour(this.colour) if (this.step_location <= this.height): drawline(this.origin, pt(0, this.height - this.step_location)) drawline(this.origin, pt(this.width, this.step_location)) if (this.step_location <= this.width): drawline(this.origin, pt(this.step_location, 0)) drawline(this.origin, pt(this.width - this.step_location, this.height)) this.step_location = this.step_location + this.step_size if ((this.step_location > this.width+100) and (this.step_location > this.height+100)): this.change_location() redraw(this.win) def main(): initapp(0,0) brian = BriansTheme() brian.init_window() settimerfn(brian.draw_and_step, None) settimer(80) mainloop() main()