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 |