Day 23-Turtle Crossing Game


Posted by pei_______ on 2022-05-03

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


# main.py

import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard

screen = Screen()
screen.setup(width=600, height=600)
screen.title("Turtle Crossing Game")
screen.tracer(0)

car = CarManager()
player = Player()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(player.up, "Up")


game_is_on = True
while game_is_on:
    time.sleep(0.1)
    screen.update()
    car.create_cars()
    car.move_cars()

    if player.is_at_finish_line():
        player.go_to_start()
        scoreboard.increase_level()
        car.speed_up()

    for cars in car.all_cars:
        if player.distance(cars) < 20:
            game_is_on = False
            scoreboard.game_over()


screen.exitonclick()
# player.py

from turtle import Turtle
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280


class Player(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("turtle")
        self.penup()
        self.setheading(90)
        self.go_to_start()

    def go_to_start(self):
        self.goto(STARTING_POSITION)

    def up(self):
        self.forward(MOVE_DISTANCE)

    def is_at_finish_line(self):
        if self.ycor() > FINISH_LINE_Y:
            return True
        else:
            return False
# car_manager.py

from turtle import Turtle
from random import randint
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 2


class CarManager:
    def __init__(self):
        self.all_cars = []
        self.count = 0
        self.speed = STARTING_MOVE_DISTANCE
        self.create_cars()

    def create_cars(self):
        self.count += 1
        if self.count % 6 == 0:
            new_car = Turtle()
            new_car.penup()
            new_car.color(COLORS[randint(0, 5)])
            new_car.shape("square")
            new_car.shapesize(stretch_wid=1, stretch_len=2)
            new_car.goto(290, randint(-250, 250))
            self.all_cars.append(new_car)

    def move_cars(self):
        for cars in self.all_cars:
            new_x = cars.xcor() - self.speed
            cars.goto(new_x, cars.ycor())

    def speed_up(self):
        self.speed += MOVE_INCREMENT
# scoreboard.py

from turtle import Turtle
FONT = ("Courier", 18, "normal")


class Scoreboard(Turtle):
    def __init__(self):
        super().__init__()
        self.level = 1
        self.hideturtle()
        self.penup()
        self.goto(-270, 250)
        self.update_board()

    def update_board(self):
        self.clear()
        print(self.level)
        self.write(f"Level: {self.level}", font=FONT)

    def increase_level(self):
        self.level += 1
        self.update_board()

    def game_over(self):
        self.goto(0, 0)
        self.write("Game Over", align="center", font=FONT)

#Python #課堂筆記 #100 Days of Code







Related Posts

EJS - Daily Journal Project

EJS - Daily Journal Project

程式導師實驗計畫 Week 15 題目

程式導師實驗計畫 Week 15 題目

ES6

ES6


Comments