# Skeleton for CSC15 lab 5: change only the mydraw function at the end. import cairo import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, GLib, GObject, Gdk, GdkPixbuf import threading import time import _thread from random import random,randint from math import pi width,height = 1280,768 win = Gtk.Window() drawingarea = Gtk.DrawingArea() win.add(drawingarea) brush = 1 da = 1 delaytime = 500 eehandler = 0 ### Thread control syn = threading.Event() # throttles mainloop syn2 = threading.Event() # throttles user thread syn.clear() # .set allows to go through, .clear blocks syn2.clear() # block stopall = False # for cleanup # old code, Thread permits a while loop inside mydraw class MyThread ( threading.Thread ): def __init__(self,d,b): global brush,da threading.Thread.__init__(self) self.da = d self.br = b brush = b da = d def run ( self ): global mydraw mydraw() #class MyThread # animation refresh control loop: def mainloop(da1, brush1): global da,brush # da = da1 brush = brush1 # print("mainloop") if not stopall: win.present() syn.wait() # wait for permission to refresh screen syn.clear() # reset for next round brush.stroke() syn2.set() # informs user thread to goto next iteration win.queue_draw_area(0,0,width-1,height-1) return False # end mainloop def area_expose_cb(daa, bbrush): global brush, da, eehandler,mainloop brush = bbrush my = MyThread(daa,bbrush) my.start() drawingarea.handler_block(eehandler) eeh = drawingarea.connect('draw', mainloop) # reconnect #win.queue_draw() mainloop(daa,bbrush) # sxxtart main animation refresh cycle return True win.connect('destroy', lambda w: Gtk.main_quit()) win.set_default_size(width,height) eehandler = drawingarea.connect('draw', area_expose_cb) win.show_all() 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 ### some common colors as RGB triples: black = (0,0,0) white=(1,1,1) red = (1,0,0) green = (0,1,0) blue=(0,0,1) yellow = (1,1,0) aqua=(0,1,1) magenta=(1,0,1) gray=(.5,.5,.5) purple=(.5,0.5) ########################################################################## ############################# STUDENT CODE ############################### ########################################################################## ## To affect animation, entire image must be redrawn by calling # updateDisplay() ### Template for Horse Race assignment: code below shows how to draw # background, lines, text, and a single animated gif. Be sure "racer.gif" # is in the same directory as this program. def mydraw(): # sets up loading and preparation of animated gif: gif1 = GdkPixbuf.PixbufAnimation.new_from_file("racer.gif") animator = gif1.get_iter() # call animator.advance() to goto next frame animframe = animator.get_pixbuf() # one animation frame gifwidth = animframe.get_width() # dimensions of gif gifheight = animframe.get_height() yoffset = 60 # leave a little space at the top for messages finish = width-20 # position of finish line hx,hy = 0,height//2+yoffset-gifheight//2 # position of horse stop = False # loop control variable while not(stop): # main animation loop # clear background (draw green background) brush.set_source_rgb(0,1,0) # green brush.paint() # paints entire window green # draw lines representing lane lanewidth = gifheight*2 brush.set_source_rgb(1,1,0) # yellow brush.move_to(0,height//2-lanewidth//2+yoffset) brush.rel_line_to(width-1,0) # relative line to current point brush.move_to(0,height//2+lanewidth//2+yoffset) brush.rel_line_to(width-1,0) brush.stroke() # call stroke before changing color # display text on screen brush.set_source_rgb(0,0,1) # blue font color brush.set_font_size(24) # sets 24pt font size brush.move_to(width//3,yoffset-24) # move to position brush.show_text("Nice Horsey") # display text brush.stroke() # call before changing color # draw horse at position hx,hy animframe = animator.get_pixbuf() Gdk.cairo_set_source_pixbuf(brush,animframe,hx,hy) brush.paint() # paints picture at coordinates animator.advance() # next to next frame of animated gif updateDisplay() # refresh screen image - must call at end of loop time.sleep(0.05) # delay by 50ms #end animation loop #end mydraw #### The following must be the last line of the program: don't move Gtk.main()