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. We can click each cell in the window to toggle the color
  6or each individual cell.
  7
  8This is not the most efficient way to maintain an updated grid
  9simply because we have to rebuild the shape list from scratch
 10every time it changes, but it's fast enough for smaller grids
 11that don't update frequently.
 12
 13If Python and Arcade are installed, this example can be run from the command line with:
 14python -m arcade.examples.array_backed_grid_buffered
 15"""
 16import arcade
 17
 18# Set how many rows and columns we will have
 19ROW_COUNT = 15
 20COLUMN_COUNT = 15
 21
 22# This sets the WIDTH and HEIGHT of each grid location
 23WIDTH = 30
 24HEIGHT = 30
 25
 26# This sets the margin between each cell
 27# and on the edges of the screen.
 28MARGIN = 5
 29
 30# Do the math to figure out our screen dimensions
 31SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
 32SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
 33SCREEN_TITLE = "Array Backed Grid Buffered Example"
 34
 35
 36class MyGame(arcade.Window):
 37    """
 38    Main application class.
 39    """
 40
 41    def __init__(self, width, height, title):
 42        """
 43        Set up the application.
 44        """
 45        super().__init__(width, height, title)
 46        self.shape_list = None
 47
 48        # Create a 2 dimensional array. A two dimensional
 49        # array is simply a list of lists.
 50        # This array can be altered later to contain 0 or 1
 51        # to show a white or green cell.
 52        #
 53        # A 4 x 4 grid would look like this
 54        #
 55        # grid = [
 56        #     [0, 0, 0, 0],
 57        #     [0, 0, 0, 0],
 58        #     [0, 0, 0, 0],
 59        #     [0, 0, 0, 0],
 60        # ]
 61        # We can quickly build a grid with python list comprehension
 62        # self.grid = [[0] * COLUMN_COUNT for _ in range(ROW_COUNT)]
 63        # Making the grid with loops:
 64        self.grid = []
 65        for row in range(ROW_COUNT):
 66            # Add an empty array that will hold each cell
 67            # in this row
 68            self.grid.append([])
 69            for column in range(COLUMN_COUNT):
 70                self.grid[row].append(0)  # Append a cell
 71
 72        # Set the window's background color
 73        self.background_color = arcade.color.BLACK
 74        # Create shapes from the grid
 75        self.recreate_grid()
 76
 77    def recreate_grid(self):
 78        """
 79        Create the shapes for our current grid.
 80
 81        We look at the values in each cell.
 82        If the cell contains 0 we crate a white shape.
 83        If the cell contains 1 we crate a green shape.
 84        """
 85        self.shape_list = arcade.shape_list.ShapeElementList()
 86        for row in range(ROW_COUNT):
 87            for column in range(COLUMN_COUNT):
 88                if self.grid[row][column] == 0:
 89                    color = arcade.color.WHITE
 90                else:
 91                    color = arcade.color.GREEN
 92
 93                x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2
 94                y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2
 95
 96                current_rect = arcade.shape_list.create_rectangle_filled(x, y, WIDTH, HEIGHT, color)
 97                self.shape_list.append(current_rect)
 98
 99    def on_draw(self):
100        """
101        Render the screen.
102        """
103        # We should always start by clearing the window pixels
104        self.clear()
105
106        # Draw the shapes representing our current grid
107        self.shape_list.draw()
108
109    def on_mouse_press(self, x, y, button, modifiers):
110        """
111        Called when the user presses a mouse button.
112        """
113
114        # Convert the clicked mouse position into grid coordinates
115        column = int(x // (WIDTH + MARGIN))
116        row = int(y // (HEIGHT + MARGIN))
117
118        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
119
120        # Make sure we are on-grid. It is possible to click in the upper right
121        # corner in the margin and go to a grid location that doesn't exist
122        if row >= ROW_COUNT or column >= COLUMN_COUNT:
123            # Simply return from this method since nothing needs updating
124            return
125
126        # Flip the location between 1 and 0.
127        if self.grid[row][column] == 0:
128            self.grid[row][column] = 1
129        else:
130            self.grid[row][column] = 0
131
132        # Rebuild the shapes
133        self.recreate_grid()
134
135
136def main():
137    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
138    arcade.run()
139
140
141if __name__ == "__main__":
142    main()