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 _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 _main(): text_grid = gk.create_grid(27, 45, "Bouncing Balls") red = Ball(gk.Rect(1, 1, 3, 3), 0.2, 0.4, '*') green = Ball(gk.Rect(10, 12, 5, 5), -0.2, 0.1, '=') blue = Ball(gk.Rect(100, 40, 2, 2), 0.8, -0.4, 'o') text_grid.map_color('*', gk.Colors.Red) text_grid.map_color('=', gk.Colors.Green) text_grid.map_color('o', gk.Colors.Blue) gk.start() for _ in range(300): red.update(text_grid) green.update(text_grid) blue.update(text_grid) red.draw(text_grid) green.draw(text_grid) blue.draw(text_grid) text_grid.blit() gk.next_frame() gk.stop()
def animate(self, program, robot): """ Animate the routine """ labels = self._animation_labels(robot) program = program.copy() program[0] = 2 computer = Computer(program) while not computer.needs_input: computer.step() for char in "".join(self) + "y\n": computer.write(ord(char)) gk.start() rows = 37 cols = 45 grid = gk.create_grid(rows, cols, "Vacuum Robot") grid.map_color('.', gk.Colors.Navy) grid.map_color('#', gk.Colors.Gray) grid.map_color('A', gk.Colors.Red) grid.map_color('B', gk.Colors.Green) grid.map_color('C', gk.Colors.Blue) lines = [[' '] * cols for _ in range(rows)] row = 0 col = 0 frame = -2 input("Press enter to begin animation...") while not computer.is_halted: while not computer.num_outputs: computer.step() val = computer.read() if val < 255: if val == ord('\n'): if col: row += 1 col = 0 else: self.draw(grid, lines, labels, frame) row = 0 col = 0 frame += 1 else: lines[row][col] = chr(val) col += 1 self.draw(grid, lines, labels, frame) gk.stop()
def _run_program_animated(robot, program): grid = gk.create_grid(6, 43, "Painting Robot") gk.start() computer = Computer(program) computer.write(robot.camera()) _draw(grid, robot) input("Press enter to begin...") while not computer.is_halted: computer.step() if computer.num_outputs == 2: robot.paint(computer.read()) robot.move(computer.read()) computer.write(robot.camera()) _draw(grid, robot) gk.next_frame() input("Press any key to finish...") gk.stop()
def _main(): # create a TextGrid text_grid = gk.create_grid(ROWS, COLS, "Hello World") # map certain ASCII characters to a color text_grid.map_color('o', gk.Colors.Red) text_grid.map_color('l', gk.Colors.Blue) text_grid.map_color('!', gk.Colors.Green) # start the GL main loop gk.start() # the animation loop bounds = gk.Rect(COLS - BALL_SIZE, ROWS, BALL_SIZE, BALL_SIZE) row = 0 text = "Hello world!" for _ in range(1, 100): # clear the grid text_grid.clear(row, 0, len(text)) text_grid.clear(bounds) # animate bounds = bounds.translate(0, -1) if bounds.bottom == 0: bounds = bounds.translate(0, ROWS + BALL_SIZE) row = (row + 1) % ROWS # we can draw text directly to the grid text_grid.draw(row, 0, text) # we can draw rectangles of the same character text_grid.draw(bounds, 'x') # both of the above methods use the grid's default # color mapping. We can control the coloring directly # as shown below letters = [ gk.Letter('l', gk.Colors.Cyan), gk.Letter('o', gk.Colors.Magenta), gk.Letter('l', gk.Colors.Yellow), gk.Letter('!', gk.Colors.White) ] text_grid.draw(ROWS // 2, COLS // 2, letters) # key input works via a polling model if gk.is_pressed(gk.Key.Left): text_grid.draw(0, 0, 'L') else: text_grid.draw(0, 0, ' ') # we let GL know that the TextGrid is ready to be redrawn text_grid.blit() # Now we wait until it is time for the next frame. # You can optionally pass a target framerate # (default is 30hz) gk.next_frame(10) # stop the GL main loop gk.stop()