28. Array-Backed Grids

28.1. Introduction

Games like minesweeper, tic-tac-toe, and many types of adventure games keep data for the game in a grid of numbers. For example, a tic-tac-toe board:





… can use a grid of numbers to represent the empty spots, the O’s and the X’s like this:










This grid of numbers can also be called a two-dimensional array or a matrix. (Finally, we get to learn about The Matrix.) The values of the numbers in the grid represent what should be displayed at each board location. In the prior example, 0 represents a spot where no one has played, a 1 represents an X, and a 2 represents an O.


Figure 16.1: Minesweeper game, showing the backing grid of numbers

The figure above is an example from the classic minesweeper game. This example has been modified to show both the classic display on the left, and the grid of numbers used to display the board on the right.

The number 10 represents a mine, the number 0 represents a space that has not been clicked, and the number 9 represents a cleared space. The numbers 1 to 8 represent how many mines are within the surrounding eight squares, and is only filled in when the user clicks on the square.

Minesweeper can actually have two grids. One for the regular display, and a completely separate grid of numbers that will track if the user has placed “flags” on the board marking where she thinks the mines are.

Classic adventure game maps are created using a tiled map editor. These are huge grids where each location is simply a number representing the type of terrain that goes there. The terrain could be things like dirt, a road, a path, green grass, brown grass, and so forth. Programs like Tiled shown in the figure below allow a developer to easily make these maps and write the grid to disk.


Figure 16.2: Using Tiled Map Editor to create an adventure map

Adventure games also use multiple grids of numbers, just like minesweeper has a grid for the mines, and a separate grid for the flags. One grid, or “layer,” in the adventure game represents terrain you can walk on; another for things you can’t walk on like walls and trees; a layer for things that can instantly kill you, like lava or bottomless pits; one for objects that can be picked up and moved around; and yet another layer for initial placement of monsters.

Maps like these can be loaded in a Python program, which is shown in more detail with the Python Arcade Library documentation.

28.2. Application

Enough talk, let’s write some code. This example will create a grid that will trigger if we display a white or green block. We can change the grid value and make it green by clicking on it. This is a first step to a grid-based game like minesweeper, battleship, connect four, etc. (One year I had a student call me over and she had modified a program like this to show my name in flashing lights. That was … disturbing. So please use this knowledge only for good!)

Start with this template:

 1import arcade
 7class MyGame(arcade.Window):
 8    """
 9    Main application class.
10    """
12    def __init__(self, width, height):
13        super().__init__(width, height)
15        arcade.set_background_color(arcade.color.WHITE)
17    def on_draw(self):
18        """
19        Render the screen.
20        """
22        arcade.start_render()
24    def on_mouse_press(self, x, y, button, key_modifiers):
25        """
26        Called when the user presses a mouse button.
27        """
28        pass
31def main():
33    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
34    arcade.run()
37if __name__ == "__main__":
38    main()

Starting with the file above, attempt to recreate this program following the instructions here. The final program is at the end of this chapter but don’t skip ahead and copy it! If you do that you’ll have learned nothing. Anyone can copy and paste the code, but if you can recreate this program you have skills people are willing to pay for. If you can only copy and paste, you’ve wasted your time here.

28.2.1. Drawing the Grid

  1. Create variables named WIDTH, HEIGHT, and MARGIN. Set the width and height to 20. This will represent how large each grid location is. Set the margin to 5. This represents the margin between each grid location and the edges of the screen. Create these variables at the top of the program, after the import statements. Also create variables ROW_COUNT and COLUMN_COUNT. Set them to 10. This will control how many rows and columns we will have.

  2. Calculate SCREEN_WIDTH and SCREEN_HEIGHT based on the variables we created above. If we have 10 rows, and each row is 20 high, that’s 200 pixels. If we have 10 rows, that’s also 11 margins. (Nine between the cells and two on each edge.) That is 55 more pixels for a total of 255. Write the equation so it works with whatever we select in the constants created by step 1.

  3. Change the background to black. Draw a white box in the lower-left corner. Draw the box drawn using the height and width variables created earlier. (Feel free to adjust the colors.) Use the draw_rectangle_filled function. You will need to center the rectangle not at (0, 0) but at a coordinate that takes into account the height and width of the rectangle, such as \(\frac{width}{2}\). When you get done your program’s window should look like:


Figure 16.3: Step 3

  1. Use a for loop to draw COLUMN_COUNT boxes in a row. Use column for the variable name in the for loop. The output will look like one long box until we add in the margin between boxes. See Figure 16.4.


Figure 16.4: Step 4

  1. Adjust the drawing of the rectangle to add in the MARGIN variable. Now there should be gaps between the rectangles. See Figure 16.5.


Figure 16.5: Step 5

  1. Add the margin before drawing the rectangles, in addition to between each rectangle. This should keep the box from appearing right next to the window edge. See Figure 16.6. You’ll end up with an equation like: \((margin+width)\cdot column+margin+\frac{width}{2}\)


Figure 16.6: Step 6

  1. Add another for loop that also will loop for each row. Call the variable in this for loop row. Now we should have a full grid of boxes. See Figure 16.7.


Figure 16.7: Step 7

28.2.2. Populating the Grid

  1. Now we need to create a two-dimensional array. We need to create this once, at program start-up. So this will go in the program’s __init__ method. Creating a two-dimensional array in Python is, unfortunately, not as easy as it is in some other computer languages. There are some libraries that can be downloaded for Python that make it easy (like numpy), but for this example they will not be used. To create a two-dimensional array and set an example, use the code below:

Create a 10x10 array of numbers

# --- Create grid of numbers
# Create an empty list
self.grid = []
# Loop for each row
for row in range(ROW_COUNT):
    # For each row, create a list that will
    # represent an entire row
    # Loop for each column
    for column in range(COLUMN_COUNT):
        # Add a the number zero to the current row

A much shorter example is below, but this example uses some odd parts of Python that I don’t bother to explain in this book:

Create a 10x10 array of numbers
self.grid = [[0 for x in range(10)] for y in range(10)]

Use one of these two examples and place the code to create our array ahead of your main program loop.

  1. Set an example location in the array to 1.

Two dimensional arrays are usually represented addressed by first their row, and then the column. This is called a row-major storage. Most languages use row-major storage, with the exception of Fortran and MATLAB. Fortran and MATLAB use column-major storage.

# Set row 1, column 5 to one
self.grid[1][5] = 1

Place this code somewhere ahead of your main program loop.

  1. Select the color of the rectangle based on the value of a variable named color. Do this by first finding the line of code where the rectangle is drawn. Ahead of it, create a variable named color and set it equal to white. Then replace the white color in the rectangle declaration with the color variable.

  2. Select the color based on the value in the grid. After setting color to white, place an if statement that looks at the value in grid[row][column] and changes the color to green if the grid value is equal to 1. There should now be one green square. See Figure 16.8.


Figure 16.8: Step 11

  1. Print “click” to the screen if the user clicks the mouse button. See Mouse Clicks if you’ve forgotten how to do that.

  2. Print the mouse coordinates when the user clicks the mouse.

  3. Convert the mouse coordinates into grid coordinates. Print those instead. Remember to use the width and height of each grid location combined with the margin. It will be necessary to convert the final value to an integer. This can be done by using int or by using the integer division operator // instead of the normal division operator /. See Figure 16.9.


Figure 16.9: Step 14

  1. Set the grid location at the row/column clicked to 1. See Figure 16.10.


Figure 16.10: Step 15

28.2.3. Resulting Program

  2Array Backed Grid
  4Show how to use a two-dimensional list/array to back the display of a
  5grid on-screen.
  7import arcade
  9# Set how many rows and columns we will have
 10ROW_COUNT = 10
 13# This sets the WIDTH and HEIGHT of each grid location
 14WIDTH = 20
 15HEIGHT = 20
 17# This sets the margin between each cell
 18# and on the edges of the screen.
 19MARGIN = 5
 21# Do the math to figure out our screen dimensions
 26class MyGame(arcade.Window):
 27    """
 28    Main application class.
 29    """
 31    def __init__(self, width, height):
 32        """
 33        Set up the application.
 34        """
 35        super().__init__(width, height)
 36        # Create a 2 dimensional array. A two dimensional
 37        # array is simply a list of lists.
 38        self.grid = []
 39        for row in range(ROW_COUNT):
 40            # Add an empty array that will hold each cell
 41            # in this row
 42            self.grid.append([])
 43            for column in range(COLUMN_COUNT):
 44                self.grid[row].append(0)  # Append a cell
 46        arcade.set_background_color(arcade.color.BLACK)
 48    def on_draw(self):
 49        """
 50        Render the screen.
 51        """
 53        # This command has to happen before we start drawing
 54        arcade.start_render()
 56        # Draw the grid
 57        for row in range(ROW_COUNT):
 58            for column in range(COLUMN_COUNT):
 59                # Figure out what color to draw the box
 60                if self.grid[row][column] == 1:
 61                    color = arcade.color.GREEN
 62                else:
 63                    color = arcade.color.WHITE
 65                # Do the math to figure out where the box is
 66                x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2
 67                y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2
 69                # Draw the box
 70                arcade.draw_rectangle_filled(x, y, WIDTH, HEIGHT, color)
 72    def on_mouse_press(self, x, y, button, modifiers):
 73        """
 74        Called when the user presses a mouse button.
 75        """
 77        # Change the x/y screen coordinates to grid coordinates
 78        column = x // (WIDTH + MARGIN)
 79        row = y // (HEIGHT + MARGIN)
 81        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")
 83        # Make sure we are on-grid. It is possible to click in the upper right
 84        # corner in the margin and go to a grid location that doesn't exist
 85        if row < ROW_COUNT and column < COLUMN_COUNT:
 87            # Flip the location between 1 and 0.
 88            if self.grid[row][column] == 0:
 89                self.grid[row][column] = 1
 90            else:
 91                self.grid[row][column] = 0
 94def main():
 96    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
 97    arcade.run()
100if __name__ == "__main__":
101    main()