"""
Array Backed Grid
Show how to use a two-dimensional list/array to back the display of a
grid on-screen.
"""
import arcade
SCREEN_WIDTH = 255
SCREEN_HEIGHT = 255
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 20
HEIGHT = 20
# This sets the margin between each cell
MARGIN = 5
class MyApplication(arcade.Window):
"""
Main application class.
"""
def __init__(self, width, height):
"""
Set up the application.
"""
super().__init__(width, height)
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
self.grid = []
for row in range(10):
# Add an empty array that will hold each cell
# in this row
self.grid.append([])
for column in range(10):
self.grid[row].append(0) # Append a cell
arcade.set_background_color(arcade.color.BLACK)
def on_draw(self):
"""
Render the screen.
"""
# This command has to happen before we start drawing
arcade.start_render()
# Draw the grid
for row in range(10):
for column in range(10):
# Figure out what color to draw the box
if self.grid[row][column] == 1:
color = arcade.color.GREEN
else:
color = arcade.color.WHITE
# Do the math to figure out where the box is
x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2
y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2
# Draw the box
arcade.draw_rectangle_filled(x, y, WIDTH, HEIGHT, color)
def on_mouse_press(self, x, y, button, modifiers):
"""
Called when the user presses a mouse button.
"""
# Change the x/y screen coordinates to grid coordinates
column = x // (WIDTH + MARGIN)
row = y // (HEIGHT + MARGIN)
# Set that location to one
self.grid[row][column] = 1
print("Click coordinates: ({}, {}). Grid coordinates: ({}, {})"
.format(x, y, row, column))
window = MyApplication(SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.run()