Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Image, ImageDraw,math, ImageFilter, random
- # needs PIL to run
- # WIP
- class Ant:
- def __init__(self):
- self.pos = (256,256)
- # potentialEnergy is used to calculate how far it should move
- self.potentialEnergy = 10.0
- # angle is for the current directional heading
- self.angle=45.0
- # size is the radius of the ant (used for sensing pheromones
- self.size = 5
- #this method does not nessecarily go to the destination
- # instead it goes in the direction of the destination
- # based off of how much potentialEnergy is left
- def head_to(self, destination):
- # find a direction to head
- angle = math.tan( destination[1]-pos[1]/destination[0]-pos[0])
- self.ydifference = math.sin(angle)*potentialEnergy
- self.xdifference = math.cos(angle)*potentialEnergy
- pos = (round(pos[0]+self.xdifference),round(pos[1]+self.ydifference))
- return
- # returns a touple of values at the sensor locations (left,right)
- def sense(self):
- # set up the sensors as 15 degrees off of the angle at a given distance
- self.Langle = self.angle + math.pi/12.0
- self.Rangle = self.angle - math.pi/12.0
- self.Lpos = (math.cos(self.Langle)*self.size,math.sin(self.Langle)*self.size)
- self.Rpos = (math.cos(self.Rangle)*self.size,math.sin(self.Rangle)*self.size)
- self.leftResult = self.sVal(self.Lpos)
- self.rightResult = self.sVal(self.Rpos)
- return (self.leftResult,self.rightResult)
- # used in the above method, to return a decent value from a location
- def sVal(self,where):
- self.x = round(where[0])
- self.y = round(where[1])
- try:
- return play.im.getpixel((self.x,self.y))
- except:
- return 255
- #similar to head_to, but instead moves in the direction of the current angle
- # uses potentialEnergy to determine distance
- def move_in(self,heading):
- self.ydifference = math.sin(heading)*self.potentialEnergy
- self.xdifference = math.cos(heading)*self.potentialEnergy
- playing.draw.line((self.pos,(self.pos[0]+self.xdifference,self.pos[1]+self.ydifference)),width=1)
- self.pos = (round(self.pos[0]+self.xdifference),round(self.pos[1]+self.ydifference))
- return
- def blur(self,howMuch):
- for i in int(howMuch):
- im = im.filter(ImageFilter.BLUR)
- # works in degrees
- def rotate(self,heading):
- heading = heading * 0.017453
- self.angle=heading+self.angle
- def wander(self, num_steps):
- self.angle_towards_p()
- for i in range(0,num_steps):
- self.rotate(random.randint(-20,20))
- self.move_in(self.angle)
- def turn_this_ant_around(self):
- # right this minute, and I mean it this time
- self.angle = self.angle+math.pi
- def try_a_significantly_new_angle(self):
- self.rotate(random.randint(-45,45))
- # this function steers the ant towards the pheromone trail
- # currently it is a binary steering operation
- # see if elif state ment, have the ant turn based on a gradient
- # might work much better.
- def angle_towards_p(self):
- sensing_result = self.sense()
- # the signs are the way they are cause we are going towards darkness (0) away from light (255)
- if sensing_result[0]<sensing_result[1] - 3:
- self.rotate(-35)
- elif sensing_result[0]>sensing_result[1] + 3:
- self.rotate(35)
- #def handle_edge(self):
- # if self.pos[0]>worldSize[0]
- class Player:
- def __init__(self):
- self.number_of_ants=25
- self.number_of_turns = 5
- self.ant_list = []
- self.worldSize = (512,512)
- self.im = Image.new("L",self.worldSize,"white")
- self.draw = ImageDraw.Draw(self.im)
- for k in range(0,self.number_of_ants):
- self.ant_list.append(Ant())
- def play(self):
- for self.NT in range(0,int(self.number_of_turns)):
- # there is something weird about this modulo behavior
- # it seems to be triggering to often.
- if self.NT % 6 == 0:
- self.im.show("pre-blur")
- self.im = self.im.filter(ImageFilter.GaussianBlur(radius = 2.0))
- for j in range(0,self.number_of_ants):
- self.ant_list[j].wander(16)
- if self.NT % 50 == 0:
- self.ant_list[j].try_a_significantly_new_angle()
- #print im.getpixel((21,21))
- #print math.pi
- #draw.line(((0,0),(50,50)),width=2)
- #ant = Ant()
- playing = Player()
- playing.play()
- playing.im.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement