lab_iteration/tile_grid.py

52 lines
1.8 KiB
Python

# tile_grid.py
# ------------
# By MWC Contributors
#
# Implements `draw_tile_grid`, which draws a grid of tiles.
from turtle import *
from tile import fly
def draw_tile_grid(width, height, tile_size, tile_function):
"""Draws a (width x height) grid, with tile_function drawn on each tile.
(draw tile grid creates a grid where width represents how many tiles wide,
heigh represents how many tiles high, tile_size represents the length of 1 side
of the tile, and tile_function represents what is drawn inside the tile like draw_squiggle
from tile.py. The function will draw 1 tile then fly the length of tile_size to move
on to draw the next tile. Then it will return to the beginning of the row by flying
backwards the length of each tile side time the number of tiles in the width. Then the
function will move up to draw the next row by turning and flying to reset. This will
repeat for as many times as the height calls for. At the very end the cursor will move
back to the very beginning by turning and flying the tile_size times the height.)
"""
for y in range(height):
for x in range(width):
tile_function(tile_size)
fly(tile_size)
return_to_x_origin(tile_size, width)
move_up_one_row(tile_size)
return_to_y_origin(tile_size, height)
def return_to_x_origin(tile_size, width):
"After drawing a row of tiles, returns the turtle to the starting x position"
fly(-1 * tile_size * width)
def return_to_y_origin(tile_size, height):
"After drawing all rows of tiles, returns the turtle to the starting y position"
right(90)
fly(tile_size * height)
left(90)
def move_up_one_row(tile_size):
"Moves the turtle up one row"
left(90)
fly(tile_size)
right(90)
done()