Grid Using Sprites v1

Screenshot of a program that shows an array backed grid.

You may also want to look at:

array_backed_grid_sprites_1.py
  1"""
  2Array Backed Grid Shown By Sprites
  3
  4Show how to use a two-dimensional list/array to back the display of a
  5grid on-screen.
  6
  7This version syncs the grid to the sprite list in one go using resync_grid_with_sprites.
  8
  9If Python and Arcade are installed, this example can be run from the command line with:
 10python -m arcade.examples.array_backed_grid_sprites_1
 11"""
 12import arcade
 13
 14# Set how many rows and columns we will have
 15ROW_COUNT = 15
 16COLUMN_COUNT = 15
 17
 18# This sets the WIDTH and HEIGHT of each grid location
 19WIDTH = 30
 20HEIGHT = 30
 21
 22# This sets the margin between each cell
 23# and on the edges of the screen.
 24MARGIN = 5
 25
 26# Do the math to figure out our screen dimensions
 27SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
 28SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
 29SCREEN_TITLE = "Array Backed Grid Example"
 30
 31
 32class MyGame(arcade.Window):
 33    """
 34    Main application class.
 35    """
 36
 37    def __init__(self, width, height, title):
 38        """
 39        Set up the application.
 40        """
 41        super().__init__(width, height, title)
 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
 55        self.grid_sprite_list = arcade.SpriteList()
 56
 57        # Create a list of solid-color sprites to represent each grid location
 58        for row in range(ROW_COUNT):
 59            for column in range(COLUMN_COUNT):
 60                x = column * (WIDTH + MARGIN) + (WIDTH / 2 + MARGIN)
 61                y = row * (HEIGHT + MARGIN) + (HEIGHT / 2 + MARGIN)
 62                sprite = arcade.SpriteSolidColor(WIDTH, HEIGHT, arcade.color.WHITE)
 63                sprite.center_x = x
 64                sprite.center_y = y
 65                self.grid_sprite_list.append(sprite)
 66
 67    def resync_grid_with_sprites(self):
 68        self.shape_list = arcade.ShapeElementList()
 69        for row in range(ROW_COUNT):
 70            for column in range(COLUMN_COUNT):
 71                # We need to convert our two dimensional grid to our
 72                # one-dimensional sprite list. For example a 10x10 grid might have
 73                # row 2, column 8 mapped to location 28. (Zero-basing throws things
 74                # off, but you get the idea.)
 75                # ALTERNATIVELY you could set self.grid_sprite_list[pos].texture
 76                # to different textures to change the image instead of the color.
 77                pos = row * COLUMN_COUNT + column
 78                if self.grid[row][column] == 0:
 79                    self.grid_sprite_list[pos].color = arcade.color.WHITE
 80                else:
 81                    self.grid_sprite_list[pos].color = arcade.color.GREEN
 82
 83    def on_draw(self):
 84        """
 85        Render the screen.
 86        """
 87
 88        # This command has to happen before we start drawing
 89        arcade.start_render()
 90
 91        self.grid_sprite_list.draw()
 92
 93    def on_mouse_press(self, x, y, button, modifiers):
 94        """
 95        Called when the user presses a mouse button.
 96        """
 97
 98        # Change the x/y screen coordinates to grid coordinates
 99        column = int(x // (WIDTH + MARGIN))
100        row = int(y // (HEIGHT + MARGIN))
101
102        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
103
104        # Make sure we are on-grid. It is possible to click in the upper right
105        # corner in the margin and go to a grid location that doesn't exist
106        if row < ROW_COUNT and column < COLUMN_COUNT:
107
108            # Flip the location between 1 and 0.
109            if self.grid[row][column] == 0:
110                self.grid[row][column] = 1
111            else:
112                self.grid[row][column] = 0
113
114        self.resync_grid_with_sprites()
115
116
117def main():
118    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
119    arcade.run()
120
121
122if __name__ == "__main__":
123    main()