[Tutor] A range of numbers

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Dec 1 20:37:42 EST 2003



On Mon, 1 Dec 2003, Cris Leung wrote:

> Okay, I was working on it just now, and I found out that this works:
> __________________________________________________________________
> def func():
> 	begin_graphics()
> 	begin_mouse()
> 	box(200,200,300,300)
> 	for i in range(100):
> 		print 'delay'
> 	while 1:
> 		x,y = mouse_position()
> 		if 200 <= x <= 300 and 200 <= y <= 300 and mouse_buttons()['left'] == 1:
> 			break
> 	box(100,100,400,400)
> 	end_mouse()
> __________________________________________________________
>
> as long as I move my mouse to the graphics window before the "print
> delay"  is finish. However, if I dun click within seconds after the
> "print 'delay'"  , the thing freezes.


Ah!

Try moving the begin_mouse() and end_mouse() stuff around the
mouse_position().  What's happening is that begin_mouse() and end_mouse()
are the function that update the mouse position in the program. Without
them, mouse_position() will continue to return the same thing over and
over.


###
def func():
    begin_graphics()
    box(200,200,300,300)
    while 1:
        begin_mouse()
        x,y = mouse_position()
        end_mouse()
        if (200 <= x <= 300
            and 200 <= y <= 300
            and mouse_buttons()['left'] == 1):
            break
    box(100,100,400,400)
###

should fix the bug.


This is not an ideal fix --- in programming lingo, this is a "busy
waiting" loop that continues to ask the system what the mouse position is,
even if it hasn't changed recently.  So it's a bit inefficient.  Still, it
should be ok enough to make things work.


(But to tell the truth, I don't know why the Livewires API is designed
that way; it seems very error prone!  I think it might simply be better to
have mouse_position() automatically call begin_mouse() and end_mouse() for
us.  I'd have to look more closely at the Livewires API to see why they
don't do that.)


Good luck!




More information about the Tutor mailing list