Grid Using Sprites v2

Screenshot of a program that shows an array backed grid.

You may also want to look at:

array_backed_grid_sprites.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 makes a grid of sprites instead of numbers
  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_2
 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 Buffered 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        arcade.set_background_color(arcade.color.BLACK)
 44
 45        # One dimensional list of all sprites in the two-dimensional sprite list
 46        self.grid_sprite_list = arcade.SpriteList()
 47
 48        # This will be a two-dimensional grid of sprites to mirror the two
 49        # dimensional grid of numbers. This points to the SAME sprites that are
 50        # in grid_sprite_list, just in a 2d manner.
 51        self.grid_sprites = []
 52
 53        # Create a list of solid-color sprites to represent each grid location
 54        for row in range(ROW_COUNT):
 55            self.grid_sprites.append([])
 56            for column in range(COLUMN_COUNT):
 57                x = column * (WIDTH + MARGIN) + (WIDTH / 2 + MARGIN)
 58                y = row * (HEIGHT + MARGIN) + (HEIGHT / 2 + MARGIN)
 59                sprite = arcade.SpriteSolidColor(WIDTH, HEIGHT, arcade.color.WHITE)
 60                sprite.center_x = x
 61                sprite.center_y = y
 62                self.grid_sprite_list.append(sprite)
 63                self.grid_sprites[row].append(sprite)
 64
 65    def on_draw(self):
 66        """
 67        Render the screen.
 68        """
 69
 70        # This command has to happen before we start drawing
 71        arcade.start_render()
 72
 73        self.grid_sprite_list.draw()
 74
 75    def on_mouse_press(self, x, y, button, modifiers):
 76        """
 77        Called when the user presses a mouse button.
 78        """
 79
 80        # Change the x/y screen coordinates to grid coordinates
 81        column = int(x // (WIDTH + MARGIN))
 82        row = int(y // (HEIGHT + MARGIN))
 83
 84        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
 85
 86        # Make sure we are on-grid. It is possible to click in the upper right
 87        # corner in the margin and go to a grid location that doesn't exist
 88        if row < ROW_COUNT and column < COLUMN_COUNT:
 89
 90            # Flip the location between 1 and 0.
 91            if self.grid_sprites[row][column].color == arcade.color.WHITE:
 92                self.grid_sprites[row][column].color = arcade.color.GREEN
 93            else:
 94                self.grid_sprites[row][column].color = arcade.color.WHITE
 95
 96
 97def main():
 98    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 99    arcade.run()
100
101
102if __name__ == "__main__":
103    main()