From: Rohit <ro...@gm...> - 2005-07-16 11:00:54
|
Hi, I have started using Pythoncard recently, so this may seem trivial. I was going through the samples which come with it and I found the "gravity" sample, which animates a number of balls. I also tried to do something similar to that, but just only for one ball with one color. For this I used one button to start the animation, another button to stop it and a canvas to draw the whole sequence. In the animation sequence the program keeps on checking whether a flag variable has been altered to stop the animation or not. This flag variable was supposed to be altered by the stop animation button, when a mouse click occured. But when I click the stop button the animation keeps on running. I am including the code of my program. [code] import time,random from PythonCard import model class MyBackground(model.Background): def on_initialize(self, event): self.stopAnimation=3DFalse pass def on_startBtn_mouseClick(self,event): event.target.enabled=3DFalse self.stopAnimation=3DFalse xco=3D50. yco=3D0. =20 xspeed=3Drandom.random()*60-30 yspeed=3Drandom.random()*60-30 leftedge=3D0 rightedge,bottomedge=3Dself.components.canvas.size topedge=3D0 gravity=3D2 drag=3D0.98 bounce=3D0.5 radius=3D10 self.components.canvas.fillColor=3D'red' prev=3D() while(not self.stopAnimation): =20 self.components.canvas.drawCircle((xco,yco),radius) time.sleep(0.02) self.components.canvas.clear(); self.components.canvas.refresh(); =20 xco=3Dxco+xspeed if(xco+radius>rightedge): xco=3Drightedge-radius xspeed=3D-xspeed*bounce if(xco-radius<leftedge): xco=3Dleftedge+radius xspeed=3D-xspeed*bounce yco=3Dyco+yspeed if(yco+radius>bottomedge): yco=3Dbottomedge-radius yspeed=3D-yspeed*bounce if(yco-radius<topedge): yco=3Dtopedge+radius yspeed=3Dyspeed*bounce if((int(xco),int(yco))=3D=3Dprev): break =20 yspeed=3Dyspeed*drag+gravity xspeed=3Dxspeed*drag prev=3D(int(xco),int(yco)) =20 def on_stopBtn_mouseClick(self,event): self.stopAnimation=3DTrue self.components.startBtn.enabled=3DTrue =20 =20 if __name__ =3D=3D '__main__': app =3D model.Application(MyBackground) app.MainLoop() [/code] Thanks for any help. Rohit |
From: Alex T. <al...@tw...> - 2005-07-16 13:39:06
|
Rohit wrote: >Hi, >I have started using Pythoncard recently, so this may seem trivial. I >was going through the samples which come with it and I found the >"gravity" sample, which animates a number of balls. >I also tried to do something similar to that, but just only for one >ball with one color. For this I used one button to start the >animation, another button to stop it and a canvas to draw the whole >sequence. In the animation sequence the program keeps on checking >whether a flag variable has been altered to stop the animation or not. >This flag variable was supposed to be altered by the stop animation >button, when a mouse click occured. But when I click the stop button >the animation keeps on running. I am including the code of my program. > > The problem is that your code runs continuously, so there is no opportunity for the click on the "stop" button to be processed, and so self.stopAnimation can't be set. This also means that you can't, for instance, move your window while the animation is running. Easiest fix is to add # give the user a chance to click Stop wx.SafeYield(self, True) inside your loop. This gives an opportunity for other things to happen. A better fix, IMO, is to use timers instead of a loop. You create a time in your initialization code, then when the start button is pressed, you start the timer. Each iteration of drawing is done within the timer handler, and the stop button simply stops the timer. See the 'flock' sample for an example of doing this. There are two reasons I say that this is better is: 1. You can more easily extend it to do more animations, perhaps at different time scales, by having multiple independent timers; the loop-with-yield mechanism is more restrictive. 2. You can do other things in the program while the timers continue to operate - if you do something else within the app during a loop+yield, the animation stops. For instance - I tested this out by creating a simple app with File/Help menus, and adding the buttons and canvas to it. I checked that, indeed, the stop button didn't work; then added the SafeYield() line, and now the stop button works immediately. Now, if I hit start and then the select File menu, the ball moves briefly then disappears while I keep the menu selected; once I release the menu, the ball continues its movement. But in the flock sample, the animation continues while the menu items are selected. -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.323 / Virus Database: 267.8.16/50 - Release Date: 15/07/2005 |
From: Don S. <saw...@st...> - 2005-07-16 20:42:02
|
Rohit, You might set up your code to run each iteration of your animation separately and asynchronously, such as when a timer event executes. The "flock.py" sample shows an example of this method. I also am new to PythonCard, so if any reader of this message can = correct my advice, please do! Don > -----Original Message----- > From: pyt...@li...=20 > [mailto:pyt...@li...] On=20 > Behalf Of Rohit > Sent: Saturday, July 16, 2005 4:01 AM > To: pyt...@li... > Subject: [Pythoncard-users] Stopping an animation sequence. >=20 > Hi, > I have started using Pythoncard recently, so this may seem=20 > trivial. I was going through the samples which come with it=20 > and I found the "gravity" sample, which animates a number of balls. > I also tried to do something similar to that, but just only=20 > for one ball with one color. For this I used one button to=20 > start the animation, another button to stop it and a canvas=20 > to draw the whole sequence. In the animation sequence the=20 > program keeps on checking whether a flag variable has been=20 > altered to stop the animation or not. > This flag variable was supposed to be altered by the stop=20 > animation button, when a mouse click occured. But when I=20 > click the stop button the animation keeps on running. I am=20 > including the code of my program. >=20 > [code] >=20 > import time,random > from PythonCard import model >=20 > class MyBackground(model.Background): >=20 > def on_initialize(self, event): > self.stopAnimation=3DFalse > pass >=20 > def on_startBtn_mouseClick(self,event): > event.target.enabled=3DFalse > self.stopAnimation=3DFalse > xco=3D50. > yco=3D0. > =20 > xspeed=3Drandom.random()*60-30 > yspeed=3Drandom.random()*60-30 > leftedge=3D0 > rightedge,bottomedge=3Dself.components.canvas.size > topedge=3D0 > gravity=3D2 > drag=3D0.98 > bounce=3D0.5 > radius=3D10 > self.components.canvas.fillColor=3D'red' > prev=3D() > while(not self.stopAnimation): > =20 > self.components.canvas.drawCircle((xco,yco),radius) > time.sleep(0.02) > self.components.canvas.clear(); > self.components.canvas.refresh(); > =20 > xco=3Dxco+xspeed > if(xco+radius>rightedge): > xco=3Drightedge-radius > xspeed=3D-xspeed*bounce > if(xco-radius<leftedge): > xco=3Dleftedge+radius > xspeed=3D-xspeed*bounce > yco=3Dyco+yspeed > if(yco+radius>bottomedge): > yco=3Dbottomedge-radius > yspeed=3D-yspeed*bounce > if(yco-radius<topedge): > yco=3Dtopedge+radius > yspeed=3Dyspeed*bounce > if((int(xco),int(yco))=3D=3Dprev): > break =20 > yspeed=3Dyspeed*drag+gravity > xspeed=3Dxspeed*drag > prev=3D(int(xco),int(yco)) > =20 > def on_stopBtn_mouseClick(self,event): > self.stopAnimation=3DTrue > self.components.startBtn.enabled=3DTrue =20 > =20 > if __name__ =3D=3D '__main__': > app =3D model.Application(MyBackground) > app.MainLoop() >=20 >=20 > [/code] >=20 > Thanks for any help. >=20 > Rohit >=20 >=20 > ------------------------------------------------------- > SF.Net email is sponsored by: Discover Easy Linux Migration=20 > Strategies from IBM. Find simple to follow Roadmaps,=20 > straightforward articles, informative Webcasts and more! Get=20 > everything you need to get up to speed, fast.=20 > http://ads.osdn.com/?ad_idt77&alloc_id=16492&op=3Dick > _______________________________________________ > Pythoncard-users mailing list > Pyt...@li... > https://lists.sourceforge.net/lists/listinfo/pythoncard-users >=20 >=20 |