def ia(self,x,y): if self.f: while True: d = randint(0,3) dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[d] if d != (3,2,1,0)[self.d] and self.espace(dx,dy): self.d = d break else: distances = [9999 for _ in range(4)] for i in range(4): if i != (3,2,1,0)[self.d]: dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[i] if self.espace(dx,dy): distances[i] = sqrt((self.y + dy - y)**2 + (self.x + dx - x)**2) self.d = distances.index(min(distances))