def drawCross(self, point, size=8, color=(0, 255, 0)): self.drawLine(point.translateBy(Vector(-size, -size)), point.translateBy(Vector(size, size)), color=color) self.drawLine(point.translateBy(Vector(size, -size)), point.translateBy(Vector(-size, size)), color=color)
def load(file): """ Loads the sector information from file """ data = np.load(file) walls = [Vector(*loc) for loc in data["walls"]] empty = [Vector(*loc) for loc in data["empty"]] oxygen_system = Vector(*data["oxygen_system"]) return Sector(walls, empty, oxygen_system)
def _collide_with_border(self, physical_object): """ Checks and performs collision with border if necessary :param physical_object: an object to check :param energy_conserved: how much kinetic energy is conserved during collision """ x, y = physical_object.pos min_x, max_x = self._x_border min_y, max_y = self._y_border speed = physical_object.velocity.magnitude() direction = physical_object.velocity.normalize() radius = physical_object.radius collided = False if (x <= min_x + radius and direction.x < 0) or (x >= max_x - radius and direction.x > 0): speed = math.sqrt(physical_object.energy_conserved * speed**2) direction = Vector(-direction.x, direction.y) physical_object.velocity = direction * speed collided = True if (y <= min_y + radius and direction.y < 0) or (y >= max_y - radius and direction.y > 0): if not collided: speed = math.sqrt(physical_object.energy_conserved * speed**2) direction = Vector(direction.x, -direction.y) physical_object.velocity = direction * speed
def drawCrossVertical(self, point, size=8, color=(0, 255, 0)): self.drawLine(point.translateBy(Vector(-size, 0)), point.translateBy(Vector(size, 0)), thickness=2, color=color) self.drawLine(point.translateBy(Vector(0, -size)), point.translateBy(Vector(0, size)), thickness=2, color=color)
def shiftImageHorizontally(self, pixelsToShift): # type: (int) -> Image boxSizeOfImage = Box.createUsingDimesions(self.width(), self.height()) fillerWidth = abs(pixelsToShift) imageWithFillerOnBothSides = self.padSidesToMakeWider(fillerWidth * 2) boxEncompasingImage = boxSizeOfImage.translateBy(Vector( fillerWidth, 0)) #slide "boxEncompasingImage" to the left or to the right depending if xDrift is negative or positive boxAroundAreaThatWeNeedToCrop = boxEncompasingImage.translateBy( Vector(pixelsToShift, 0)) return imageWithFillerOnBothSides.subImage( boxAroundAreaThatWeNeedToCrop)
def update(self): x, y = pg.mouse.get_pos() self.direction = (Vector(x, y) - self.pos).normalize() if self.is_mouse_down: self.shooting_power = min(self.shooting_power + Cannon.shooting_power_per_second * self.game.dt, Cannon.max_shooting_power)
def draw(self, drone=Vector(0, 0)): """ Draw the sector to the console """ bounds = self.bounds() offset = Vector(bounds.left, bounds.top) lines = [['.'] * bounds.width for _ in range(bounds.height)] for wall in self.walls: pos = wall - offset lines[pos.y][pos.x] = '#' pos = self.oxygen_system - offset lines[pos.y][pos.x] = 'O' pos = drone - offset lines[pos.y][pos.x] = 'D' for line in lines: print("".join(line))
def __init__(self, program, move_cb=None): self._cpu = Computer(program) self.location = Vector(0, 0) self._empty = set() self._walls = set() self._oxygen_system = None self._move_cb = move_cb
def _explore_animation(program, sector): bounds = sector.bounds() gk.start() grid = gk.create_grid(bounds.height, bounds.width, "Repair Droid") grid.map_color('#', gk.Colors.Red) grid.map_color('D', gk.Colors.White) grid.map_color('.', gk.Colors.Gray) grid.map_color('O', gk.Colors.Blue) offset = Vector(bounds.left, bounds.top) def _move_cb(location, walls, empty, oxygen_system): for tile in walls: pos = tile - offset grid.draw(pos.y, pos.x, "#") for tile in empty: pos = tile - offset grid.draw(pos.y, pos.x, '.') if oxygen_system: pos = oxygen_system - offset grid.draw(pos.y, pos.x, 'O') pos = location - offset grid.draw(pos.y, pos.x, "D") grid.blit() gk.next_frame(30) input("Press enter to start animation...") robot = RepairDrone(program, move_cb=_move_cb) robot.explore() gk.stop()
def dnd_moved(self, event): for dp, do in self.drag_points.items(): coords = self.canvas.coords(dp)[:2] do[0].SetPoint( Vector(coords[0] + self.dp_w2, coords[1] + self.dp_w2), do[1]) self.update()
def _fill_animation(sector): bounds = sector.bounds() gk.start() grid = gk.create_grid(bounds.height, bounds.width, "Oxygen Fill") grid.map_color('#', gk.Colors.Red) grid.map_color('.', gk.Colors.Gray) grid.map_color('O', gk.Colors.Blue) offset = Vector(bounds.left, bounds.top) def _step_cb(walls, empty, oxygen): for tile in walls: pos = tile - offset grid.draw(pos.y, pos.x, "#") for tile in empty: pos = tile - offset grid.draw(pos.y, pos.x, '.') for tile in oxygen: pos = tile - offset grid.draw(pos.y, pos.x, 'O') grid.blit() gk.next_frame(30) input("Press enter to start animation...") sector.fill(step_cb=_step_cb) gk.stop()
def __init__(self, lines): self.entrances = set() self.keys = set() self.doors = set() self.door_keys = {} self.grid = {} for y, line in enumerate(lines): for x, char in enumerate(line.strip()): loc = Vector(x, y) if char == '#': self.grid[loc] = '#' elif char == '.': self.grid[loc] = '.' elif char == '@': self.entrances.add(loc) self.grid[loc] = '.' elif char.isalpha(): self.grid[loc] = char if char.islower(): self.keys.add(loc) else: self.doors.add(loc) self.door_keys[loc] = char.lower() else: raise ValueError("Unexpected character: " + char)
def _parse_bugs(lines): bugs = set() for y, line in enumerate(lines): for x, char in enumerate(line): if char == '#': bugs.add(Vector(x, y)) return bugs
def __init__(self, pos, game): super().__init__(pos, game) game.subscribe_to_event(pg.MOUSEBUTTONDOWN, self._mousebuttondown_listener) game.subscribe_to_event(pg.MOUSEBUTTONUP, self._mousebuttonup_listener) self.shooting_power = 0 self.direction = Vector(1, 0) self.is_mouse_down = False self._projectiles = []
def __init__(self, game): width, height = game.resolution super().__init__( Vector(Scoreboard.x_pos * width, Scoreboard.y_pos * height), game) self.scoreboard = { 'projectiles_shot': 0, 'enemies_destroyed': 0, 'score': 0 }
def __init__(self, canvas, *points, **kwargs): pts = [] i = iter(points) while True: try: pts.append(Vector(next(i), next(i))) except StopIteration: break points = pts Polygon.__init__(self, points, deepcopy=False) self.c = canvas self.p = self.c.create_polygon(tuple(self.GenCoords()), **kwargs)
def _write_output(self, index): computer = self.computers[index] while computer.num_outputs < 3: computer.step() if computer.num_outputs == 3: address = computer.read() x = computer.read() y = computer.read() packet = Vector(x, y) if address == 255: self.nat = packet else: self.queues[address].append(packet)
def __init__(self, master): CanvasDnD.__init__(self, master) self.polygons = [] self.segments = [] self.crosses = [] self.drag_points = {} self.dp_w2 = 10 self.segments.extend([ CanvasSegment(self.canvas, Vector(100, 100), Vector(0, 100), fill="red"), CanvasSegment(self.canvas, Vector(200, 100), Vector(0, 100), fill="green"), ]) self.polygons.extend([ CanvasPolygon(self.canvas, 300, 300, 400, 300, 400, 400, 300, 400, fill="", outline="black") ]) self.bind('<<DnDMoved>>', self.dnd_moved) self.create_drags() self.update()
def getDrift(self): # type: () -> Vector numOfFrames = len(self.__topLeftPoints) if numOfFrames <= 1: return None lastFrame = self.__frameIDs[numOfFrames - 1] beforeLastFrame = self.__frameIDs[numOfFrames - 2] lastPoint = self.__topLeftPoints[lastFrame] beforeLastPoint = self.__topLeftPoints[beforeLastFrame] driftVector = Vector(lastPoint.x - beforeLastPoint.x, lastPoint.y - beforeLastPoint.y) if (driftVector.isZeroVector()): return None return driftVector
def getMedianDriftVector(self): withoutOutliers = self.excludeOutliers(self._drifts) if not withoutOutliers: return None if len(withoutOutliers) <= 0: return None driftX = list() driftY = list() for drift in withoutOutliers: if not drift.isZeroVector(): driftX.append(drift.x) driftY.append(drift.y) medianXDrift = numpy.median(driftX) medianYDrift = numpy.median(driftY) return Vector(medianXDrift, medianYDrift)
def __init__(self, lines): self.walls = set() self.open = set() portals = {} self.portals = {} self.warps = {} self.inner_warps = set() self.outer_warps = set() self.warp_portal = {} self.rows = len(lines) self.cols = len(lines[0]) - 1 for y, line in enumerate(lines): for x, char in enumerate(line): loc = Vector(x, y) if char == '#': self.walls.add(loc) elif char == '.': self.open.add(loc) elif char.isalpha(): portals[loc] = char self._init_portals(portals)
def x(self, new_x): self.position = Vector(new_x, self.y)
def __init__(self, pos): super().__init__() self.position = Vector(*pos)
""" Solution for day 15 """ from collections import namedtuple from enum import IntEnum import numpy as np from intcode import Computer from common import asset, a_star, Vector, Neighbors import glasskey as gk Directions = [Vector(0, -1), Vector(0, 1), Vector(-1, 0), Vector(1, 0)] def _to_command(vec): return Directions.index(vec) + 1 class Status(IntEnum): """ The robot status """ Wall = 0 Empty = 1 OxygenSystem = 2 def _to_numpy(vectors): result = np.zeros((len(vectors), 2), np.int32) for i, vector in enumerate(vectors): result[i, 0] = vector.x result[i, 1] = vector.y
def position(self): return Vector(self.rect.x, self.rect.y)
def draw(self, surface): for layer in self.layers: layer.draw(surface, Vector(0, 0)) if self.has_dialogue(): self._dialogue.draw(surface)
def steps_to_oxygen(self): """ Compute the number of steps to get to the oxygen system from the origin. """ start = Vector(0, 0) goal = self.oxygen_system return len(a_star(start, goal, Neighbors(self.empty))) - 1
def y(self, new_y): self.position = Vector(self.x, new_y)
from functools import lru_cache from common import asset, read_tests, Vector def _parse_bugs(lines): bugs = set() for y, line in enumerate(lines): for x, char in enumerate(line): if char == '#': bugs.add(Vector(x, y)) return bugs POS = [Vector(x, y) for x, y in itertools.product(range(5), range(5))] def _step(current_bugs): next_bugs = set() for pos in POS: num_bugs = sum( [neighbor in current_bugs for neighbor in pos.neighbors()]) if pos in current_bugs: if num_bugs == 1: next_bugs.add(pos) elif num_bugs in (1, 2): next_bugs.add(pos) return next_bugs
def spawn_enemies(self): number = random.randint(Game.min_enemies, Game.max_enemies + 1) self.enemies = [Enemy(Vector(200, 200), self) for i in range(number)]