• API
• FAQ
• Tools
• Archive
SHARE
TWEET Dry wall ants 0.0.2 jeffwincek  Nov 6th, 2011 13 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import Image, ImageDraw,math, ImageFilter, random
2.
3. class Ant:
4.     def __init__(self):
5.         self.pos = (256,256)
6.         # potentialEnergy is used to calculate how far it should move
7.         self.potentialEnergy = 10.0
8.         # angle is for the current directional heading
9.         self.angle=45.0
10.         # size is the radius of the ant (used for sensing pheromones
11.         self.size = 5
12.     #this method does not nessecarily go to the destination
13.     # instead it goes in the direction of the destination
14.     # based off of how much potentialEnergy is left
15.     def head_to(self, destination):
16.         # find a direction to head
17.         angle = math.tan( destination-pos/destination-pos)
18.         self.ydifference = math.sin(angle)*potentialEnergy
19.         self.xdifference = math.cos(angle)*potentialEnergy
20.         pos = (round(pos+self.xdifference),round(pos+self.ydifference))
21.         return
22.
23.     # returns a touple of values at the sensor locations (left,right)
24.     def sense(self):
25.         # set up the sensors as 15 degrees off of the angle at a given distance
26.         self.Langle = self.angle + math.pi/12.0
27.         self.Rangle = self.angle - math.pi/12.0
28.         self.Lpos = (math.cos(self.Langle)*self.size,math.sin(self.Langle)*self.size)
29.         self.Rpos = (math.cos(self.Rangle)*self.size,math.sin(self.Rangle)*self.size)
30.         self.leftResult = self.sVal(self.Lpos)
31.         self.rightResult = self.sVal(self.Rpos)
32.         return (self.leftResult,self.rightResult)
33.     # used in the above method, to return a decent value from a location
34.     def sVal(self,where):
35.         self.x = round(where)
36.         self.y = round(where)
37.         try:
38.             return play.im.getpixel((self.x,self.y))
39.         except:
40.             return 255
41.
42.     #similar to head_to, but instead moves in the direction of the current angle
43.     # uses potentialEnergy to determine distance
45.         self.ydifference = math.sin(heading)*self.potentialEnergy
46.         self.xdifference = math.cos(heading)*self.potentialEnergy
47.         playing.draw.line((self.pos,(self.pos+self.xdifference,self.pos+self.ydifference)),width=1)
48.         self.pos = (round(self.pos+self.xdifference),round(self.pos+self.ydifference))
49.         return
50.     def blur(self,howMuch):
51.         for i in int(howMuch):
52.             im = im.filter(ImageFilter.BLUR)
53.
54.     # works in degrees
58.
59.     def wander(self, num_steps):
60.         self.angle_towards_p()
61.         for i in range(0,num_steps):
62.             self.rotate(random.randint(-20,20))
63.             self.move_in(self.angle)
64.
65.     def turn_this_ant_around(self):
66.         # right this minute, and I mean it this time
67.         self.angle = self.angle+math.pi
68.
69.     def try_a_significantly_new_angle(self):
70.         self.rotate(random.randint(-45,45))
71.
72.     # this function steers the ant towards the pheromone trail
73.     # currently it is a binary steering operation
74.     # see if elif state ment, have the ant turn based on a gradient
75.     # might work much better.
76.     def angle_towards_p(self):
77.         sensing_result = self.sense()
78.         # the signs are the way they are cause we are going towards darkness (0) away from light (255)
79.         if sensing_result<sensing_result - 3:
80.             self.rotate(-35)
81.         elif sensing_result>sensing_result + 3:
82.             self.rotate(35)
83.
84.     #def handle_edge(self):
85.     #    if self.pos>worldSize
86.
87.
88. class Player:
89.     def __init__(self):
90.         self.number_of_ants=25
91.         self.number_of_turns = 5
92.         self.ant_list = []
93.         self.worldSize = (512,512)
94.         self.im = Image.new("L",self.worldSize,"white")
95.         self.draw = ImageDraw.Draw(self.im)
96.         for k in range(0,self.number_of_ants):
97.             self.ant_list.append(Ant())
98.     def play(self):
99.         for self.NT in range(0,int(self.number_of_turns)):
100.             # there is something weird about this modulo behavior
101.             # it seems to be triggering to often.
102.             if self.NT % 6 == 0:
103.                 self.im.show("pre-blur")
104.                 self.im = self.im.filter(ImageFilter.GaussianBlur(radius = 2.0))
105.             for j in range(0,self.number_of_ants):
106.                 self.ant_list[j].wander(16)
107.                 if self.NT % 50 == 0:
108.                     self.ant_list[j].try_a_significantly_new_angle()
109.
110.
111.
112.
113. #print im.getpixel((21,21))
114. #print math.pi
115. #draw.line(((0,0),(50,50)),width=2)
116. #ant = Ant()
117. playing = Player()
118. playing.play()
119.
120.
121. playing.im.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top