generated from mwc/lab_iteration
Initial commit
This commit is contained in:
commit
20c7fd5197
|
@ -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,2 @@
|
|||
.DS_Store
|
||||
__pycache__
|
|
@ -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()
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# This file is automatically @generated by Poetry 2.0.0 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "superturtle"
|
||||
version = "0.2.0"
|
||||
description = "Extensions to Python's turtle"
|
||||
optional = false
|
||||
python-versions = "<4.0,>=3.9"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "superturtle-0.2.0-py3-none-any.whl", hash = "sha256:ca3a31be3259387b4490846adbf64502acc9d23472912cc43497ab170e89f506"},
|
||||
{file = "superturtle-0.2.0.tar.gz", hash = "sha256:807fb419c1dba9cb809a22a68e72c0193bdeed4a9326eb36ad940b2a7ff6ac04"},
|
||||
]
|
||||
|
||||
[metadata]
|
||||
lock-version = "2.1"
|
||||
python-versions = ">=3.10,<4.0"
|
||||
content-hash = "6aad436bbbf760fa856344262eab22d62a167cac4e5dfefbf4be77d5a37428c9"
|
|
@ -0,0 +1,21 @@
|
|||
[project]
|
||||
name = "lab-iteration"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = [
|
||||
{name = "Chris Proctor",email = "chris@chrisproctor.net"}
|
||||
]
|
||||
license = {text = "MIT"}
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10,<4.0"
|
||||
dependencies = [
|
||||
"superturtle (>=0.2.0,<0.3.0)"
|
||||
]
|
||||
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry]
|
||||
package-mode = false
|
|
@ -0,0 +1,31 @@
|
|||
# ranges.py
|
||||
# ---------
|
||||
# By MWC Contributors
|
||||
|
||||
def print_all_numbers(maximum):
|
||||
"Prints all integers from 0 to maximum."
|
||||
for number in range(maximum):
|
||||
print(number)
|
||||
|
||||
def print_even_numbers(maximum):
|
||||
"Prints all even integers from 0 to maximum."
|
||||
pass
|
||||
|
||||
def print_odd_numbers(maximum):
|
||||
"Prints all odd integers from 0 to maximum."
|
||||
pass
|
||||
|
||||
def print_multiples_of_five(maximum):
|
||||
"Prints all integers which are multiples of five from 0 to maximum."
|
||||
pass
|
||||
|
||||
chosen_maximum = int(input("Choose a number: "))
|
||||
print(f"All numbers from 0 to {chosen_maximum}")
|
||||
print_all_numbers(chosen_maximum)
|
||||
print(f"All even numbers from 0 to {chosen_maximum}")
|
||||
print_even_numbers(chosen_maximum)
|
||||
print(f"All odd numbers from 0 to {chosen_maximum}")
|
||||
print_odd_numbers(chosen_maximum)
|
||||
print(f"All multiples of 5 from 0 to {chosen_maximum}")
|
||||
print_multiples_of_five(chosen_maximum)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
from turtle import *
|
||||
|
||||
def square(side_length):
|
||||
forward(side_length)
|
||||
right(90)
|
||||
forward(side_length)
|
||||
right(90)
|
||||
forward(side_length)
|
||||
right(90)
|
||||
forward(side_length)
|
||||
right(90)
|
||||
|
||||
sizes = [20, 40, 60, 80, 100]
|
||||
for size in sizes:
|
||||
square(size)
|
||||
input()
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
from turtle import *
|
||||
|
||||
def draw_tile(size):
|
||||
"Draws one tile, which can be repeated to form a pattern."
|
||||
draw_tile_outline(size)
|
||||
draw_squiggle(size)
|
||||
|
||||
def draw_tile_outline(size):
|
||||
pencolor("#dddddd")
|
||||
square(size)
|
||||
|
||||
def draw_squiggle(size):
|
||||
forward(size/4)
|
||||
pencolor("black")
|
||||
left(90)
|
||||
quarter_arc_right(size/4)
|
||||
quarter_arc_left(size/4)
|
||||
quarter_arc_left(size/4)
|
||||
quarter_arc_right(size/4)
|
||||
left(90)
|
||||
fly(size/4)
|
||||
left(90)
|
||||
fly(size)
|
||||
left(90)
|
||||
|
||||
def fly(distance):
|
||||
"Moves without drawing."
|
||||
penup()
|
||||
forward(distance)
|
||||
pendown()
|
||||
|
||||
def square(size):
|
||||
"Draws a square of side length `size`"
|
||||
for side in range(4):
|
||||
forward(size)
|
||||
left(90)
|
||||
|
||||
def quarter_arc_right(radius):
|
||||
"Draws a quarter of an arc, turning to the right."
|
||||
circle(-radius, 90)
|
||||
|
||||
def quarter_arc_left(radius):
|
||||
"Draws a quarter of an arc, turning to the left."
|
||||
circle(radius, 90)
|
|
@ -0,0 +1,42 @@
|
|||
# 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.
|
||||
|
||||
(Your explanation here.)
|
||||
"""
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue