petri_dish
changeset 0:3ce177d96be2
Moving circles,first try
author | Yashina Ksenia <ksenia_yashina@kodomo.fbb.msu.ru> |
---|---|
date | Tue, 07 Dec 2010 18:29:59 +0300 |
parents | |
children | aaa8ab76e38e 31d64bb536aa |
files | tk1.py |
diffstat | 1 files changed, 100 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tk1.py Tue Dec 07 18:29:59 2010 +0300 1.3 @@ -0,0 +1,100 @@ 1.4 +import Tkinter 1.5 +import time 1.6 +from math import * 1.7 +import random 1.8 + 1.9 +class Vector(object): 1.10 + def __init__(self,x,y): 1.11 + self.x=x 1.12 + self.y=y 1.13 + def __abs__(self): 1.14 + return sqrt(self.x**2+self.y**2) 1.15 + def __add__(self,other): 1.16 + return Vector(self.x+other.x,self.y+other.y) 1.17 + def __mul__(self,digit): 1.18 + return Vector(digit*self.x,digit*self.y) 1.19 + def angle(self): 1.20 + if self.x==0: 1.21 + if self.y>0: 1.22 + return -pi/2 1.23 + else: 1.24 + return pi/2 1.25 + if self.y==0: 1.26 + if self.x>0: 1.27 + return 0 1.28 + else: 1.29 + return pi 1.30 + m=abs(self) 1.31 + if acos(self.x/m)>pi/2 or (acos(self.x/m)>pi/2 and asin(self.y/m)<0): 1.32 + return atan(self.y/self.x)-pi 1.33 + else: 1.34 + return atan(self.y/self.x) 1.35 + def angleToCoord(self,angle): 1.36 + magn=abs(self) 1.37 + self.x=magn*cos(angle) 1.38 + self.y=magn*sin(angle) 1.39 + return self 1.40 + def magnitToCoord(self,m): 1.41 + ang=self.angle() 1.42 + self.x=m*cos(ang) 1.43 + self.y=m*sin(ang) 1.44 + return self 1.45 + 1.46 +class Ball(object): 1.47 + def __init__(self,position,velocity): 1.48 + self.position=position 1.49 + self.velocity=velocity 1.50 + def bounds(self): 1.51 + if self.position.x>=595: 1.52 + self.position.x=595 1.53 + self.velocity=self.velocity.angleToCoord(pi-self.velocity.angle()) 1.54 + elif self.position.x<=2: 1.55 + self.position.x=2 1.56 + self.velocity=self.velocity.angleToCoord(pi-self.velocity.angle()) 1.57 + if self.position.y>=595: 1.58 + self.position.y=595 1.59 + self.velocity=self.velocity.angleToCoord(-self.velocity.angle()) 1.60 + elif self.position.y<=2: 1.61 + self.position.y=2 1.62 + self.velocity=self.velocity.angleToCoord(-self.velocity.angle()) 1.63 + def collision(self,other): 1.64 + if abs(self.position.y-other.position.y)<70 and abs(self.position.x-other.position.x)<70: 1.65 + self.velocity.angleToCoord(2*pi*random.random()) 1.66 + other.velocity.angleToCoord(2*pi*random.random()) 1.67 +root = Tkinter.Tk() 1.68 +c = Tkinter.Canvas( root, width = 670, height = 670 ) 1.69 +c.pack() 1.70 + 1.71 +b1=Ball(Vector(440,100),Vector(5,7)) 1.72 +b2=Ball(Vector(40,90),Vector(-3,-1)) 1.73 +b3=Ball(Vector(100,250),Vector(0,5)) 1.74 +b4=Ball(Vector(150,250),Vector(-1,2)) 1.75 +ball1 = c.create_oval(0,0,0,0) 1.76 +ball2 = c.create_oval(0,0,0,0) 1.77 +ball3 = c.create_oval(0,0,0,0) 1.78 +ball4 = c.create_oval(0,0,0,0) 1.79 +while True: 1.80 + time.sleep( 0.02 ) 1.81 + c.delete(ball1) 1.82 + c.delete(ball2) 1.83 + c.delete(ball3) 1.84 + c.delete(ball4) 1.85 + ball1=c.create_oval(b1.position.x, b1.position.y, b1.position.x+75, b1.position.y+75,fill="blue" ) 1.86 + ball2=c.create_oval(b2.position.x, b2.position.y, b2.position.x+75, b2.position.y+75,fill="red") 1.87 + ball3=c.create_oval(b3.position.x, b3.position.y, b3.position.x+75, b3.position.y+75,fill="yellow") 1.88 + ball4=c.create_oval(b4.position.x, b4.position.y, b4.position.x+75, b4.position.y+75,fill="green") 1.89 + b1.bounds() 1.90 + b2.bounds() 1.91 + b3.bounds() 1.92 + b4.bounds() 1.93 + b1.collision(b2) 1.94 + b2.collision(b3) 1.95 + b1.collision(b3) 1.96 + b1.collision(b4) 1.97 + b3.collision(b4) 1.98 + b2.collision(b4) 1.99 + b1.position = b1.position+b1.velocity 1.100 + b2.position = b2.position+b2.velocity 1.101 + b3.position = b3.position+b3.velocity 1.102 + b4.position = b4.position+b4.velocity 1.103 + c.update()