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

Class Painter

src/gamesbyexample/floorpainters.py:94–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

92
93
94class Painter:
95 def __init__(self, color, floor):
96 if color == 'random':
97 color = random.choice(bext.ALL_COLORS)
98
99 self.color = color
100 self.x = random.randint(0, WIDTH - 1)
101 self.y = random.randint(0, HEIGHT - 1)
102 self.floor = floor
103
104 def move(self):
105 """Move the painter, while painting the floor behind them."""
106 possibleMoves = [NORTH, SOUTH, EAST, WEST]
107 # Remove any moves that would move off of the floor:
108 if self.x == 0:
109 possibleMoves.remove(WEST)
110 if self.x == WIDTH - 1:
111 possibleMoves.remove(EAST)
112 if self.y == 0:
113 possibleMoves.remove(NORTH)
114 if self.y == HEIGHT - 1:
115 possibleMoves.remove(SOUTH)
116
117 # Remove any moves that go to a space already painted:
118 if (NORTH in possibleMoves
119 and self.floor[(self.x, self.y - 1)] == self.color):
120 possibleMoves.remove(NORTH)
121 if (SOUTH in possibleMoves
122 and self.floor[(self.x, self.y + 1)] == self.color):
123 possibleMoves.remove(SOUTH)
124 if (WEST in possibleMoves
125 and self.floor[(self.x - 1, self.y)] == self.color):
126 possibleMoves.remove(WEST)
127 if (EAST in possibleMoves
128 and self.floor[(self.x + 1, self.y)] == self.color):
129 possibleMoves.remove(EAST)
130
131 # But if every space is already painted, move anywhere that
132 # isn't off of the floor:
133 if possibleMoves == []:
134 if self.x != 0:
135 possibleMoves.append(WEST)
136 if self.x != WIDTH - 1:
137 possibleMoves.append(EAST)
138 if self.y != 0:
139 possibleMoves.append(NORTH)
140 if self.y != HEIGHT - 1:
141 possibleMoves.append(SOUTH)
142
143 move = random.choice(possibleMoves)
144 bext.bg(self.color)
145
146 # Move the painter:
147 if move == NORTH:
148 self.y -= 1
149 elif move == SOUTH:
150 self.y += 1
151 elif move == WEST:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected