def obstacleAvoidance(self, nPixels, intPixels, states): a = arange(0, nPixels, intPixels) vAux = Vector2D(self.velocity.x, self.velocity.y) vAux.normalize() xBase = vAux.x yBase = vAux.y for i in a: patch = self.world.terrain.getPatch( int(self.location.x + i * xBase), int(self.location.y + i * yBase)) if patch.targetState in states: break if i < a[-1]: v2 = Vector2D(-vAux.y, vAux.x) v2.mul(0.5) v2.add(vAux) v2.normalize() xBase2 = v2.x yBase2 = v2.y patch = self.world.terrain.getPatch( int(self.location.x + i * xBase2), int(self.location.y + i * yBase2)) if patch.targetState in states: vector = Vector2D(self.velocity.y, -self.velocity.x) else: vector = Vector2D(-self.velocity.y, self.velocity.x) vector.normalize() vector.mul( max((1. - (1. * i / nPixels)) * self.maxForce * 2, self.maxForce)) return vector else: return Vector2D(0, 0)
def draw(self, display): print 'Pv.draw() ' super(self.__class__, self).draw(display) #display.led.draw_text2(0, 0, 'PAINEL > CORRENTE', 1) #display.setString('8,88 A ', arial_16, Align.RIGHT_BOTTOM) r = 8 x1 = 0 y1 = 30 x2 = x1 + self.wid y2 = y1 - self.hei p1 = Vector2D(x1, y1) p2 = Vector2D(x2, y1) p3 = Vector2D(x2 + r, y2) p4 = Vector2D(x1 + r, y2) display.drawLine(p1.x, p1.y, p2.x, p2.y) display.drawLine(p2.x, p2.y, p3.x, p3.y) display.drawLine(p3.x, p3.y, p4.x, p4.y) display.drawLine(p4.x, p4.y, p1.x, p1.y) x = p1.x + (p4.x - p1.x) / 2 y = p1.y + (p4.y - p1.y) / 2 w = p2.x + (p3.x - p2.x) / 2 z = p2.y + (p3.y - p2.y) / 2 display.drawLine(int(x), int(y), int(w), int(z)) xt = p1.x + (p2.x - p1.x) / 2 xw = p4.x + (p3.x - p4.x) / 2 display.drawLine(int(xt), int(p1.y), int(xw), int(p4.y))
def decidePrey(self, racesToEscape, statesToEat): if self.energy < self.ungry_Limit and self.targetPatch == None: (col, line) = self.world.terrain.getPatchCoor(self.location.x, self.location.y) for (x, y) in FOOD_SEARCH_NEIGH: col += x line += y if col < 0 or line < 0 or col >= self.world.terrain.ncols or line >= self.world.terrain.nlines: continue patch = self.world.terrain.patches[col][line] if patch.state in statesToEat and ENERGY_FROM_FOOD[ patch.state] > 0: self.targetPatch = patch break if self.energy < self.ungry_Limit and self.targetPatch != None: f = Vector2D(0, 0) if ENERGY_FROM_FOOD[self.targetPatch.state] == 0: self.targetPatch = None else: f = self.arriving( Vector2D( (self.targetPatch.pxcor * self.targetPatch.w) + 10, (self.targetPatch.pycor * self.targetPatch.h) + 10)) else: f = self.escape(racesToEscape) f2 = self.wandering() fObst = self.obstacleAvoidance(40, 10, array([6])) fObst.mul(10) f.add(fObst) f.add(f2) return f
def drawLine(self, x1, y1, x2, y2): vector1 = Vector2D(x1, y1) vector2 = Vector2D(x2, y2) pointList = vector1.bresenham(vector2) for p in pointList: self.led.draw_pixel(p[0], p[1])
def setUp(self): self.x = Vector2D(1, 0) self.y = Vector2D(0, 1) self.v1 = Vector2D(1, 1) self.v2 = Vector2D(1, 0) self.identity = Matrix(1, 0, 0, 1) self.matrix1 = Matrix(1, 1, 1, 1) self.matrix2 = Matrix(0, 1, 0, 1)
def __init__(self, screensize, x, y, life): self.position = Vector2D(x, y) self.positionShot = Vector2D(self.getPositionX(), self.getPositionY()) self.life = life self.sprite = pygame.image.load("../imagens/player/ship.png") self.shotY = 0 self.shotBool = False self.velocity = 10 self.colorShot = (0, 255, 127)
def drawRectangleAngle(self, x, y, w, h, a): p1 = Vector2D(x, y) p2 = Vector2D(x+w, y) p4 = Vector2D(x+g, y-h) p3 = Vector2D(x+g + w, y-h) display.drawLine(p1.x, p1.y, p2.x, p2.y) display.drawLine(p2.x, p2.y, p3.x, p3.y) display.drawLine(p3.x, p3.y, p4.x, p4.y) display.drawLine(p4.x, p4.y, p1.x, p1.y)
def drawRectangle(self, x1, y1, width, height): x2 = x1 + width y2 = y1 + height pointA = Vector2D(x1, y1) pointB = Vector2D(x2, y1) pointC = Vector2D(x2, y2) pointD = Vector2D(x1, y2) pointAll = [ [ pointA, pointB ], [ pointB, pointC ], [ pointC, pointD ], [ pointD, pointA ] ] for point in pointAll: self.drawLine(point[0].x, point[0].y, point[1].x, point[1].y)
def __init__(self): self.position = Vector2D() self.start_position = Vector2D() self.last_position = Vector2D() self.velocity = Vector2D() self.max_velocity = Vector2D() self.orientation = 0 self.collision_radius = 1 self.start_health = 1 self.health = 1 self.operations = {} self.invulnerable = False self.alive = True
def definePointXY(self, align, width, font, one): cols = 'cols' rows = 'rows' posX = 0 posY = 0 if(width >= self.cols): return None if (align == Align.MODAL): posX = self.center(width, cols) posY = self.center(font.char_height, rows) elif (align == Align.RIGHT_CENTER): posX = (width - self.cols) * -one posY = self.center(font.char_height, rows) elif (align == Align.RIGHT_BOTTOM): posX = (width - self.cols) * -one posY = (self.rows - font.char_height) + one elif (align == Align.CENTER_BOTTOM): posX = self.center(width, cols) posY = (self.rows - font.char_height) + one else: return None return Vector2D(posX, posY)
def __init__(self, x=0, y=0, wid=0, hei=0, margin=0): print 'Component.__init__' self.point = Vector2D(x, y) self.wid = wid self.hei = hei self.margin = margin self.border = 0
def circle(radius=1, resolution=100): points = [Vector2D(radius, 0)] rotation_matrix = Matrix.rotation_matrix(2 * pi / resolution) for angle in range(1, resolution): new_point = rotation_matrix.dot(points[-1]) points.append(new_point) return Polygon(*points)
def decidePredator(self, racesToEscape, racesToEat): if self.energy < self.ungry_Limit and self.targetPrey == None: (col, line) = self.world.terrain.getPatchCoor(self.location.x, self.location.y) for (x, y) in FOOD_SEARCH_NEIGH: col += x line += y if col < 0 or line < 0 or col >= self.world.terrain.ncols or line >= self.world.terrain.nlines: continue patch = self.world.terrain.patches[col][line] preys = patch.whichAnimalsAreOn(self.world.animals.values(), racesToEat) if any(preys): self.targetPrey = preys[0].id break if self.energy < self.ungry_Limit and self.targetPrey != None: try: f = Vector2D(0, 0) prey = self.world.animals[self.targetPrey] f = self.pursuit(prey) except KeyError: self.targetPrey = None else: f = self.escape(racesToEscape) f2 = self.wandering() fObst = self.obstacleAvoidance(40, 10, array([6])) fObst.mul(10) f.add(fObst) f.add(f2) return f
def intersection(self, line): if self.is_parallel(line): return None x1 = self.start.x y1 = self.start.y x2 = self.end.x y2 = self.end.y x3 = line.start.x y3 = line.start.y x4 = line.end.x y4 = line.end.y denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) x = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4) x = x / denominator y = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4) y = y / denominator intersection = Vector2D(x, y) if not self.contains(intersection): return None if not line.contains(intersection): return None return intersection
def reproduce(self, terrain): if self.energy >= ENERGY_TO_REPRODUCE_PREDATOR: self.energy -= INITIAL_PREDATOR_ENERGY patch = terrain.getPatch(self.location.x, self.location.y) pneigh = choice(patch.neighbors) x, y = pneigh.getCenter() p = Bear(self.screen, Vector2D(x, y), self.world) self.world.add_animal(p)
def setUp(self): points = [ Vector2D(0, 0), Vector2D(0, 1), Vector2D(1, 1), Vector2D(1, 0) ] self.p = Polygon(*points) self.l1 = Line(Vector2D(-1, 0.5), Vector2D(2, 0.5)) self.l2 = Line(Vector2D(-1, 2), Vector2D(2, 2))
def comprobar_colision(self): if self.serpiente.bolas[0] in self.serpiente.bolas[1:]: self.jugar = False for fruta in self.frutas: if fruta == self.serpiente.bolas[0]: self.frutas.remove(fruta) self.puntos += 12 self.serpiente.bolas.append(Vector2D(fruta.x, fruta.y))
def __init__(self, screen, location, world): velocity = Vector2D(uniform(-10, 10), uniform(-10, 10)) Mover.__init__(self, screen, location, velocity, 1) self.id = None self.world = world self.energy = 0 self.type = '' self.shape = None self.ungry_Limit = None
def __init__(self, screen, location, velocity, mass): self.location = location self.velocity = velocity self.acceleration = Vector2D(0, 0) self.mass = mass self.screen = screen self.maxSpeed = 90. self.maxForce = 120. self.boxWidth, self.boxHeight = (self.screen.get_size()[0] - GRAPHICS_W, self.screen.get_size()[1])
def wandering(self): deltaTw = 0.5 radius = 100. t = self.location.copy() v = self.velocity.copy() v.mul(deltaTw) t.add(v) theta = uniform(-pi, pi) r = Vector2D(radius * cos(theta), radius * sin(theta)) t.add(r) f = self.seek(t) return f
def escape(self, animalsRace): (col, line) = self.world.terrain.getPatchCoor(self.location.x, self.location.y) EscapeAnimals = () n = 0 for race in animalsRace: print int(countAnimals[race]) n += int(countAnimals[race]) if n == 0: return Vector2D(0, 0) for (x, y) in FOOD_SEARCH_NEIGH: col += x line += y if col < 0 or line < 0 or col >= self.world.terrain.ncols or line >= self.world.terrain.nlines: continue EscapeAnimals = set(EscapeAnimals).union( set(self.world.terrain.patches[col][line].whichAnimalsAreOn( self.world.animals.values(), animalsRace))) force = Vector2D(0, 0) for animal in EscapeAnimals: f = self.flee(animal.location) force.add(f) return force
def onKeyDown(self, unicode, key, mod): Keys = pygame.key.get_pressed() if Keys[K_RETURN] or Keys[K_KP_ENTER] or Keys[K_ESCAPE]: self.running = False if Keys[K_b] and Keys[K_x]: print "Remove Bear" self.world.remove_race("bear") elif Keys[K_r] and Keys[K_x]: print "Remove Rabbit" self.world.remove_race("rabbit") elif Keys[K_w] and Keys[K_x]: print "Remove Wolf" self.world.remove_race("wolf") elif Keys[K_c] and Keys[K_x]: print "Remove Cow" self.world.remove_race("cow") elif Keys[K_b]: (x, y) = pygame.mouse.get_pos() x = min(x, self.width - GRAPHICS_W) a = Bear(self.screen, Vector2D(x, y), self.world) self.world.add_animal(a) elif Keys[K_r]: (x, y) = pygame.mouse.get_pos() x = min(x, self.width - GRAPHICS_W) a = Rabbit(self.screen, Vector2D(x, y), self.world) self.world.add_animal(a) elif Keys[K_w]: (x, y) = pygame.mouse.get_pos() x = min(x, self.width - GRAPHICS_W) a = Wolf(self.screen, Vector2D(x, y), self.world) self.world.add_animal(a) elif Keys[K_c]: (x, y) = pygame.mouse.get_pos() x = min(x, self.width - GRAPHICS_W) a = Cow(self.screen, Vector2D(x, y), self.world) self.world.add_animal(a)
def from_file(file_path): f = None points = [] try: f = open(file_path, 'r') for line in f: values = re.split(r'[ \t]+', line) points.append(Vector2D(float(values[0]), float(values[1]))) except IOError: print("Error reading file") finally: if f: f.close() return Polygon(*points)
def definePoint(self, align, width, font): print 'definePoint() align: ', align one = 1 point = self.definePointX(align, width, font, one) if (point != None): return point point = self.definePointXY(align, width, font, one) if (point != None): return point point = self.definePointY(align, width, font, one) if (point != None): return point return Vector2D(0, 0)
def definePointX(self, align, width, font, one): posX = 0 posY = 0 if (align == Align.CENTER and width < self.cols): posX = self.center(width, 'cols') elif (align == Align.RIGHT and width < self.cols): posX = (width - self.cols) * - one elif (align == Align.LEFT_CENTER): posY = self.center(font.char_height, 'rows') elif (align == Align.LEFT_BOTTOM): posY = (self.rows - font.char_height) + one else: return None return Vector2D(posX, posY)
def definePointY(self, align, width, font, one): rows = 'rows' posY = 0 if(width < self.cols): return None if (align == Align.MODAL): posY = self.center(font.char_height, rows) elif (align == Align.RIGHT_CENTER): posY = self.center(font.char_height, rows) elif (align == Align.RIGHT_BOTTOM): print align posY = (self.rows + font.char_height) + one elif (align == Align.CENTER_BOTTOM): posY = (self.rows - font.char_height) + one else: return None return Vector2D(0, posY)
def __init__(self, number, x, y, life): self.position = Vector2D(x, y) self.number = number self.sprite = [] #sprites self.sprite.append( pygame.image.load("../imagens/enemy/enemyPurple/enemy1_1.png")) self.sprite.append( pygame.image.load("../imagens/enemy/enemyPurple/enemy1_2.png")) self.sprite.append( pygame.image.load("../imagens/enemy/enemyBlue/enemy1_1.png")) self.sprite.append( pygame.image.load("../imagens/enemy/enemyBlue/enemy1_2.png")) self.sprite.append( pygame.image.load("../imagens/enemy/enemyGreen/enemy1_1.png")) self.sprite.append( pygame.image.load("../imagens/enemy/enemyGreen/enemy1_2.png")) self.directionRight = True self.velocity = 5 self.acelerate = 0.0
def __init__(self, x=0, y=0, wid=0, hei=0): self.point = Vector2D(x, y) self.wid = wid self.hei = hei
def test_hit_test1(self): self.assertEqual(self.p.hit_point(self.l1), Vector2D(0, 0.5))
def test_intersection(self): intersections = self.p.intersections(self.l1) self.assertEqual(intersections[0], Vector2D(0, 0.5)) self.assertEqual(intersections[1], Vector2D(1, 0.5))