generated from mwc/lab_iteration
55 lines
1.7 KiB
Python
55 lines
1.7 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.
|
|
|
|
(The width, height, and tile_size are input when running the program
|
|
from the terminal. The tile_function determines the pattern drawn on the
|
|
tile, and when the tile_function argument is called, it passes and runs
|
|
the function draw_tile from tile.py, because it is the last function it
|
|
encountered...?
|
|
|
|
The "for y in range(height):" calls a loop determined by the the number of
|
|
rows indicated by the height. The "for x in range(width):" calls a loop
|
|
determined by the number of columns indicated by the width. The program
|
|
draws a complete row, then stops drawing and returns to the horizontal
|
|
starting point and moves up one tile length. It returns to the origin at
|
|
the very end.)
|
|
|
|
"""
|
|
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)
|
|
|
|
|
|
|
|
|
|
|