def __init__(self, x1, y1, x2, y2, color=(255, 255, 255)): self.p1 = Vector2D(x1, y1) self.p2 = Vector2D(x2, y2) self.vec = self.p2 - self.p1 #print self.vec self.color = color self.thickness = 1
def __init__(self, screen, pos=Vector2D(0, 0), size=24, vert_count=10): self.screen = screen self.pos = pos self.size = size self.color = colors.green self.velocity = Vector2D(0, 0) self.points = [] self.generate_random_shape(vert_count) self.type = 1
def move(self, pacnode, nodelist, alg): new_node = self.find_node(nodelist) if (new_node): self.currentnode = new_node self.moving = False if (not self.moving): node = self.currentnode keyerrorFlag = False #this is to catch keyerror exception, as child key is not present always try: map = search_pacnode_by_alg(node, pacnode, alg) child = pacnode parent = pacnode while True: parent = map[child] if (parent == node): break else: child = parent except: keyerrorFlag = True if not keyerrorFlag: direct = child.position - parent.position direct = direct.normalize() self.direction = direct else: self.direction = Vector2D(0, 0) self.moving = True self.pos += self.direction * self.speed
def move_to(self, position): """ Moves object to a target position :param position: an absolute target position """ self._last_position = self.position self.position = Vector2D(position)
def collide(self, other): # Exit early for optimization if (self.position - other.position).length() > max(self.width, self.height) + max(other.width, other.height): return False, None, None def project(vertices, axis): dots = [vertex.dot(axis) for vertex in vertices] return Vector2D(min(dots), max(dots)) collision_depth = sys.maxsize collision_normal = None for edge in self.edges + other.edges: axis = Vector2D(edge).orthogonal().normalize() projection_1 = project(self.vertices, axis) projection_2 = project(other.vertices, axis) min_intersection = max(min(projection_1), min(projection_2)) max_intersection = min(max(projection_1), max(projection_2)) overlapping = min_intersection <= max_intersection if not overlapping: return False, None, None else: overlap = max_intersection - min_intersection if overlap < collision_depth: collision_depth = overlap collision_normal = axis return True, collision_depth, collision_normal
def __init__(self, x, y): self.name = "pellet" self.position = Vector2D(x, y) self.color = YELLOW self.radius = 2 self.value = 10 self.draw = True
def __init__(self, pos, width, height): self.x, self.y = (pos) #self.coord = (self.x*width*1.0, self.y*height*1.0) self.position = Vector2D(self.x * width, self.y * height) self.neighbors = [] self.target = None self.directions = []
def __init__(self, position, drawer): self._matrix = [(None, None, 1, None), (None, None, 1, None), (None, None, 1, None), (None, None, 1, None)] self._drawer = drawer self.position = Vector2D(position)
def AddSegment(self, segment): '''Turn segment into unit vector perpendicular to segment''' self.segments.append(segment) if self.direction is None: d = Vector2D(segment.vec.y, segment.vec.x * -1) d = d.norm() self.direction = d
def __init__(self, speed, node, color, dim, pos=(0, 0)): AbstractEntity.__init__(self, dim, pos) self.COLOR = color self.direction = Vector2D(0, 0) self.speed = speed self.currentnode = node self.moving = False self.scatter = False self.rand_move_count = 1
def edges(self): return [ Vector2D(v).rotate(self.angle) for v in ( (self.width, 0), (0, self.height), (-self.width, 0), (0, -self.height), ) ]
def vertices(self): return [ self.position + Vector2D(v).rotate(-self.angle) for v in ( (-self.width / 2, -self.height / 2), (self.width / 2, -self.height / 2), (self.width / 2, self.height / 2), (-self.width / 2, self.height / 2) ) ]