100일 간의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 23일차(거북이 횡단 캡스톤 프로젝트)
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
import time
# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("white")
screen.title("Turtle Crossing")
screen.tracer(0)
# Class
player = Player()
car = CarManager()
score = Scoreboard()
# Screen settings
screen.listen()
screen.onkeypress(player.go_up, "Up")
# Game logic
game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.1)
car.create_cars()
car.move_cars()
# (Turtle)Detect wall contact at y_axis (top of the screen), reset to start position
if player.ycor() > 280:
player.reset_position()
score.point()
car.level_up()
# Detect collision with car
for vehicle in car.all_cars:
if vehicle.distance(player) < 20:
game_is_on = False
score.game_over()
screen.exitonclick()
from turtle import Turtle
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
class Player(Turtle):
def __init__(self):
super().__init__()
self.showturtle()
self.shape("turtle")
self.setheading(90)
self.penup()
self.goto(STARTING_POSITION)
def go_up(self):
new_y = self.ycor() + 20
self.penup()
self.goto(self.xcor(), new_y)
def reset_position(self):
self.goto(STARTING_POSITION)
from turtle import Turtle
import random
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
GO_TO = (280, 0)
STARTING_MOV_DISTANCE = 5
MOVE_INCREMENT = 10
class CarManager:
def __init__(self):
self.all_cars = []
self.car_speed = STARTING_MOV_DISTANCE
def create_cars(self):
random_chance = random.randint(1, 6)
if random_chance == 1:
new_car = Turtle(shape="square")
new_car.color(random.choice(COLORS))
new_car.shapesize(stretch_wid=1, stretch_len=2)
new_car.penup()
random_y = random.randint(-250, 250)
new_car.goto(300, random_y)
self.all_cars.append(new_car)
def move_cars(self):
for car in self.all_cars:
car.backward(self.car_speed)
def level_up(self):
self.car_speed += MOVE_INCREMENT
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Courier", 24, "bold")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.score = 0
self.penup()
self.color("black")
self.update_scoreboard()
def update_scoreboard(self):
self.clear()
self.goto(-205, 260)
self.write(f"Level: {self.score}", align=ALIGNMENT, font=FONT)
def point(self):
self.score += 1
self.update_scoreboard()
def game_over(self):
self.goto(0, 0)
self.write("Game Over", align=ALIGNMENT, font=FONT)
Reference
이 문제에 관하여(100일 간의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 23일차(거북이 횡단 캡스톤 프로젝트)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mike_kameta_aed62d48c2d0f/100-days-of-code-the-complete-python-pro-bootcamp-for-2022-day-23-the-turtle-crossing-capstone-project-3lo2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)