Displays the board and ants on the screen. The changedTiles argument is a list of (x, y) tuples for tiles on the screen that have changed and need to be redrawn.
(board, ants, changedTiles)
| 120 | |
| 121 | |
| 122 | def displayBoard(board, ants, changedTiles): |
| 123 | """Displays the board and ants on the screen. The changedTiles |
| 124 | argument is a list of (x, y) tuples for tiles on the screen that |
| 125 | have changed and need to be redrawn.""" |
| 126 | |
| 127 | # Draw the board data structure: |
| 128 | for x, y in changedTiles: |
| 129 | bext.goto(x, y) |
| 130 | if board.get((x, y), False): |
| 131 | bext.bg(BLACK_TILE) |
| 132 | else: |
| 133 | bext.bg(WHITE_TILE) |
| 134 | |
| 135 | antIsHere = False |
| 136 | for ant in ants: |
| 137 | if (x, y) == (ant['x'], ant['y']): |
| 138 | antIsHere = True |
| 139 | if ant['direction'] == NORTH: |
| 140 | print(ANT_UP, end='') |
| 141 | elif ant['direction'] == SOUTH: |
| 142 | print(ANT_DOWN, end='') |
| 143 | elif ant['direction'] == EAST: |
| 144 | print(ANT_LEFT, end='') |
| 145 | elif ant['direction'] == WEST: |
| 146 | print(ANT_RIGHT, end='') |
| 147 | break |
| 148 | if not antIsHere: |
| 149 | print(' ', end='') |
| 150 | |
| 151 | # Display the quit message at the bottom of the screen: |
| 152 | bext.goto(0, HEIGHT) |
| 153 | bext.bg(WHITE_TILE) |
| 154 | print('Press Ctrl-C to quit.', end='') |
| 155 | |
| 156 | sys.stdout.flush() # (Required for bext-using programs.) |
| 157 | time.sleep(PAUSE_AMOUNT) |
| 158 | |
| 159 | |
| 160 | # If this program was run (instead of imported), run the game: |