Skip to content

Commit d6378d1

Browse files
committed
Added a Turtle Crossing Game Project based on Turtle Library
1 parent eae5b44 commit d6378d1

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed
Binary file not shown.
998 Bytes
Binary file not shown.
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from turtle import Turtle
2+
import random
3+
4+
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
5+
STARTING_MOVE_DISTANCE = 5
6+
MOVE_INCREMENT = 5
7+
8+
9+
10+
11+
class CarManager(Turtle):
12+
# class to manage the blocks
13+
14+
15+
def __init__(self):
16+
# creating a list of cars
17+
18+
self.all_cars = []
19+
self.car_speed = STARTING_MOVE_DISTANCE
20+
21+
def create_car(self):
22+
# method to create a car
23+
24+
random_chance = random.randint(1, 6)
25+
26+
if random_chance == 1:
27+
"""
28+
By this the car will be created only when 1 will come so there will be some distance between the
29+
cars where out turtle can move.
30+
"""
31+
32+
33+
new_car = Turtle()
34+
35+
new_car.color(random.choice(COLORS))
36+
new_car.shape("square")
37+
new_car.penup()
38+
new_car.shapesize(stretch_wid=1,stretch_len=2)
39+
y_cor = random.randint(-250, 250)
40+
new_car.goto(300, y_cor)
41+
self.all_cars.append(new_car)
42+
43+
44+
def move_car(self):
45+
# method to move the car
46+
47+
for car in self.all_cars:
48+
car.backward(self.car_speed)
49+
50+
def level_up_speed(self):
51+
# level up speed everytime the turtle hits the wall
52+
53+
self.car_speed += MOVE_INCREMENT
54+
55+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import time
2+
from turtle import Screen
3+
from player import Player
4+
from car_manager import CarManager
5+
from scoreboard import Scoreboard
6+
7+
screen = Screen()
8+
9+
screen.setup(width=600, height=600)
10+
screen.tracer(0)
11+
12+
player = Player()
13+
car_manager = CarManager()
14+
scoreboard = Scoreboard()
15+
16+
screen.listen()
17+
18+
# move turtle when we press the Up key
19+
screen.onkey(key="Up", fun=player.move_turtle)
20+
21+
game_is_on = True
22+
23+
24+
while game_is_on:
25+
time.sleep(0.1)
26+
27+
car_manager.create_car()
28+
car_manager.move_car()
29+
30+
# Detect collision with cars
31+
screen.update()
32+
33+
# create and move the cars
34+
for car in car_manager.all_cars:
35+
if car.distance(player) < 22:
36+
game_is_on = False
37+
scoreboard.game_over()
38+
# print game over whenever the turtle collide with cars
39+
40+
# Detect collision with wall
41+
if player.ycor() > 285:
42+
player.reset_turtle()
43+
car_manager.level_up_speed()
44+
#level up the speed of car whenever turtle collide with wall
45+
46+
scoreboard.increase_level()
47+
#level up the score whenever turtle collide with the wall
48+
49+
screen.exitonclick()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from turtle import Turtle
2+
3+
STARTING_POSITION = (0, -280)
4+
MOVE_DISTANCE = 10
5+
FINISH_LINE_Y = 280
6+
7+
8+
class Player(Turtle):
9+
# class to create the turtle
10+
11+
def __init__(self):
12+
super().__init__()
13+
self.shape("turtle")
14+
self.penup()
15+
self.goto(STARTING_POSITION)
16+
self.setheading(90)
17+
18+
def move_turtle(self):
19+
# method to move the turtle
20+
21+
self.forward(MOVE_DISTANCE)
22+
23+
def reset_turtle(self):
24+
# method to reset the turtle to move it original position when hit the wall
25+
26+
self.goto(STARTING_POSITION)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from turtle import Turtle
2+
3+
FONT = ("Courier", 24, "normal")
4+
5+
6+
class Scoreboard(Turtle):
7+
# class to get hold of scoreboard
8+
9+
def __init__(self):
10+
super().__init__()
11+
self.level = 1
12+
self.hideturtle()
13+
self.penup()
14+
self.goto(-270, 250)
15+
self.update_scoreboard()
16+
17+
def update_scoreboard(self):
18+
self.clear()
19+
self.write(f"Level: {self.level}", align="left", font=FONT)
20+
21+
def increase_level(self):
22+
# Increase the level when turtle hits the wall
23+
24+
self.level += 1
25+
self.update_scoreboard()
26+
27+
def game_over(self):
28+
29+
self.goto(0, 0)
30+
self.write("Game Over!!", align="center", font=FONT)

0 commit comments

Comments
 (0)