def inside_grid(ball_position, grid):
return ball_position[0] >= 0 \
and ball_position[1] >= 0 \
and ball_position[0] <= grid.shape[0] - 1 \
and ball_position[1] <= grid.shape[1] - 1
def move_monster(monster_position, grid):
while True:
move_directions = [(0,1), (1, 0), (-1, 0), (0, -1)] # try changing this!
move = random.choice(move_directions)
next_position = add_position(monster_position, move)
if inside_grid(next_position, grid):
break
return next_position
def add_position(pos1, pos2):
"""
Add a position and a move together:
add_position( (2,3), (0,1) ) == (2,4)
"""
return (pos1[0] + pos2[0], pos1[1] + pos2[1])
monster_position = (randint(1, grid.shape[0]), randint(1, grid.shape[1]))
grid = BlockGrid(41, 10, block_size=30, lines_on=True)
eaten_positions = []
# Try changing the colors!
monster_color = colors['DarkOrange']
grid_color = colors['Purple']
eaten_color = colors['White']
for i in range(100):
# Add the monster position to the list of eaten positions
eaten_positions.append(monster_position)
monster_position = move_monster(monster_position, grid)
for block in grid:
block_coordinates = (block.col, block.row)
if block_coordinates == monster_position:
block.rgb = monster_color
elif block_coordinates in eaten_positions:
block.rgb = eaten_color
else:
block.rgb = grid_color
grid.flash()
grid.show()