Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.ru/hg/petri_dish/raw-rev/3ce177d96be2
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 18:37:23 2012
Кодировка:

Поисковые слова: m 2

# HG changeset patch
# User Yashina Ksenia
# Date 1291735799 -10800
# Node ID 3ce177d96be29a6519156b019f72466c43cf4a40

Moving circles,first try

diff -r 000000000000 -r 3ce177d96be2 tk1.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tk1.py Tue Dec 07 18:29:59 2010 +0300
@@ -0,0 +1,100 @@
+import Tkinter
+import time
+from math import *
+import random
+
+class Vector(object):
+ def __init__(self,x,y):
+ self.x=x
+ self.y=y
+ def __abs__(self):
+ return sqrt(self.x**2+self.y**2)
+ def __add__(self,other):
+ return Vector(self.x+other.x,self.y+other.y)
+ def __mul__(self,digit):
+ return Vector(digit*self.x,digit*self.y)
+ def angle(self):
+ if self.x==0:
+ if self.y>0:
+ return -pi/2
+ else:
+ return pi/2
+ if self.y==0:
+ if self.x>0:
+ return 0
+ else:
+ return pi
+ m=abs(self)
+ if acos(self.x/m)>pi/2 or (acos(self.x/m)>pi/2 and asin(self.y/m)<0):
+ return atan(self.y/self.x)-pi
+ else:
+ return atan(self.y/self.x)
+ def angleToCoord(self,angle):
+ magn=abs(self)
+ self.x=magn*cos(angle)
+ self.y=magn*sin(angle)
+ return self
+ def magnitToCoord(self,m):
+ ang=self.angle()
+ self.x=m*cos(ang)
+ self.y=m*sin(ang)
+ return self
+
+class Ball(object):
+ def __init__(self,position,velocity):
+ self.position=position
+ self.velocity=velocity
+ def bounds(self):
+ if self.position.x>=595:
+ self.position.x=595
+ self.velocity=self.velocity.angleToCoord(pi-self.velocity.angle())
+ elif self.position.x<=2:
+ self.position.x=2
+ self.velocity=self.velocity.angleToCoord(pi-self.velocity.angle())
+ if self.position.y>=595:
+ self.position.y=595
+ self.velocity=self.velocity.angleToCoord(-self.velocity.angle())
+ elif self.position.y<=2:
+ self.position.y=2
+ self.velocity=self.velocity.angleToCoord(-self.velocity.angle())
+ def collision(self,other):
+ if abs(self.position.y-other.position.y)<70 and abs(self.position.x-other.position.x)<70:
+ self.velocity.angleToCoord(2*pi*random.random())
+ other.velocity.angleToCoord(2*pi*random.random())
+root = Tkinter.Tk()
+c = Tkinter.Canvas( root, width = 670, height = 670 )
+c.pack()
+
+b1=Ball(Vector(440,100),Vector(5,7))
+b2=Ball(Vector(40,90),Vector(-3,-1))
+b3=Ball(Vector(100,250),Vector(0,5))
+b4=Ball(Vector(150,250),Vector(-1,2))
+ball1 = c.create_oval(0,0,0,0)
+ball2 = c.create_oval(0,0,0,0)
+ball3 = c.create_oval(0,0,0,0)
+ball4 = c.create_oval(0,0,0,0)
+while True:
+ time.sleep( 0.02 )
+ c.delete(ball1)
+ c.delete(ball2)
+ c.delete(ball3)
+ c.delete(ball4)
+ ball1=c.create_oval(b1.position.x, b1.position.y, b1.position.x+75, b1.position.y+75,fill="blue" )
+ ball2=c.create_oval(b2.position.x, b2.position.y, b2.position.x+75, b2.position.y+75,fill="red")
+ ball3=c.create_oval(b3.position.x, b3.position.y, b3.position.x+75, b3.position.y+75,fill="yellow")
+ ball4=c.create_oval(b4.position.x, b4.position.y, b4.position.x+75, b4.position.y+75,fill="green")
+ b1.bounds()
+ b2.bounds()
+ b3.bounds()
+ b4.bounds()
+ b1.collision(b2)
+ b2.collision(b3)
+ b1.collision(b3)
+ b1.collision(b4)
+ b3.collision(b4)
+ b2.collision(b4)
+ b1.position = b1.position+b1.velocity
+ b2.position = b2.position+b2.velocity
+ b3.position = b3.position+b3.velocity
+ b4.position = b4.position+b4.velocity
+ c.update()