Advertisement
jeffwincek

Dry wall ants 0.0.1

Nov 6th, 2011
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  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[1]-pos[1]/destination[0]-pos[0])
  18.         self.ydifference = math.sin(angle)*potentialEnergy
  19.         self.xdifference = math.cos(angle)*potentialEnergy
  20.         pos = (round(pos[0]+self.xdifference),round(pos[1]+self.ydifference))
  21.         return
  22.  
  23.     # rturns 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.  
  34.     def sVal(self,where):
  35.         self.x = round(where[0])
  36.         self.y = round(where[1])
  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
  44.     def move_in(self,heading):
  45.         self.ydifference = math.sin(heading)*self.potentialEnergy
  46.         self.xdifference = math.cos(heading)*self.potentialEnergy
  47.         play.draw.line((self.pos,(self.pos[0]+self.xdifference,self.pos[1]+self.ydifference)),width=1)
  48.         self.pos = (round(self.pos[0]+self.xdifference),round(self.pos[1]+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
  55.     def rotate(self,heading):
  56.         heading = heading * 0.017453
  57.         self.angle=heading+self.angle
  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.     def angle_towards_p(self):
  73.         sensing_result = self.sense()
  74.         # the signs are the way they are cause we are going towards darkness (0) away from light (255)
  75.         if sensing_result[0]<sensing_result[1] - 3:
  76.             self.rotate(-35)
  77.         elif sensing_result[0]>sensing_result[1] + 3:
  78.             self.rotate(35)
  79.  
  80.     #def handle_edge(self):
  81.     #    if self.pos[0]>worldSize[0]
  82.        
  83.  
  84. class Player:
  85.     def __init__(self, number_of_ants=6, number_of_turns=2000, worldSize = (512,512)):
  86.         #self.number_of_ants=6
  87.         #self.number_of_turns = 2000
  88.         self.ant_list = []
  89.         #self.worldSize = (512,512)
  90.         self.im = Image.new("L",worldSize,"white")
  91.         self.draw = ImageDraw.Draw(self.im)
  92.         for i in range(0,number_of_ants):
  93.             self.ant_list.append(Ant())
  94.     def play(self):
  95.         for i in range(0,number_of_turns):
  96.             for j in range(0,number_of_ants):
  97.                 self.ant_list[j].wander(15)
  98.             if (i % 50 == 0):
  99.                 for j in range(0,number_of_ants):
  100.                     self.ant_list[j].try_a_significantly_new_angle()
  101.             if (i % 128 == 0):
  102.                 self.im = self.im.filter(ImageFilter.GaussianBlur(radius = 1.5))
  103.  
  104.  
  105.  
  106.  
  107. #print im.getpixel((21,21))
  108. #print math.pi
  109. #draw.line(((0,0),(50,50)),width=2)
  110. #ant = Ant()
  111. play = Player()
  112. play.play()
  113.  
  114.  
  115. play.im.show()
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement