MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / displayBoard

Function displayBoard

src/gamesbyexample/langtonsant.py:122–157  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

120
121
122def 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:

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected