Major updates
This commit is contained in:
parent
485f37565f
commit
0172850749
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------
|
||||||
|
# Write your entire commit message above this line.
|
||||||
|
#
|
||||||
|
# The first line should be a quick description of what you changed.
|
||||||
|
# Then leave a blank line.
|
||||||
|
# Then, taking as many lines as you want, answer the questions
|
||||||
|
# corresponding to your checkpoint.
|
||||||
|
#
|
||||||
|
# Checkpoint 1:
|
||||||
|
# - Was it difficult to figure out how to rewrite square() using
|
||||||
|
# a for-loop? What strategies did you use to figure it out?
|
||||||
|
#
|
||||||
|
# Checkpoint 2:
|
||||||
|
# - Describe something you understand well, or find interesting, about ranges.
|
||||||
|
# - Describe something you're still unsure about related to ranges.
|
||||||
|
#
|
||||||
|
# Checkpoint 3:
|
||||||
|
# - Docstrings are meant for humans to read; they don't affect how a program runs.
|
||||||
|
# When you write your own programs in the future, do you think you'll write
|
||||||
|
# docstrings? Why or why not?
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
# drawtiles.py
|
||||||
|
# ------------
|
||||||
|
# By MWC Contributors
|
||||||
|
#
|
||||||
|
# Provides a command-line interface for drawing a grid of tiles.
|
||||||
|
# Run `python drawtiles.py --help` for usage instructions.
|
||||||
|
|
||||||
|
from tile import draw_tile
|
||||||
|
from tile_grid import draw_tile_grid
|
||||||
|
from superturtle.movement import no_delay
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
parser = ArgumentParser("python drawtiles.py", description="Draws a grid of tiles.")
|
||||||
|
parser.add_argument("width", type=int, help="How many tiles across the grid should be")
|
||||||
|
parser.add_argument("height", type=int, help="How many tiles high the grid should be")
|
||||||
|
parser.add_argument("size", type=int, help="Side length of each tile")
|
||||||
|
parser.add_argument("--fast", action="store_true", help="Skip turtle animation and show the result")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.fast:
|
||||||
|
with no_delay():
|
||||||
|
draw_tile_grid(args.width, args.height, args.size, draw_tile)
|
||||||
|
else:
|
||||||
|
draw_tile_grid(args.width, args.height, args.size, draw_tile)
|
||||||
|
input()
|
||||||
|
|
25
tile.py
25
tile.py
|
@ -2,17 +2,23 @@ from turtle import *
|
||||||
|
|
||||||
def draw_tile(size):
|
def draw_tile(size):
|
||||||
"Draws one tile, which can be repeated to form a pattern."
|
"Draws one tile, which can be repeated to form a pattern."
|
||||||
color("grey")
|
draw_tile_outline(size)
|
||||||
|
draw_squiggle(size)
|
||||||
|
|
||||||
|
def draw_tile_outline(size):
|
||||||
|
pencolor("#dddddd")
|
||||||
square(size)
|
square(size)
|
||||||
forward(size/2)
|
|
||||||
color("black")
|
def draw_squiggle(size):
|
||||||
|
forward(size/4)
|
||||||
|
pencolor("black")
|
||||||
left(90)
|
left(90)
|
||||||
quarter_arc_right(size/4)
|
quarter_arc_right(size/4)
|
||||||
quarter_arc_left(size/4)
|
quarter_arc_left(size/4)
|
||||||
quarter_arc_left(size/4)
|
quarter_arc_left(size/4)
|
||||||
quarter_arc_right(size/4)
|
quarter_arc_right(size/4)
|
||||||
left(90)
|
left(90)
|
||||||
fly(size/2)
|
fly(size/4)
|
||||||
left(90)
|
left(90)
|
||||||
fly(size)
|
fly(size)
|
||||||
left(90)
|
left(90)
|
||||||
|
@ -25,14 +31,9 @@ def fly(distance):
|
||||||
|
|
||||||
def square(size):
|
def square(size):
|
||||||
"Draws a square of side length `size`"
|
"Draws a square of side length `size`"
|
||||||
forward(size)
|
for side in range(4):
|
||||||
left(90)
|
forward(size)
|
||||||
forward(size)
|
left(90)
|
||||||
left(90)
|
|
||||||
forward(size)
|
|
||||||
left(90)
|
|
||||||
forward(size)
|
|
||||||
left(90)
|
|
||||||
|
|
||||||
def quarter_arc_right(radius):
|
def quarter_arc_right(radius):
|
||||||
"Draws a quarter of an arc, turning to the right."
|
"Draws a quarter of an arc, turning to the right."
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
from tile import draw_tile
|
|
||||||
from tile_grid import draw_tile_grid
|
|
||||||
from superturtle.motion import no_delay
|
|
||||||
|
|
||||||
width = 10
|
|
||||||
height = 8
|
|
||||||
size = 10
|
|
||||||
|
|
||||||
draw_tile_grid(width, height, size, draw_tile)
|
|
||||||
|
|
21
tile_grid.py
21
tile_grid.py
|
@ -1,4 +1,11 @@
|
||||||
|
# tile_grid.py
|
||||||
|
# ------------
|
||||||
|
# By MWC Contributors
|
||||||
|
#
|
||||||
|
# Implements `draw_tile_grid`, which draws a grid of tiles.
|
||||||
|
|
||||||
from turtle import *
|
from turtle import *
|
||||||
|
from tile import fly
|
||||||
|
|
||||||
def draw_tile_grid(width, height, tile_size, tile_function):
|
def draw_tile_grid(width, height, tile_size, tile_function):
|
||||||
"""Draws a (width x height) grid, with tile_function drawn on each tile.
|
"""Draws a (width x height) grid, with tile_function drawn on each tile.
|
||||||
|
@ -8,32 +15,26 @@ def draw_tile_grid(width, height, tile_size, tile_function):
|
||||||
for y in range(height):
|
for y in range(height):
|
||||||
for x in range(width):
|
for x in range(width):
|
||||||
tile_function(tile_size)
|
tile_function(tile_size)
|
||||||
forward(tile_size)
|
fly(tile_size)
|
||||||
return_to_x_origin(tile_size, width)
|
return_to_x_origin(tile_size, width)
|
||||||
move_up_one_row(tile_size)
|
move_up_one_row(tile_size)
|
||||||
return_to_y_origin(tile_size, height)
|
return_to_y_origin(tile_size, height)
|
||||||
|
|
||||||
def return_to_x_origin(tile_size, width):
|
def return_to_x_origin(tile_size, width):
|
||||||
"After drawing a row of tiles, returns the turtle to the starting x position"
|
"After drawing a row of tiles, returns the turtle to the starting x position"
|
||||||
penup()
|
fly(-1 * tile_size * width)
|
||||||
back(tile_size * width)
|
|
||||||
pendown()
|
|
||||||
|
|
||||||
def return_to_y_origin(tile_size, height):
|
def return_to_y_origin(tile_size, height):
|
||||||
"After drawing all rows of tiles, returns the turtle to the starting y position"
|
"After drawing all rows of tiles, returns the turtle to the starting y position"
|
||||||
penup()
|
|
||||||
right(90)
|
right(90)
|
||||||
forward(tile_size * height)
|
fly(tile_size * height)
|
||||||
left(90)
|
left(90)
|
||||||
pendown()
|
|
||||||
|
|
||||||
def move_up_one_row(tile_size):
|
def move_up_one_row(tile_size):
|
||||||
"Moves the turtle up one row"
|
"Moves the turtle up one row"
|
||||||
penup()
|
|
||||||
left(90)
|
left(90)
|
||||||
forward(tile_size)
|
fly(tile_size)
|
||||||
right(90)
|
right(90)
|
||||||
pendown()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue