#!/usr/bin/env python # Horserace template import pygtk import gtk import gobject import time import threading from random import * import pango width,height = 900,700 delaytime = 60 # milliseconds delay between animation frames window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Simplified Drawing Example") window.connect("destroy", lambda w: gtk.main_quit()) area = gtk.DrawingArea() area.set_size_request(width,height) window.resize(width,height) window.add(area) window.show_all() window.move(0,0) gc0 = area.window.new_gc() cmap = gc0.get_colormap() # common colors green = cmap.alloc_color(red=0,green=65535,blue=0) blue = cmap.alloc_color(red=0,green=0,blue=65535) red = cmap.alloc_color(red=65535,green=0,blue=0) white = cmap.alloc_color("white") black = cmap.alloc_color("black") gray = cmap.alloc_color("gray") yellow = cmap.alloc_color("yellow") purple = cmap.alloc_color("purple") gc0.set_foreground(green) gc0.set_background(blue) gc0.line_width = 1 # double buffering setup dbuf = gtk.gdk.Pixmap(area.window,width,height,-1) gc1 = dbuf.new_gc() gc1.set_colormap(cmap) ### Thread control syn = threading.Event() # throttles mainloop syn2 = threading.Event() # throttles user thread syn.clear() syn2.clear() stopall = False # for cleanup class MyThread ( threading.Thread ): def __init__(self,brush,gc): self.win = brush self.dgc = gc threading.Thread.__init__(self) def run ( self ): global mydraw mydraw(self.win,self.dgc) eehandler = 0 def area_expose_cb(area, event): global mainloop,eehandler my = MyThread(dbuf,gc1) my.start() # start student thread, which calls mydraw area.handler_block(eehandler) # prevent restart when window moves. mainloop(area.window) # start main animation refresh cycle return True # get ready for expose event eehandler = area.connect("expose_event", area_expose_cb) # animation refresh control loop: def mainloop(brush): if not stopall: syn.wait() # wait for permission to refresh screen syn.clear() # reset for next round brush.draw_drawable(gc0,dbuf,0,0,0,0,width,height) # refresh syn2.set() # informs user thread to goto next iteration eid = gobject.timeout_add(delaytime, mainloop, brush) # reschedule return False # end mainloop def updateDisplay(): syn.set() # release mainloop to refresh screen syn2.wait() # synch with mainloop before looping again syn2.clear() # reset for next round # end updateDisplay def cleanup(): global stopall stopall = True syn.set() syn2.set() # end cleanup ######################## YOUR CODE GOES BELOW ############################ # function draws circle at center x,y, given radius, fill is True or False def drawcircle(brush,gc,x,y,radius,fill): brush.draw_arc(gc,fill,x-radius,y-radius,2*radius,2*radius,0,360*64) # draws text with given font such as "normal bold 24" (see sample call below) def drawtext(brush,gc,x,y,font,text): layout = area.create_pango_layout(text) layout.set_font_description(pango.FontDescription(font)) brush.draw_layout(gc,x,y,layout) # end drawtext def mydraw(brush,gc): # the following 3 lines prepare an animated gif for use in the program: horsean = gtk.gdk.PixbufAnimation("racer.gif") # load animated gif hwidth,hheight = horsean.get_width(), horsean.get_height() #gif size horse = horsean.get_iter() # watch usage below... # main animation loop format x = width/2 # x coordinate of horse y = height/2 # y coordinate of horse while True: # replace with better boolean condition! # erase background: gc.set_foreground(green) # Erase previou image brush.draw_rectangle(gc,True,0,0,width,height) # Draw one horse at x, y coordinate # Draws horse: (x,y are the coordinates of upper-left corner) brush.draw_pixbuf(gc,horse.get_pixbuf(),0,0,x,y) horse.advance() # advance animated gif (move legs of horse) # to change horse's position on screen, change the x (and y) coords: x = x + randint(-5,5) # display text as graphics on screen: gc.set_foreground(red) # text color drawtext(brush,gc,100,height-50,"sans bold 12", "Nice Horsey") updateDisplay() # refresh screen (displays new frame) # end main animation loop cleanup() #end mydraw ########################################################################## # The following line must be the last line in the program gtk.main()