# This example draws a green ball. You must click on the canvas to # start it moving. click again to stop it. # Copyright 2005 Boley use strict; use warnings; do 'gdraw.pl'; ## build all the basic objects. my $circle=fill_oval(20,40,60,80,'green'); ## the ball stall(); ## draw it now. my @CanvasDim=get_width(0); ## where's the right wall? my $counter = 0; ## count the iterations my $paused = 1; ## global 'paused' state. sub move_ball { ## move the ball 1 step my @position = get_coords($circle); $position[0] += 2; $position[2] += 2; if ($position[2] >= $CanvasDim[0]) ## check for wrap-around { $position[0]=20; $position[2]=60 }; set_coords($circle,@position); print "\r$counter "; $counter++; stall(); return @position } sub Move_Loop { ## Outer loop to move ball ## The 'Move_Loop' procedure checks the paused state, moves the ball, ### and finally repeats itself after 50 msec. if ($paused) { print "paused\n"; return }; move_ball() ; after(50,\&Move_Loop) }; sub toggle { $paused = not $paused ; Move_Loop() } ## toggle 'paused' state. ## toggle state, then try main task set_binding(0,\&toggle); ## what to do upon mouse click. my $textbox = draw_text('Click here to start and stop',200,300); MainLoop(); ## Tell Tk to start processing.