Test/Python(20220101~)

Days23. Turtle crossing 게임

kiostory 2022. 3. 5. 18:10

 

1. Move the turtle with keypress

2. Create and move the cars

3. Detect collision with car

4. Detect when turtle reachs the other side

5. Create a scoreboard

 

*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.tracer(0)
screen.listen()
player = Player()
screen.onkeypress(player.gogogo, "Up")
game_is_on = True
while game_is_on:
    time.sleep(0.1)
    screen.update()
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.color("black")
        self.shape("turtle")
        self.pu()
        self.setpos(STARTING_POSITION)
        self.setheading(90)
    def gogogo(self):
        self.fd(MOVE_DISTANCE)

 

[결과]