Array-Backed Grid

Screenshot of a program that shows an array backed grid.

If you work with grids much, you’ll find this to be slow. You may want to look at:

array_backed_grid.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
  7Note: Regular drawing commands are slow. Particularly when drawing a lot of
  8items, like the rectangles in this example.
  9
 10For faster drawing, create the shapes and then draw them as a batch.
 11See array_backed_grid_buffered.py
 12
 13If Python and Arcade are installed, this example can be run from the command line with:
 14python -m arcade.examples.array_backed_grid
 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
 31WINDOW_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
 32WINDOW_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
 33WINDOW_TITLE = "Array Backed Grid Example"
 34
 35
 36class GameView(arcade.View):
 37    """
 38    Main application class.
 39    """
 40
 41    def __init__(self):
 42        """
 43        Set up the application.
 44        """
 45
 46        super().__init__()
 47
 48        # Create a 2 dimensional array. A two-dimensional
 49        # array is simply a list of lists.
 50        self.grid = []
 51        for row in range(ROW_COUNT):
 52            # Add an empty array that will hold each cell
 53            # in this row
 54            self.grid.append([])
 55            for column in range(COLUMN_COUNT):
 56                self.grid[row].append(0)  # Append a cell
 57
 58        self.background_color = arcade.color.BLACK
 59
 60    def on_draw(self):
 61        """
 62        Render the screen.
 63        """
 64
 65        # This command has to happen before we start drawing
 66        self.clear()
 67
 68        # Draw the grid
 69        for row in range(ROW_COUNT):
 70            for column in range(COLUMN_COUNT):
 71                # Figure out what color to draw the box
 72                if self.grid[row][column] == 1:
 73                    color = arcade.color.GREEN
 74                else:
 75                    color = arcade.color.WHITE
 76
 77                # Do the math to figure out where the box is
 78                x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2
 79                y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2
 80
 81                # Draw the box
 82                arcade.draw_rect_filled(arcade.rect.XYWH(x, y, WIDTH, HEIGHT), color)
 83
 84    def on_mouse_press(self, x, y, button, modifiers):
 85        """
 86        Called when the user presses a mouse button.
 87        """
 88
 89        # Change the x/y screen coordinates to grid coordinates
 90        column = int(x // (WIDTH + MARGIN))
 91        row = int(y // (HEIGHT + MARGIN))
 92
 93        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
 94
 95        # Make sure we are on-grid. It is possible to click in the upper right
 96        # corner in the margin and go to a grid location that doesn't exist
 97        if row < ROW_COUNT and column < COLUMN_COUNT:
 98
 99            # Flip the location between 1 and 0.
100            if self.grid[row][column] == 0:
101                self.grid[row][column] = 1
102            else:
103                self.grid[row][column] = 0
104
105
106def main():
107    """ Main function """
108    # Create a window class. This is what actually shows up on screen
109    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
110
111    # Create the GameView
112    game = GameView()
113
114    # Show GameView on screen
115    window.show_view(game)
116
117    # Start the arcade game loop
118    arcade.run()
119
120
121if __name__ == "__main__":
122    main()