#!/usr/local/bin/python ## # Brian's Theme # ------------- # 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. # It also uses a lot of global variables, which is not really # the "Python way", but I've left this the same as in the # C language version, for comparison. ## from graphapp import * import os import sys import string import time import random ## # Global variables. ## width = 400 height = 240 banner_height = 40 banner = "0-9 to change the step size, Q to stop, R,G,B,C,M,Y,K for colours." win = None step_size = 3 step_location = 0 origin = pt(0,0) colour = Blue ## # Functions. ## def rand(max): return int(random.random() * max) def change_location(): global origin, width, height, step_location origin.x = rand(width) origin.y = rand(height) step_location = 0 def handle_mouse(w, buttons, xy): global origin, step_location, win origin = xy step_location = 0 redraw(win) def handle_keydown(w, key): global step_size, colour if (key in string.digits): step_size = ord(key) - ord('0') elif ((key == 'q') or (key == 'Q') or (key == ESC)): exitapp() else: key = string.lower(key) if (key == 'r'): colour = Red elif (key == 'g'): colour = Green elif (key == 'b'): colour = Blue elif (key == 'c'): colour = Cyan elif (key == 'm'): colour = Magenta elif (key == 'y'): colour = Yellow elif (key == 'k'): colour = Black def draw_window(w, r): global banner_height, height, banner if (banner_height > 0): r.y = height r.height = banner_height setcolour(Black) drawtext(r, Center + VCenter, banner) def resize_window(w, r): global banner_height, width, height banner_height = 2*getheight(SystemFont)+8 if (r.height < 4*banner_height): # too small for banner banner_height = 0 height = r.height - banner_height width = r.width change_location() ## create and display the window def init_window(): global win, banner_height, width, height banner_height = getheight(SystemFont)*2+8 win = newwindow("Brian's Theme", rect(0,0,width,height+banner_height), StandardWindow | Centered ) setmouseup(win, handle_mouse) setkeydown(win, handle_keydown) setredraw(win, draw_window) setresize(win, resize_window) show(win) def draw_and_step(data): global win, colour, origin, step_location, step_size, width, height drawto(win) setcolour(colour) if (step_location <= height): drawline(origin, pt(0, height-step_location)) drawline(origin, pt(width, step_location)) if (step_location <= width): drawline(origin, pt(step_location, 0)) drawline(origin, pt(width-step_location, height)) step_location = step_location + step_size if ((step_location > width+100) and (step_location > height+100)): change_location() redraw(win) def main(): initapp(0,0) init_window() settimerfn(draw_and_step, None) settimer(80) mainloop() main()