Turtle Crossing Game Analysis
1. The turtle moves forward when the 'up' key is pressed. It cannot move in any other direction.
2. Cars are randomly generated at the right edge of the screen and move towards the left edge.
3. When the turtle reaches the top of the screen, it returns to its original position and
progresses to the next level, where the cars' speed increases.
4. If the turtle collides with a car, the game ends and everything stops.
main.py
import time
import random
from turtle import Screen
from player import Player
from car_manager import CarManager
from level import Level
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.title("Turtle Crossing Game")
turtle = Player()
car_manager = CarManager()
level = Level()
screen.listen()
screen.onkey(turtle.move, "Up")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.create_car()
car_manager.move()
if turtle.is_finish_line():
turtle.go_to_start_line()
car_manager.increase_speed()
level.increase_level()
for car in car_manager.all_cars:
if turtle.is_collision(car):
game_is_on = False
level.game_over()
screen.exitonclick()
car_manager.py
from turtle import Turtle
import random
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10
Y_POSITIONS = range(-250, 251, 20)
class CarManager():
def __init__(self):
self.all_cars = []
self.car_speed = STARTING_MOVE_DISTANCE
def create_car(self):
random_chance = random.randint(1,10)
if random_chance == 1:
new_car = Turtle()
new_car.shape("square")
new_car.setheading(180)
new_car.shapesize(stretch_wid=1, stretch_len=2)
new_car.penup()
new_car.color(random.choice(COLORS))
new_car.goto(300, random.choice(Y_POSITIONS))
self.all_cars.append(new_car)
def move(self):
for car in self.all_cars:
car.forward(self.car_speed)
def increase_speed(self):
self.car_speed += MOVE_INCREMENT
level.py
from turtle import Turtle
FONT = ("Courier", 24, "normal")
class Level(Turtle):
def __init__(self):
super().__init__()
self.level = 1
self.hideturtle()
self.penup()
self.goto(-280, 250)
self.update_scoreboard()
def update_scoreboard(self):
self.clear()
self.write(f"Level: {self.level}", align="left", font=FONT)
def increase_level(self):
self.level += 1
self.update_scoreboard()
def game_over(self):
self.goto(0, 0)
self.write(f"GAME OVER", align="center", font=FONT)
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.color("black")
self.setheading(90)
self.penup()
self.goto(STARTING_POSITION)
def move(self):
self.forward(MOVE_DISTANCE)
def is_finish_line(self):
return self.ycor() > FINISH_LINE_Y
def go_to_start_line(self):
self.goto(STARTING_POSITION)
def is_collision(self, car):
if self.distance(car) < 25:
return True
return False
Realization
The code I initially wrote had an issue where multiple Car objects were created directly
within main.py.
As a result, when the turtle reached the top of the screen and called
the increase_speed() method, only the speeds of the already created cars would increase.
New Car objects that were generated afterwards would still move at the default speed.
To resolve this issue, I reviewed the good code on Internet and
found that the problem stemmed from the __init__ method of the Car class.
The __init__ method is responsible for initializing the object's attributes when it is created.
Since Car objects were being created directly in main.py,
the __init__ method was being called repeatedly,
causing new cars to always start with the default speed.
The solution was to manage the creation of Car objects within the CarManager class.
By doing so, the __init__ method is called only once for the CarManager,
and it handles the management of car creation in a consistent manner.
This ensures that new cars are created with the same speed settings as the existing cars,
thus solving the issue.
To manage object properties consistently,
object creation and initialization should be handled centrally.
'Python' 카테고리의 다른 글
Sixth Python Project: Ping-Pong Game (0) | 2024.08.16 |
---|---|
Fifth Python Project: Snake Game - 2 (0) | 2024.08.10 |
Fifth Python Project: Snake Game - 1 (0) | 2024.08.05 |
Fourth Python Project: Turtle Racing Game (0) | 2024.08.04 |
Third Python Project: Quiz game (1) | 2024.08.03 |