Add the Scoreboard and the game over message

Task: MIL-006#1
Task: MIL-006#2
Task: MIL-006#3
This commit is contained in:
2026-10-13 14:00:37 +08:00
parent 6ca2d760dd
commit 01087debf2
8 changed files with 199 additions and 52 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
"""Tests for the car drawing."""
from turtle import TurtleScreen
from car_shape import car_components, register_car_shapes, shape_name
from constants import CAR_HEIGHT, CAR_LENGTH, COLORS
@@ -23,6 +25,6 @@ def test_body_and_cabin_use_the_car_colour() -> None:
assert fills.count("green") == 2
def test_register_car_shapes_adds_one_shape_per_colour(screen) -> None:
def test_register_car_shapes_adds_one_shape_per_colour(screen: TurtleScreen) -> None:
register_car_shapes(screen)
assert all(shape_name(color) in screen.getshapes() for color in COLORS)
+50 -48
View File
@@ -1,48 +1,50 @@
"""Tests for the Player class (need a display; skipped without one)."""
import pytest
from constants import FINISH_LINE_Y, MOVE_DISTANCE, STARTING_POSITION
from player import Player
@pytest.fixture
def player(screen) -> Player:
new_player = Player()
yield new_player
new_player.hideturtle()
def test_player_starts_at_starting_position(player: Player) -> None:
assert player.position() == STARTING_POSITION
def test_player_faces_north(player: Player) -> None:
assert player.heading() == 90
def test_go_up_moves_forward_by_move_distance(player: Player) -> None:
start_y = player.ycor()
player.go_up()
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()
assert player.xcor() == pytest.approx(STARTING_POSITION[0])
"""Tests for the Player class (need a display; skipped without one)."""
from turtle import TurtleScreen
import pytest
from constants import FINISH_LINE_Y, MOVE_DISTANCE, STARTING_POSITION
from player import Player
@pytest.fixture
def player(screen: TurtleScreen) -> Player:
new_player = Player()
yield new_player
new_player.hideturtle()
def test_player_starts_at_starting_position(player: Player) -> None:
assert player.position() == STARTING_POSITION
def test_player_faces_north(player: Player) -> None:
assert player.heading() == 90
def test_go_up_moves_forward_by_move_distance(player: Player) -> None:
start_y = player.ycor()
player.go_up()
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()
assert player.xcor() == pytest.approx(STARTING_POSITION[0])
+36
View File
@@ -0,0 +1,36 @@
"""@file test_scoreboard.py
@brief Tests for the Scoreboard.
"""
from turtle import TurtleScreen
from scoreboard import Scoreboard
def test_level_starts_at_one(screen: TurtleScreen) -> None:
assert Scoreboard().level == 1
def test_level_text_shows_level_one_at_start(screen: TurtleScreen) -> None:
assert Scoreboard().level_text == "Level: 1"
def test_level_text_follows_level() -> None:
board = Scoreboard.__new__(Scoreboard)
board.level = 7
assert board.level_text == "Level: 7"
def test_increase_level_adds_one(screen: TurtleScreen) -> None:
board = Scoreboard()
board.increase_level()
assert board.level == 2
assert board.level_text == "Level: 2"
def test_game_over_keeps_level(screen: TurtleScreen) -> None:
board = Scoreboard()
board.increase_level()
board.game_over()
assert board.level == 2
assert board.position() == (0, 0)