Grid Using Sprites v2

You may also want to look at:
Array-Backed Grid - very slow but simple, uses drawing commands
Array-Backed Grid Buffered - slow and uses buffered shapes
Grid Using Sprites v1 - super-fast and uses sprites. Resyncs to number grid in one function call
Grid Using Sprites v2 - (This program) super-fast and uses sprites. Keeps a second 2D grid of sprites to match 2D grid of numbers
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"""
15import arcade
16
17# Set how many rows and columns we will have
18ROW_COUNT = 15
19COLUMN_COUNT = 15
20
21# This sets the WIDTH and HEIGHT of each grid location
22WIDTH = 30
23HEIGHT = 30
24
25# This sets the margin between each cell
26# and on the edges of the screen.
27MARGIN = 5
28
29# Do the math to figure out our screen dimensions
30WINDOW_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
31WINDOW_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
32WINDOW_TITLE = "Array Backed Grid Buffered Example"
33
34
35class GameView(arcade.View):
36 """
37 Main application class.
38 """
39
40 def __init__(self):
41 """
42 Set up the application.
43 """
44 super().__init__()
45
46 # Set the background color of the window
47 self.background_color = arcade.color.BLACK
48
49 # One dimensional list of all sprites in the two-dimensional sprite list
50 self.grid_sprite_list = arcade.SpriteList()
51
52 # This will be a two-dimensional grid of sprites to mirror the two
53 # dimensional grid of numbers. This points to the SAME sprites that are
54 # in grid_sprite_list, just in a 2d manner.
55 self.grid_sprites = []
56
57 # Create a list of solid-color sprites to represent each grid location
58 for row in range(ROW_COUNT):
59 self.grid_sprites.append([])
60 for column in range(COLUMN_COUNT):
61 x = column * (WIDTH + MARGIN) + (WIDTH / 2 + MARGIN)
62 y = row * (HEIGHT + MARGIN) + (HEIGHT / 2 + MARGIN)
63 sprite = arcade.SpriteSolidColor(WIDTH, HEIGHT, color=arcade.color.WHITE)
64 sprite.center_x = x
65 sprite.center_y = y
66 self.grid_sprite_list.append(sprite)
67 self.grid_sprites[row].append(sprite)
68
69 def on_draw(self):
70 """
71 Render the screen.
72 """
73 # We should always start by clearing the window pixels
74 self.clear()
75
76 # Batch draw the grid sprites
77 self.grid_sprite_list.draw()
78
79 def on_mouse_press(self, x, y, button, modifiers):
80 """
81 Called when the user presses a mouse button.
82 """
83
84 # Convert the clicked mouse position into grid coordinates
85 column = int(x // (WIDTH + MARGIN))
86 row = int(y // (HEIGHT + MARGIN))
87
88 print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
89
90 # Make sure we are on-grid. It is possible to click in the upper right
91 # corner in the margin and go to a grid location that doesn't exist
92 if row >= ROW_COUNT or column >= COLUMN_COUNT:
93 # Simply return from this method since nothing needs updating
94 return
95
96 # Flip the color of the sprite
97 if self.grid_sprites[row][column].color == arcade.color.WHITE:
98 self.grid_sprites[row][column].color = arcade.color.GREEN
99 else:
100 self.grid_sprites[row][column].color = arcade.color.WHITE
101
102
103def main():
104 """ Main function """
105 # Create a window class. This is what actually shows up on screen
106 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
107
108 # Create the GameView
109 game = GameView()
110
111 # Show GameView on screen
112 window.show_view(game)
113
114 # Start the arcade game loop
115 arcade.run()
116
117
118if __name__ == "__main__":
119 main()