""" Array Backed Grid Show how to use a two-dimensional list/array to back the display of a grid on-screen. If Python and Arcade are installed, this example can be run from the command line with: python -m arcade.examples.array_backed_grid_buffered """ import arcade # Set how many rows and columns we will have ROW_COUNT = 15 COLUMN_COUNT = 15 # This sets the WIDTH and HEIGHT of each grid location WIDTH = 30 HEIGHT = 30 # This sets the margin between each cell # and on the edges of the screen. MARGIN = 5 # Do the math to figure out our screen dimensions SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN SCREEN_TITLE = "Array Backed Grid Buffered Example" class MyGame(arcade.Window): """ Main application class. """ def __init__(self, width, height, title): """ Set up the application. """ super().__init__(width, height, title) self.shape_list = None # Create a 2 dimensional array. A two dimensional # array is simply a list of lists. self.grid = [] for row in range(ROW_COUNT): # Add an empty array that will hold each cell # in this row self.grid.append([]) for column in range(COLUMN_COUNT): self.grid[row].append(0) # Append a cell arcade.set_background_color(arcade.color.BLACK) self.recreate_grid() def recreate_grid(self): self.shape_list = arcade.ShapeElementList() for row in range(ROW_COUNT): for column in range(COLUMN_COUNT): if self.grid[row][column] == 0: color = arcade.color.WHITE else: color = arcade.color.GREEN x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2 y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2 current_rect = arcade.create_rectangle_filled(x, y, WIDTH, HEIGHT, color) self.shape_list.append(current_rect) def on_draw(self): """ Render the screen. """ # This command has to happen before we start drawing arcade.start_render() self.shape_list.draw() def on_mouse_press(self, x, y, button, modifiers): """ Called when the user presses a mouse button. """ # Change the x/y screen coordinates to grid coordinates column = x // (WIDTH + MARGIN) row = y // (HEIGHT + MARGIN) print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})") # Make sure we are on-grid. It is possible to click in the upper right # corner in the margin and go to a grid location that doesn't exist if row < ROW_COUNT and column < COLUMN_COUNT: # Flip the location between 1 and 0. if self.grid[row][column] == 0: self.grid[row][column] = 1 else: self.grid[row][column] = 0 self.recreate_grid() def main(): MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) arcade.run() if __name__ == "__main__": main()