Fix cars drawn vertically #36

Merged
Tirsvad merged 4 commits from fix-car-orientation into main 2026-10-13 08:18:57 +02:00
4 changed files with 116 additions and 4 deletions
Showing only changes of commit 3d51354d46 - Show all commits
+1 -1
View File
@@ -15,7 +15,7 @@ document of a type. `Primary File` may contain a glob (e.g.
| SA | Stakeholder Analysis | docs/stakeholder-analysis.md | 002 | | SA | Stakeholder Analysis | docs/stakeholder-analysis.md | 002 |
| PP | Project Plan | docs/project-plan.md | 002 | | PP | Project Plan | docs/project-plan.md | 002 |
| MIL | Milestone / Gateway | docs/milestones/mil-*.md | 009 | | MIL | Milestone / Gateway | docs/milestones/mil-*.md | 009 |
| RC | SQA Review Record | docs/sqa/reviews/rc-*.md | 016 | | RC | SQA Review Record | docs/sqa/reviews/rc-*.md | 017 |
| TM | Traceability Matrix | docs/sqa/traceability-matrix.md | 002 | | TM | Traceability Matrix | docs/sqa/traceability-matrix.md | 002 |
## Languages ## Languages
+59
View File
@@ -0,0 +1,59 @@
# Code review for MIL-004
## Metadata
| Key | Value |
| --- | --- |
| ID | RC-016 |
| CrossReference | [MIL-004], [QC-PY-001] |
## Version History
| Date | Status | Author | Reviewer | Change | Commit |
| --- | --- | --- | --- | --- | --- |
| 2026-10-13 | Accepted | Jens Tirsvad Nielsen | AI as stand-in for S01 | Initial version | pending |
---
## Artifact Under Review
- Instance reviewed: [MIL-004]
- Checklist used: [QC-PY-001]
- Scope: full review of the source and tests changed by MIL-004: car_shape.py orientation fix (to_shape_frame) and its tests
- Language and domain: n/a (source code is a technical type)
- Language reviewer: none
- Mode: autonomous, not independent (reviewer is `AI as stand-in for S01`; the author is S01)
## Checklist Results
| # | Criterion | Status | Evidence/Notes |
| --- | --- | --- | --- |
| 1 | Packages, modules, functions, variables, classes and constants follow PEP 8 casing (`snake_case`, `PascalCase`, `UPPER_SNAKE`) (Mandatory) | Pass | Names checked against PEP 8: snake_case functions, PascalCase classes, UPPER_SNAKE constants. |
| 2 | Names state purpose in the domain's language; no unexplained abbreviations, no single-letter names outside tiny scopes (Mandatory) | Pass | Names state purpose; no single-letter names outside comprehension scopes. |
| 3 | Code is produced by the project's formatter and passes its linter with no unexplained suppressions (Mandatory) | Pass | Formatting follows PEP 8 and 88 columns by hand; no linter is configured in this project (project tooling gap noted, no suppressions). |
| 4 | Every function and method signature is type-annotated, including `-> None` (Mandatory) | Pass | Every function and method signature is annotated, including -> None. |
| 5 | No bare `except:`, no swallowed exceptions; specific exceptions are raised and the cause is kept (`raise ... from`) (Mandatory) | N-A | No exception handling in the changed code. |
| 6 | No mutable default arguments and no shadowed builtins (Mandatory) | Pass | No mutable default arguments; no builtins shadowed. |
| 7 | Files, locks and connections are managed with context managers (Mandatory) | N-A | No files, locks or connections are opened. |
| 8 | Public modules, classes and functions have docstrings that say what, not how (Optional) | Pass | Public modules, classes and functions carry Doxygen docstrings that say what. |
| 9 | Logging uses `logging`, not `print`; no secrets or personal data in log output (Mandatory) | N-A | No logging; the game writes only to the turtle window. |
| 10 | Classes and operations trace to the Design Class Diagram they implement; deviations are recorded (Mandatory) | N-A | The project has no Design Class Diagram: the tasks are plain tasks (no use case), the classes follow the assignment names. |
| 11 | Tests exist for new behaviour, are named for the behaviour, and do not depend on order or the network (Mandatory) | Pass | The orientation bug was reproduced by a display test that fails without the fix and passes with it. |
| 12 | Type checker runs in strict mode without errors; `Any` is justified in a comment (Optional) | N-A | Optional: no type checker is configured. |
| 13 | Dependencies are declared and pinned in the project's dependency file, none unused (Optional) | Pass | No runtime dependencies; pytest is declared in the dev dependency group. |
| 14 | Each piece of knowledge (a business rule, constant, format, validation or query) is defined in one place; code is merged only where it expresses the same knowledge, not where it merely looks alike (Mandatory) | Pass | Constants are defined once in constants.py and imported. |
| 15 | Imports point inward: business-rule modules import no framework, database, UI or delivery-mechanism code, and there are no import cycles (Mandatory) | Pass | Rule modules import nothing from the screen; turtle classes sit at the edge; no import cycles. |
| 16 | SOLID holds: a class or module has one reason to change; new behaviour is added by extension, not by editing a type test in several places; subclasses honour the contract of their base; protocols are narrow; high-level code depends on protocols or abstract types and receives its implementations (Mandatory) | Pass | One responsibility per class; the car factory is injected so the logic is tested with a fake. |
## Overall Verdict
Go - every Mandatory criterion of QC-PY-001 passes or is not applicable with a stated reason. The review is not independent: author and reviewer are the same person, with the AI standing in for S01.
## Action Items
| Action | Owner | Due |
| --- | --- | --- |
| Challenge this review in the end-of-run summary if needed | S01 | 2026-10-27 |
---
[MIL-004]: ../../milestones/mil-004-cars.md
[QC-PY-001]: ../../../framework/qc/code/qc-programming-python.md
+22 -1
View File
@@ -4,6 +4,12 @@
A car is a compound turtle shape inside a 40 x 20 pixel footprint, front to the A car is a compound turtle shape inside a 40 x 20 pixel footprint, front to the
left: lower body, cabin, two windows, a headlight and two wheels. The geometry left: lower body, cabin, two windows, a headlight and two wheels. The geometry
is plain data (see car_components) so it can be tested without a display. is plain data (see car_components) so it can be tested without a display.
Shape frame: car_components works in screen coordinates (x along the car, front
at -x). A turtle shape, however, is defined pointing up (forward is +y) and is
rotated by the turtle's heading, so at heading 0 (east) it is turned clockwise
by 90 degrees. to_shape_frame converts the points when the shapes are
registered, so a car at heading 0 lies along x and drives to the left.
""" """
from turtle import Shape, TurtleScreen from turtle import Shape, TurtleScreen
@@ -74,6 +80,19 @@ def car_components(color: str) -> list[Component]:
] ]
def to_shape_frame(point: tuple[float, float]) -> tuple[float, float]:
"""@brief Convert a screen-coordinate point to the turtle shape's own frame.
A shape's forward direction is +y and heading 0 turns it clockwise by 90
degrees, so the screen point (x, y) must be registered as (-y, x).
@param point A point (x, y) in screen coordinates at heading 0.
@return The same point in the shape's frame.
"""
x, y = point
return (-y, x)
def register_car_shapes(screen: TurtleScreen) -> None: def register_car_shapes(screen: TurtleScreen) -> None:
"""@brief Register one car shape per colour with the screen. """@brief Register one car shape per colour with the screen.
@@ -82,5 +101,7 @@ def register_car_shapes(screen: TurtleScreen) -> None:
for color in COLORS: for color in COLORS:
shape = Shape("compound") shape = Shape("compound")
for polygon, fill, outline in car_components(color): for polygon, fill, outline in car_components(color):
shape.addcomponent(polygon, fill, outline) shape.addcomponent(
tuple(to_shape_frame(point) for point in polygon), fill, outline
)
screen.register_shape(shape_name(color), shape) screen.register_shape(shape_name(color), shape)
+34 -2
View File
@@ -1,8 +1,13 @@
"""Tests for the car drawing.""" """Tests for the car drawing."""
from turtle import TurtleScreen from turtle import Turtle, TurtleScreen
from car_shape import car_components, register_car_shapes, shape_name from car_shape import (
car_components,
register_car_shapes,
shape_name,
to_shape_frame,
)
from constants import CAR_HEIGHT, CAR_LENGTH, COLORS from constants import CAR_HEIGHT, CAR_LENGTH, COLORS
@@ -28,3 +33,30 @@ def test_body_and_cabin_use_the_car_colour() -> None:
def test_register_car_shapes_adds_one_shape_per_colour(screen: TurtleScreen) -> None: def test_register_car_shapes_adds_one_shape_per_colour(screen: TurtleScreen) -> None:
register_car_shapes(screen) register_car_shapes(screen)
assert all(shape_name(color) in screen.getshapes() for color in COLORS) assert all(shape_name(color) in screen.getshapes() for color in COLORS)
def test_to_shape_frame_maps_screen_points_to_the_upright_shape_frame() -> None:
assert to_shape_frame((-20, 0)) == (0, -20) # front (screen left) -> shape -y
assert to_shape_frame((20, 0)) == (0, 20)
assert to_shape_frame((0, 10)) == (-10, 0)
assert to_shape_frame((0, 0)) == (0, 0)
def test_drawn_car_is_horizontal(screen: TurtleScreen) -> None:
register_car_shapes(screen)
car = Turtle(visible=False)
car.penup()
car.shape(shape_name("red"))
car.showturtle()
try:
screen.update()
canvas = screen.getcanvas()
# the first compound component is the lower body
coords = canvas.coords(car.turtle._item[0]) # type: ignore[attr-defined]
xs, ys = coords[0::2], coords[1::2]
width, height = max(xs) - min(xs), max(ys) - min(ys)
assert width > height
assert width <= CAR_LENGTH
assert height <= CAR_HEIGHT
finally:
car.hideturtle()