Add draw_square and draw_dashed_line with tests

Challenge 1 draws four sides and turns right 90 degrees after each. Challenge 2 puts
the pen down for a dash and up for a gap and leaves it down at the end. Lengths and
counts come from constants.py; invalid values raise ValueError.

Task: MIL-002#3
Task: MIL-002#4
Task: MIL-002#5
This commit is contained in:
2026-10-07 23:58:52 +08:00
parent e2249de46e
commit a34bf6bbc2
6 changed files with 217 additions and 1 deletions
+11 -1
View File
@@ -3,5 +3,15 @@
@brief Solutions to the turtle graphics challenges of day 18 of Udemy's
100 Days of Code.
The challenges are added one milestone at a time; see the README.
The challenges so far are @ref turtle_challenges.square.draw_square and
@ref turtle_challenges.dashed_line.draw_dashed_line. Each takes the turtle as its
first argument, so it can be tested without a window.
"""
from turtle_challenges.dashed_line import draw_dashed_line
from turtle_challenges.square import draw_square
__all__ = [
"draw_dashed_line",
"draw_square",
]
+18
View File
@@ -13,3 +13,21 @@ WINDOW_TITLE = "Turtle Challenges"
## Largest value of one color channel; also the turtle color mode, so that
## colors can be given as RGB tuples from 0 to 255.
COLOR_MODE = 255
## Degrees in a right angle.
RIGHT_ANGLE_DEGREES = 90
## Challenge 1: number of sides of a square.
SQUARE_SIDES = 4
## Challenge 1: length of one side of the square, in turtle units.
SQUARE_SIDE_LENGTH = 100
## Challenge 2: number of dashes in the dashed line.
DASH_COUNT = 50
## Challenge 2: length of one dash, in turtle units.
DASH_LENGTH = 10
## Challenge 2: length of the gap after each dash, in turtle units.
GAP_LENGTH = 10
+40
View File
@@ -0,0 +1,40 @@
"""!
@file dashed_line.py
@brief Turtle Challenge 2: draw a dashed line.
"""
from turtle_challenges.constants import DASH_COUNT, DASH_LENGTH, GAP_LENGTH
from turtle_challenges.pen import Pen
def draw_dashed_line(
pen: Pen,
dash_count: int = DASH_COUNT,
dash_length: float = DASH_LENGTH,
gap_length: float = GAP_LENGTH,
) -> None:
"""!
@brief Draw a line of dashes: pen down for a dash, pen up for a gap.
The pen is down again when the function returns, so that the caller can
keep drawing.
@param pen The turtle that draws.
@param dash_count How many dashes to draw.
@param dash_length Length of one dash, in turtle units.
@param gap_length Length of the gap after each dash, in turtle units.
@exception ValueError If @p dash_count is negative, or a length is not
positive.
"""
if dash_count < 0:
raise ValueError(f"dash_count must not be negative, got {dash_count}")
if dash_length <= 0 or gap_length <= 0:
raise ValueError(
f"lengths must be positive, got dash {dash_length} and gap {gap_length}"
)
for _ in range(dash_count):
pen.pendown()
pen.forward(dash_length)
pen.penup()
pen.forward(gap_length)
pen.pendown()
+26
View File
@@ -0,0 +1,26 @@
"""!
@file square.py
@brief Turtle Challenge 1: draw a square.
"""
from turtle_challenges.constants import (
RIGHT_ANGLE_DEGREES,
SQUARE_SIDE_LENGTH,
SQUARE_SIDES,
)
from turtle_challenges.pen import Pen
def draw_square(pen: Pen, side_length: float = SQUARE_SIDE_LENGTH) -> None:
"""!
@brief Draw a square: move forward, turn right, four times.
@param pen The turtle that draws.
@param side_length Length of each side, in turtle units.
@exception ValueError If @p side_length is not positive.
"""
if side_length <= 0:
raise ValueError(f"side_length must be positive, got {side_length}")
for _ in range(SQUARE_SIDES):
pen.forward(side_length)
pen.right(RIGHT_ANGLE_DEGREES)