Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.ru/hg/petri_dish/file/02609649d88b/tk1.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 05:52:50 2013
Кодировка:

Поисковые слова: m 2
petri_dish: 02609649d88b tk1.py

petri_dish

view tk1.py @ 2:02609649d88b

Bacteria.py module 0.1
author Smirnova Victoria
date Tue, 07 Dec 2010 21:26:34 +0300
parents
children
line source
1 import Tkinter
2 import time
3 from math import *
4 import random
6 class Vector(object):
7 def __init__(self,x,y):
8 self.x=x
9 self.y=y
10 def __abs__(self):
11 return sqrt(self.x**2+self.y**2)
12 def __add__(self,other):
13 return Vector(self.x+other.x,self.y+other.y)
14 def __mul__(self,digit):
15 return Vector(digit*self.x,digit*self.y)
16 def angle(self):
17 if self.x==0:
18 if self.y>0:
19 return -pi/2
20 else:
21 return pi/2
22 if self.y==0:
23 if self.x>0:
24 return 0
25 else:
26 return pi
27 m=abs(self)
28 if acos(self.x/m)>pi/2 or (acos(self.x/m)>pi/2 and asin(self.y/m)<0):
29 return atan(self.y/self.x)-pi
30 else:
31 return atan(self.y/self.x)
32 def angleToCoord(self,angle):
33 magn=abs(self)
34 self.x=magn*cos(angle)
35 self.y=magn*sin(angle)
36 return self
37 def magnitToCoord(self,m):
38 ang=self.angle()
39 self.x=m*cos(ang)
40 self.y=m*sin(ang)
41 return self
43 class Ball(object):
44 def __init__(self,position,velocity):
45 self.position=position
46 self.velocity=velocity
47 def bounds(self):
48 if self.position.x>=595:
49 self.position.x=595
50 self.velocity=self.velocity.angleToCoord(pi-self.velocity.angle())
51 elif self.position.x<=2:
52 self.position.x=2
53 self.velocity=self.velocity.angleToCoord(pi-self.velocity.angle())
54 if self.position.y>=595:
55 self.position.y=595
56 self.velocity=self.velocity.angleToCoord(-self.velocity.angle())
57 elif self.position.y<=2:
58 self.position.y=2
59 self.velocity=self.velocity.angleToCoord(-self.velocity.angle())
60 def collision(self,other):
61 if abs(self.position.y-other.position.y)<70 and abs(self.position.x-other.position.x)<70:
62 self.velocity.angleToCoord(2*pi*random.random())
63 other.velocity.angleToCoord(2*pi*random.random())
64 root = Tkinter.Tk()
65 c = Tkinter.Canvas( root, width = 670, height = 670 )
66 c.pack()
68 b1=Ball(Vector(440,100),Vector(5,7))
69 b2=Ball(Vector(40,90),Vector(-3,-1))
70 b3=Ball(Vector(100,250),Vector(0,5))
71 b4=Ball(Vector(150,250),Vector(-1,2))
72 ball1 = c.create_oval(0,0,0,0)
73 ball2 = c.create_oval(0,0,0,0)
74 ball3 = c.create_oval(0,0,0,0)
75 ball4 = c.create_oval(0,0,0,0)
76 while True:
77 time.sleep( 0.02 )
78 c.delete(ball1)
79 c.delete(ball2)
80 c.delete(ball3)
81 c.delete(ball4)
82 ball1=c.create_oval(b1.position.x, b1.position.y, b1.position.x+75, b1.position.y+75,fill="blue" )
83 ball2=c.create_oval(b2.position.x, b2.position.y, b2.position.x+75, b2.position.y+75,fill="red")
84 ball3=c.create_oval(b3.position.x, b3.position.y, b3.position.x+75, b3.position.y+75,fill="yellow")
85 ball4=c.create_oval(b4.position.x, b4.position.y, b4.position.x+75