Add collision detection, finish line and level up
Task: MIL-005#1 Task: MIL-005#2 Task: MIL-005#3 Task: MIL-005#4
This commit is contained in:
@@ -15,7 +15,7 @@ document of a type. `Primary File` may contain a glob (e.g.
|
||||
| SA | Stakeholder Analysis | docs/stakeholder-analysis.md | 002 |
|
||||
| PP | Project Plan | docs/project-plan.md | 002 |
|
||||
| MIL | Milestone / Gateway | docs/milestones/mil-*.md | 009 |
|
||||
| RC | SQA Review Record | docs/sqa/reviews/rc-*.md | 014 |
|
||||
| RC | SQA Review Record | docs/sqa/reviews/rc-*.md | 015 |
|
||||
| TM | Traceability Matrix | docs/sqa/traceability-matrix.md | 002 |
|
||||
|
||||
## Languages
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# Code review for MIL-005
|
||||
|
||||
## Metadata
|
||||
| Key | Value |
|
||||
| --- | --- |
|
||||
| ID | RC-014 |
|
||||
| CrossReference | [MIL-005], [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-005]
|
||||
- Checklist used: [QC-PY-001]
|
||||
- Scope: full review of the source and tests changed by MIL-005: collision.py, is_hit, finish line and go_to_start, game loop, 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 | Tests exist for the new behaviour, are named for it and are order-independent and offline. |
|
||||
| 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-005]: ../../milestones/mil-005-collision-levelup.md
|
||||
[QC-PY-001]: ../../../framework/qc/code/qc-programming-python.md
|
||||
@@ -5,8 +5,10 @@
|
||||
import random
|
||||
from collections.abc import Callable
|
||||
from turtle import Turtle
|
||||
from typing import Protocol
|
||||
|
||||
from car_shape import shape_name
|
||||
from collision import is_colliding
|
||||
from constants import (
|
||||
CAR_REMOVE_X,
|
||||
CAR_START_X,
|
||||
@@ -18,6 +20,18 @@ from constants import (
|
||||
)
|
||||
|
||||
|
||||
class HasPosition(Protocol):
|
||||
"""@brief Anything with a position, such as a turtle."""
|
||||
|
||||
def xcor(self) -> float:
|
||||
"""@brief X coordinate."""
|
||||
...
|
||||
|
||||
def ycor(self) -> float:
|
||||
"""@brief Y coordinate."""
|
||||
...
|
||||
|
||||
|
||||
class CarManager:
|
||||
"""@brief Keeps the cars of the road: creates, moves and removes them."""
|
||||
|
||||
@@ -54,6 +68,17 @@ class CarManager:
|
||||
car.hideturtle()
|
||||
self.all_cars.remove(car)
|
||||
|
||||
def is_hit(self, player: HasPosition) -> bool:
|
||||
"""@brief Tell whether any car collides with the player.
|
||||
|
||||
@param player Anything exposing xcor and ycor.
|
||||
@return True when a car overlaps the player.
|
||||
"""
|
||||
return any(
|
||||
is_colliding(player.xcor(), player.ycor(), car.xcor(), car.ycor())
|
||||
for car in self.all_cars
|
||||
)
|
||||
|
||||
def level_up(self) -> None:
|
||||
"""@brief Make the cars faster."""
|
||||
self.move_distance += MOVE_INCREMENT
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
"""@file collision.py
|
||||
@brief The collision rule between the player and a car, free of any window.
|
||||
"""
|
||||
|
||||
from constants import CAR_HEIGHT, CAR_LENGTH, PLAYER_HALF_SIZE
|
||||
|
||||
|
||||
def is_colliding(player_x: float, player_y: float, car_x: float, car_y: float) -> bool:
|
||||
"""@brief Tell whether the player's square overlaps the car's rectangle.
|
||||
|
||||
Touching edges is not a collision.
|
||||
|
||||
@param player_x X coordinate of the player's centre.
|
||||
@param player_y Y coordinate of the player's centre.
|
||||
@param car_x X coordinate of the car's centre.
|
||||
@param car_y Y coordinate of the car's centre.
|
||||
@return True when the two shapes overlap.
|
||||
"""
|
||||
is_overlapping_x = abs(player_x - car_x) < CAR_LENGTH / 2 + PLAYER_HALF_SIZE
|
||||
is_overlapping_y = abs(player_y - car_y) < CAR_HEIGHT / 2 + PLAYER_HALF_SIZE
|
||||
return is_overlapping_x and is_overlapping_y
|
||||
@@ -31,6 +31,15 @@ def main() -> None:
|
||||
car_manager.create_cars()
|
||||
car_manager.move_cars()
|
||||
|
||||
if car_manager.is_hit(player):
|
||||
game_is_on = False
|
||||
|
||||
if player.is_at_finish_line():
|
||||
player.go_to_start()
|
||||
car_manager.level_up()
|
||||
|
||||
screen.exitonclick()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+13
-2
@@ -4,7 +4,7 @@
|
||||
|
||||
from turtle import Turtle
|
||||
|
||||
from constants import MOVE_DISTANCE, STARTING_POSITION
|
||||
from constants import FINISH_LINE_Y, MOVE_DISTANCE, STARTING_POSITION
|
||||
|
||||
|
||||
class Player(Turtle):
|
||||
@@ -15,8 +15,19 @@ class Player(Turtle):
|
||||
super().__init__(shape="turtle")
|
||||
self.penup()
|
||||
self.setheading(90)
|
||||
self.goto(STARTING_POSITION)
|
||||
self.go_to_start()
|
||||
|
||||
def go_up(self) -> None:
|
||||
"""@brief Move one step north."""
|
||||
self.forward(MOVE_DISTANCE)
|
||||
|
||||
def is_at_finish_line(self) -> bool:
|
||||
"""@brief Tell whether the player has crossed the road.
|
||||
|
||||
@return True when the player is above the finish line.
|
||||
"""
|
||||
return self.ycor() > FINISH_LINE_Y
|
||||
|
||||
def go_to_start(self) -> None:
|
||||
"""@brief Put the player back at the starting position."""
|
||||
self.goto(STARTING_POSITION)
|
||||
|
||||
@@ -36,6 +36,9 @@ class FakeCar:
|
||||
def xcor(self) -> float:
|
||||
return self.x
|
||||
|
||||
def ycor(self) -> float:
|
||||
return self.y
|
||||
|
||||
def setx(self, x: float) -> None:
|
||||
self.x = x
|
||||
|
||||
@@ -99,6 +102,37 @@ def test_cars_past_the_left_edge_are_removed() -> None:
|
||||
assert manager.all_cars == [] and car.hidden
|
||||
|
||||
|
||||
class FakePlayer:
|
||||
"""Stands in for the player turtle."""
|
||||
|
||||
def __init__(self, x: float, y: float) -> None:
|
||||
self.x, self.y = x, y
|
||||
|
||||
def xcor(self) -> float:
|
||||
return self.x
|
||||
|
||||
def ycor(self) -> float:
|
||||
return self.y
|
||||
|
||||
|
||||
def test_is_hit_when_a_car_overlaps_the_player() -> None:
|
||||
manager = make_manager()
|
||||
manager.create_cars()
|
||||
car = manager.all_cars[0]
|
||||
assert manager.is_hit(FakePlayer(car.x, car.y))
|
||||
|
||||
|
||||
def test_is_not_hit_when_cars_are_far_away() -> None:
|
||||
manager = make_manager()
|
||||
manager.create_cars()
|
||||
car = manager.all_cars[0]
|
||||
assert not manager.is_hit(FakePlayer(car.x - 200, car.y))
|
||||
|
||||
|
||||
def test_is_not_hit_when_there_are_no_cars() -> None:
|
||||
assert not make_manager().is_hit(FakePlayer(0, 0))
|
||||
|
||||
|
||||
def test_level_up_increases_speed() -> None:
|
||||
manager = make_manager()
|
||||
manager.level_up()
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Tests for the pure collision rule."""
|
||||
|
||||
from collision import is_colliding
|
||||
from constants import CAR_HEIGHT, CAR_LENGTH, PLAYER_HALF_SIZE
|
||||
|
||||
REACH_X = CAR_LENGTH / 2 + PLAYER_HALF_SIZE
|
||||
REACH_Y = CAR_HEIGHT / 2 + PLAYER_HALF_SIZE
|
||||
|
||||
|
||||
def test_hit_when_player_overlaps_car() -> None:
|
||||
assert is_colliding(5, 3, 0, 0)
|
||||
|
||||
|
||||
def test_no_hit_when_beyond_car_half_length_horizontally() -> None:
|
||||
assert not is_colliding(REACH_X + 1, 0, 0, 0)
|
||||
|
||||
|
||||
def test_no_hit_when_a_lane_away_vertically() -> None:
|
||||
assert not is_colliding(0, CAR_HEIGHT * 2, 0, 0)
|
||||
|
||||
|
||||
def test_no_hit_when_exactly_at_horizontal_boundary() -> None:
|
||||
assert not is_colliding(REACH_X, 0, 0, 0)
|
||||
|
||||
|
||||
def test_no_hit_when_exactly_at_vertical_boundary() -> None:
|
||||
assert not is_colliding(0, REACH_Y, 0, 0)
|
||||
|
||||
|
||||
def test_hit_just_inside_the_boundary() -> None:
|
||||
assert is_colliding(REACH_X - 1, REACH_Y - 1, 0, 0)
|
||||
+16
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from constants import MOVE_DISTANCE, STARTING_POSITION
|
||||
from constants import FINISH_LINE_Y, MOVE_DISTANCE, STARTING_POSITION
|
||||
from player import Player
|
||||
|
||||
|
||||
@@ -27,6 +27,21 @@ def test_go_up_moves_forward_by_move_distance(player: Player) -> None:
|
||||
assert player.ycor() == start_y + MOVE_DISTANCE
|
||||
|
||||
|
||||
def test_player_is_not_at_finish_line_at_start(player: Player) -> None:
|
||||
assert not player.is_at_finish_line()
|
||||
|
||||
|
||||
def test_player_is_at_finish_line_above_finish_y(player: Player) -> None:
|
||||
player.goto(0, FINISH_LINE_Y + 1)
|
||||
assert player.is_at_finish_line()
|
||||
|
||||
|
||||
def test_go_to_start_returns_to_starting_position(player: Player) -> None:
|
||||
player.goto(0, FINISH_LINE_Y + 1)
|
||||
player.go_to_start()
|
||||
assert player.position() == STARTING_POSITION
|
||||
|
||||
|
||||
def test_go_up_never_moves_sideways(player: Player) -> None:
|
||||
for _ in range(5):
|
||||
player.go_up()
|
||||
|
||||
Reference in New Issue
Block a user