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. Instead of
  8iterating all the cells when the grid changes we simply just
  9swap the color of the selected sprite. This means this version
 10can handle very large grids and still have the same performance.
 11
 12If Python and Arcade are installed, this example can be run from the command line with:
 13python -m arcade.examples.array_backed_grid_sprites_2
 14"""
 15from __future__ import annotations
 16
 17import arcade
 18
 19# Set how many rows and columns we will have
 20ROW_COUNT = 15
 21COLUMN_COUNT = 15
 22
 23# This sets the WIDTH and HEIGHT of each grid location
 24WIDTH = 30
 25HEIGHT = 30
 26
 27# This sets the margin between each cell
 28# and on the edges of the screen.
 29MARGIN = 5
 30
 31# Do the math to figure out our screen dimensions
 32SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
 33SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
 34SCREEN_TITLE = "Array Backed Grid Buffered Example"
 35
 36
 37class MyGame(arcade.Window):
 38    """
 39    Main application class.
 40    """
 41
 42    def __init__(self, width, height, title):
 43        """
 44        Set up the application.
 45        """
 46        super().__init__(width, height, title)
 47
 48        # Set the background color of the window
 49        self.background_color = arcade.color.BLACK
 50
 51        # One dimensional list of all sprites in the two-dimensional sprite list
 52        self.grid_sprite_list = arcade.SpriteList()
 53
 54        # This will be a two-dimensional grid of sprites to mirror the two
 55        # dimensional grid of numbers. This points to the SAME sprites that are
 56        # in grid_sprite_list, just in a 2d manner.
 57        self.grid_sprites = []
 58
 59        # Create a list of solid-color sprites to represent each grid location
 60        for row in range(ROW_COUNT):
 61            self.grid_sprites.append([])
 62            for column in range(COLUMN_COUNT):
 63                x = column * (WIDTH + MARGIN) + (WIDTH / 2 + MARGIN)
 64                y = row * (HEIGHT + MARGIN) + (HEIGHT / 2 + MARGIN)
 65                sprite = arcade.SpriteSolidColor(WIDTH, HEIGHT, color=arcade.color.WHITE)
 66                sprite.center_x = x
 67                sprite.center_y = y
 68                self.grid_sprite_list.append(sprite)
 69                self.grid_sprites[row].append(sprite)
 70
 71    def on_draw(self):
 72        """
 73        Render the screen.
 74        """
 75        # We should always start by clearing the window pixels
 76        self.clear()
 77
 78        # Batch draw the grid sprites
 79        self.grid_sprite_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        # Convert the clicked mouse position into 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 or column >= COLUMN_COUNT:
 95            # Simply return from this method since nothing needs updating
 96            return
 97
 98        # Flip the color of the sprite
 99        if self.grid_sprites[row][column].color == arcade.color.WHITE:
100            self.grid_sprites[row][column].color = arcade.color.GREEN
101        else:
102            self.grid_sprites[row][column].color = arcade.color.WHITE
103
104
105def main():
106    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
107    arcade.run()
108
109
110if __name__ == "__main__":
111    main()