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.tracer(0)
player = Player()# 플레이어 호출
car_manager = CarManager()# 차량 호출
scoreboard = Scoreboard()
# 스크린 이벤트
screen.listen()
screen.onkey(fun=player.move_up, key="Up")
# 게임 시작
game_is_on = True
while game_is_on:
time.sleep(0.1)# 0.1초 간격으로 실행
screen.update()# 스크린 업데이트
car_manager.create_car()#차량 생성
car_manager.car_move()# 차량 이동# 차량 충돌 감지for car in car_manager.all_cars :
if player.distance(car) < 20 :
game_is_on = False
scoreboard.game_over()
# 결승선 도달 감지if player.ycor() > 280 :
player.reset()
car_manager.car_speed()
scoreboard.level_up()
screen.exitonclick()
from turtle import Turtle
FONT = ("Courier", 18, "bold")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.level = 1
self.penup()
self.hideturtle()
self.goto(-280,260)
self.write(f"Level : {self.level}", align="left", font=FONT)
def update_scoreboard(self):
"""스코어보드 업데이트"""
self.clear()
self.write(f"Level : {self.level}", align="left", font=FONT)
def level_up(self):
"""레벨 기록"""
self.level += 1
self.update_scoreboard()
def game_over(self):
"""게임 종료"""
self.goto(0,0)
self.write(f"Game_over", align="left", font=FONT)
car_manager.py
from turtle import Turtle
import random
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10
class CarManager(Turtle):
def __init__(self):
self.speed = 10
self.all_cars = []
def create_car(self):
"""차량 생성 함수."""
random_chance = random.randint(1,5)
if random_chance == 1 :
new_car = Turtle("square")
new_car.penup()
new_car.color(random.choice(COLORS))
random_position = (300, random.randint(-240, 240))
new_car.goto(random_position)
new_car.setheading(180)
new_car.shapesize(stretch_wid=1, stretch_len=2)
self.all_cars.append(new_car)
def car_move(self):
"""차량 이동"""
for car in self.all_cars :
car.forward(self.speed)
def car_speed(self):
"""차량 속도 상승"""
self.speed += MOVE_INCREMENT
from turtle import Turtle
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 15
FINISH_LINE_Y = 280
class Player(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.goto(STARTING_POSITION)
self.setheading(90)
self.shape("turtle")
# 플레이어 이동def move_up(self):
self.forward(MOVE_DISTANCE)