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

Function main

src/gamesbyexample/langtonsant.py:46–119  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

44
45
46def main():
47 bext.fg(ANT_COLOR) # The ants' color is the foreground color.
48 bext.bg(WHITE_TILE) # Set the background to white to start.
49 bext.clear()
50
51 # Create a new board data structure:
52 board = {'width': WIDTH, 'height': HEIGHT}
53
54 # Create ant data structures:
55 ants = []
56 for i in range(NUMBER_OF_ANTS):
57 ant = {
58 'x': random.randint(0, WIDTH - 1),
59 'y': random.randint(0, HEIGHT - 1),
60 'direction': random.choice([NORTH, SOUTH, EAST, WEST]),
61 }
62 ants.append(ant)
63
64 # Keep track of which tiles have changed and need to be redrawn on
65 # the screen:
66 changedTiles = []
67
68 while True: # Main program loop.
69 displayBoard(board, ants, changedTiles)
70 changedTiles = []
71
72 # nextBoard is what the board will look like on the next step in
73 # the simulation. Start with a copy of the current step's board:
74 nextBoard = copy.copy(board)
75
76 # Run a single simulation step for each ant:
77 for ant in ants:
78 if board.get((ant['x'], ant['y']), False) == True:
79 nextBoard[(ant['x'], ant['y'])] = False
80 # Turn clockwise:
81 if ant['direction'] == NORTH:
82 ant['direction'] = EAST
83 elif ant['direction'] == EAST:
84 ant['direction'] = SOUTH
85 elif ant['direction'] == SOUTH:
86 ant['direction'] = WEST
87 elif ant['direction'] == WEST:
88 ant['direction'] = NORTH
89 else:
90 nextBoard[(ant['x'], ant['y'])] = True
91 # Turn counter clockwise:
92 if ant['direction'] == NORTH:
93 ant['direction'] = WEST
94 elif ant['direction'] == WEST:
95 ant['direction'] = SOUTH
96 elif ant['direction'] == SOUTH:
97 ant['direction'] = EAST
98 elif ant['direction'] == EAST:
99 ant['direction'] = NORTH
100 changedTiles.append((ant['x'], ant['y']))
101
102 # Move the ant forward in whatever direction it's facing:
103 if ant['direction'] == NORTH:

Callers 1

langtonsant.pyFile · 0.70

Calls 2

clearMethod · 0.80
displayBoardFunction · 0.70

Tested by

no test coverage detected