Files
lab_iteration/tile_grid.py
mbhatti4 68400dfa65 Yes! I think once we start coding longer programs, it would be super helpful for me to use docstrings to explain to myself what each function does.
If you have loads of functions, they can start not making sense if you are just looking at the code.
2025-09-16 19:21:20 -04:00

48 lines
1.4 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.
This code draws the grid of the tiles. It takes the width, height and tile size and creates a grid that matches.
Everytime it finishes a tile, it goes back the origin of the square to then continue to the next tile.
Once it finishes the row, it goes back to the origin of the first tile to then build up the next row.
At the end of drawing all the tiles, it moves the pen back to the origin.
"""
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)