Array-Backed Grid Buffered

Screenshot of a program that shows an array backed grid.

You may also want to look at:

array_backed_grid_buffered.py
  1"""
  2Array Backed Grid
  3
  4Show how to use a two-dimensional list/array to back the display of a
  5grid on-screen.
  6
  7If Python and Arcade are installed, this example can be run from the command line with:
  8python -m arcade.examples.array_backed_grid_buffered
  9"""
 10import arcade
 11
 12# Set how many rows and columns we will have
 13ROW_COUNT = 15
 14COLUMN_COUNT = 15
 15
 16# This sets the WIDTH and HEIGHT of each grid location
 17WIDTH = 30
 18HEIGHT = 30
 19
 20# This sets the margin between each cell
 21# and on the edges of the screen.
 22MARGIN = 5
 23
 24# Do the math to figure out our screen dimensions
 25SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
 26SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
 27SCREEN_TITLE = "Array Backed Grid Buffered Example"
 28
 29
 30class MyGame(arcade.Window):
 31    """
 32    Main application class.
 33    """
 34
 35    def __init__(self, width, height, title):
 36        """
 37        Set up the application.
 38        """
 39        super().__init__(width, height, title)
 40
 41        self.shape_list = None
 42
 43        # Create a 2 dimensional array. A two dimensional
 44        # array is simply a list of lists.
 45        self.grid = []
 46        for row in range(ROW_COUNT):
 47            # Add an empty array that will hold each cell
 48            # in this row
 49            self.grid.append([])
 50            for column in range(COLUMN_COUNT):
 51                self.grid[row].append(0)  # Append a cell
 52
 53        arcade.set_background_color(arcade.color.BLACK)
 54        self.recreate_grid()
 55
 56    def recreate_grid(self):
 57        self.shape_list = arcade.ShapeElementList()
 58        for row in range(ROW_COUNT):
 59            for column in range(COLUMN_COUNT):
 60                if self.grid[row][column] == 0:
 61                    color = arcade.color.WHITE
 62                else:
 63                    color = arcade.color.GREEN
 64
 65                x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2
 66                y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2
 67
 68                current_rect = arcade.create_rectangle_filled(x, y, WIDTH, HEIGHT, color)
 69                self.shape_list.append(current_rect)
 70
 71    def on_draw(self):
 72        """
 73        Render the screen.
 74        """
 75
 76        # This command has to happen before we start drawing
 77        arcade.start_render()
 78
 79        self.shape_list.draw()
 80
 81    def on_mouse_press(self, x, y, button, modifiers):
 82        """
 83        Called when the user presses a mouse button.
 84        """
 85
 86        # Change the x/y screen coordinates to grid coordinates
 87        column = int(x // (WIDTH + MARGIN))
 88        row = int(y // (HEIGHT + MARGIN))
 89
 90        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
 91
 92        # Make sure we are on-grid. It is possible to click in the upper right
 93        # corner in the margin and go to a grid location that doesn't exist
 94        if row < ROW_COUNT and column < COLUMN_COUNT:
 95
 96            # Flip the location between 1 and 0.
 97            if self.grid[row][column] == 0:
 98                self.grid[row][column] = 1
 99            else:
100                self.grid[row][column] = 0
101
102        self.recreate_grid()
103
104
105def main():
106    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
107    arcade.run()
108
109
110if __name__ == "__main__":
111    main()