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 _main(): left = gk.create_grid(ROWS, COLS, "Left") right = gk.create_grid(ROWS, COLS, "Right") gk.start() bounds = gk.Rect(0, 3, 3, 3) left.draw(9, COLS - 5, "multiitlum") right.draw(9, -5, "multiitlum") delta_x = 1 for _ in range(300): left.clear(bounds) right.clear(bounds.translate(-COLS, 0)) bounds = bounds.translate(delta_x, 0) if bounds.right == 2 * COLS or bounds.left == 0: delta_x *= -1 left.draw(bounds, '+') right.draw(bounds.translate(-COLS, 0), '+') left.blit() right.blit() gk.next_frame() gk.stop()
def bounds(self): """ The bounds of the sector """ max_x = max([loc.x for loc in self.walls]) max_y = max([loc.y for loc in self.walls]) min_x = min([loc.x for loc in self.walls]) min_y = min([loc.y for loc in self.walls]) width = max_x - min_x + 1 height = max_y - min_y + 1 return gk.Rect(min_x, min_y, width, height)
def _bounds(robot): x_values = set([tile.x for tile in robot.painted]) y_values = set([tile.y for tile in robot.painted]) min_x = min(x_values) min_y = min(y_values) width = max(x_values) - min_x + 1 height = max(y_values) - min_y + 1 return gk.Rect(0, 0, width, height)
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()