View difference between Paste ID: fTZMG2pv and V3kEstFL
SHOW: | | - or go back to the newest paste.
1
import Image, ImageDraw,math, ImageFilter, random
2
# needs PIL to run
3-
# WIP
3+
# WIP 
4
class Ant:
5
    def __init__(self):
6
        self.pos = (50,50)
7-
        self.pos = (256,256)
7+
8
        self.potentialEnergy = 10.0
9
        # angle is for the current directional heading
10
        self.angle=45.0
11
        # size is the radius of the ant (used for sensing pheromones
12
        self.size = 3
13-
        self.size = 5
13+
        # used for determining when to turn significantly.
14
        # simulates a level of pheromones in an ants nose such that
15
        # if it hasn't smelled any recently, it should turn. 
16
        self.pheromone_levels = 20
17
    #this method does not nessecarily go to the destination
18
    # instead it goes in the direction of the destination
19
    # based off of how much potentialEnergy is left
20
    def head_to(self, destination):
21
        # find a direction to head 
22
        angle = math.tan( destination[1]-pos[1]/destination[0]-pos[0])
23
        self.ydifference = math.sin(angle)*potentialEnergy
24
        self.xdifference = math.cos(angle)*potentialEnergy
25
        pos = (round(pos[0]+self.xdifference),round(pos[1]+self.ydifference))
26
        return
27
28
    # returns a touple of values at the sensor locations (left,right)
29
    def sense(self):
30
        # set up the sensors as 15 degrees off of the angle at a given distance
31
        self.Langle = self.angle + math.pi/12.0
32
        self.Rangle = self.angle - math.pi/12.0
33
        self.Lpos = (math.cos(self.Langle)*self.size,math.sin(self.Langle)*self.size)
34
        self.Rpos = (math.cos(self.Rangle)*self.size,math.sin(self.Rangle)*self.size)
35
        self.leftResult = self.sVal(self.Lpos)
36
        self.rightResult = self.sVal(self.Rpos)
37
        self.pheromone_levels += self.leftResult + self.rightResult
38
        return (self.leftResult,self.rightResult)
39
    # used in the above method, to return a decent value from a location
40
    def sVal(self,where):
41
        self.x = round(where[0])
42
        self.y = round(where[1])
43
        try:
44
            return play.im.getpixel((self.x,self.y))
45
        except:
46
            return 255
47
        
48
    #similar to head_to, but instead moves in the direction of the current angle
49
    # uses potentialEnergy to determine distance 
50
    def move_in(self,heading):
51
        self.ydifference = math.sin(heading)*self.potentialEnergy
52
        self.xdifference = math.cos(heading)*self.potentialEnergy
53
        playing.draw.line((self.pos,(self.pos[0]+self.xdifference,self.pos[1]+self.ydifference)),width=1)
54
        self.pos = (round(self.pos[0]+self.xdifference),round(self.pos[1]+self.ydifference))
55
        return
56
    def blur(self,howMuch):
57
        for i in int(howMuch):
58
            print "blurred in ant class"
59
            im = im.filter(ImageFilter.BLUR)
60
61
    # works in degrees
62-
        self.angle_towards_p()
62+
63
        heading = heading * 0.017453
64-
            self.rotate(random.randint(-20,20))
64+
65
    
66
    def wander(self, num_steps):
67
        
68
        for i in range(0,num_steps):
69
            self.angle_towards_p()
70
            if self.pheromone_levels <= 10:
71
                self.try_a_significantly_new_angle()
72-
        self.rotate(random.randint(-45,45))
72+
            elif i % 3 == 0 and i != 0:
73
                self.rotate(random.randint(-20,20))
74
            self.move_in(self.angle)
75
            self.pheromone_levels = self.pheromone_levels / 1.3
76
77
    def turn_this_ant_around(self):
78
        # right this minute, and I mean it this time
79
        self.angle = self.angle+math.pi
80
81
    def try_a_significantly_new_angle(self):
82-
            self.rotate(-35)
82+
        self.rotate(random.randint(-120,120))
83
84
    # this function steers the ant towards the pheromone trail
85
    # currently it is a binary steering operation
86
    # see if elif state ment, have the ant turn based on a gradient
87
    # might work much better. 
88
    def angle_towards_p(self):
89
        sensing_result = self.sense()
90
        # the signs are the way they are cause we are going towards darkness (0) away from light (255)
91
        if sensing_result[0]<sensing_result[1] - 3:
92
            self.rotate(self.how_much_to_turn(sensing_result))
93-
        self.number_of_turns = 5
93+
94
            self.rotate(35)
95
96
    #def handle_edge(self):
97
    #    if self.pos[0]>worldSize[0]
98
        
99
100
class Player:
101
    def __init__(self):
102
        self.number_of_ants=25
103-
            # it seems to be triggering to often. 
103+
        self.number_of_turns = 200
104-
            if self.NT % 6 == 0:
104+
        self.number_of_food = 10
105-
                self.im.show("pre-blur")
105+
106
        self.food_list = []
107
        self.food_size = 5
108-
                self.ant_list[j].wander(16)
108+
109
        self.im = Image.new("L",self.worldSize,"white")
110
        self.draw = ImageDraw.Draw(self.im)
111
        for k in range(0,self.number_of_ants):
112
            self.ant_list.append(Ant())
113
        for f in range(0,self.number_of_food):
114
            self.food_list.append((random.randint(0,self.worldSize[0]),random.randint(0,self.worldSize[0])))
115
        self.food_list.sort()
116
        print self.number_of_turns
117
    def play(self):
118
        for self.NT in range(0,int(self.number_of_turns)):
119
            # there is something weird about this modulo behavior
120
            # it seems to be triggering to often.
121
            self.drawFood()
122
            for j in range(0,self.number_of_ants):
123-
playing.im.show()
123+
                #print "wander"
124
                self.ant_list[j].wander(6)
125
                if self.NT % 50 == 0:
126
                    self.ant_list[j].try_a_significantly_new_angle()
127
            if self.NT % 16 == 0 and self.NT != 0:
128
                print "blurred"
129
                #self.im.show("pre-blur")
130
                self.im = self.im.filter(ImageFilter.GaussianBlur(radius = 2.0))
131
    def drawFood(self):
132
        #print "draw food"
133
        for fo in range(0,len(self.food_list)):
134
            other_side = (self.food_list[fo][0]+self.food_size,self.food_list[fo][1]+self.food_size)
135
            self.draw.ellipse((self.food_list[fo],other_side),fill="black")
136
#print im.getpixel((21,21))
137
#print math.pi
138
#draw.line(((0,0),(50,50)),width=2)
139
#ant = Ant()
140
playing = Player()
141
playing.play()
142
143
144
playing.im.show()
145
print playing.food_list
146
147