Add the Player class and the Up key binding

Task: MIL-003#1
Task: MIL-003#2
Task: MIL-003#3
This commit is contained in:
2026-10-13 13:56:40 +08:00
parent 8632d60518
commit 71498a9f67
6 changed files with 159 additions and 30 deletions
+31 -27
View File
@@ -1,27 +1,31 @@
"""@file main.py
@brief Entry point: sets up the screen and runs the game loop.
"""
import time
from turtle import Screen
from car_manager import CarManager
from constants import LOOP_DELAY, SCREEN_HEIGHT, SCREEN_WIDTH
from player import Player
from scoreboard import Scoreboard
def main() -> None:
"""@brief Run the game until the player is hit by a car."""
screen = Screen()
screen.setup(width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
screen.tracer(0)
game_is_on = True
while game_is_on:
time.sleep(LOOP_DELAY)
screen.update()
if __name__ == "__main__":
main()
"""@file main.py
@brief Entry point: sets up the screen and runs the game loop.
"""
import time
from turtle import Screen
from car_manager import CarManager
from constants import LOOP_DELAY, SCREEN_HEIGHT, SCREEN_WIDTH
from player import Player
from scoreboard import Scoreboard
def main() -> None:
"""@brief Run the game until the player is hit by a car."""
screen = Screen()
screen.setup(width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
screen.tracer(0)
player = Player()
screen.listen()
screen.onkey(player.go_up, "Up")
game_is_on = True
while game_is_on:
time.sleep(LOOP_DELAY)
screen.update()
if __name__ == "__main__":
main()
+17 -2
View File
@@ -2,6 +2,21 @@
@brief The turtle the user controls.
"""
from turtle import Turtle
class Player:
"""@brief Placeholder, implemented in milestone MIL-003."""
from constants import MOVE_DISTANCE, STARTING_POSITION
class Player(Turtle):
"""@brief The turtle that crosses the road; it can only move forward (north)."""
def __init__(self) -> None:
"""@brief Create the player at the starting position, facing north."""
super().__init__(shape="turtle")
self.penup()
self.setheading(90)
self.goto(STARTING_POSITION)
def go_up(self) -> None:
"""@brief Move one step north."""
self.forward(MOVE_DISTANCE)