Skip to content

Commit 44c4ad0

Browse files
committed
added turtle game
1 parent 542997b commit 44c4ad0

13 files changed

+153
-0
lines changed
Loading
Loading
Loading

Turtle Crossing Game/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
**Turtle Crossing Game**
2+
3+
**GOAL**
4+
The Goal of this game is to create a Turtle Crossing Game.
5+
6+
**DESCRIPTION**
7+
In this game, turtle has to cross the road, with running cars safely. If road crossed safely, turtle moves to next level, with even faster cars.
8+
9+
### LIBRARIES NEEDED
10+
11+
1. Turtle: provides a powerful graphic tool.
12+
13+
**WHAT I HAD DONE**
14+
1) Create a turtle player that starts at the bottom of the screen and listen for the "Up" keypress to move the turtle north.
15+
2) Create cars that are 20px high by 40px wide that are randomly generated along the y-axis and move to the left edge of the screen.
16+
3) Detect when the turtle player collides with a car and stop the game if this happens.
17+
4) Detect when the turtle player has reached the top edge of the screen (i.e., reached the FINISH_LINE_Y). When this happens, return the turtle to the starting position and increase the speed of the cars.
18+
5) Create a scoreboard that keeps track of which level the user is on. Every time the turtle player does a successful crossing, the level should increase. When the turtle hits a car, GAME OVER should be displayed in the centre.
19+
20+
### DEMONSTRATION
21+
(https://drive.google.com/drive/folders/1HcUC_fTHpEL1mGZdLIifHHlVecsB3Egg?usp=share_link)
22+
23+
### GAMEPLAY
24+
![turtle-game](turtle_crossing.gif)
25+
26+
### Sarthak Joleya ###
Binary file not shown.
Binary file not shown.
Binary file not shown.

Turtle Crossing Game/car_manager.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 = 10
7+
8+
9+
class CarManager:
10+
11+
def __init__(self):
12+
self.all_cars = []
13+
self.car_speed = STARTING_MOVE_DISTANCE
14+
15+
def create_car(self):
16+
random_chance = random.randint(1, 6)
17+
if random_chance == 1:
18+
new_car = Turtle("square")
19+
new_car.shapesize(stretch_wid=1, stretch_len=2)
20+
new_car.penup()
21+
new_car.color(random.choice(COLORS))
22+
random_y = random.randint(-250, 250)
23+
new_car.goto(300, random_y)
24+
self.all_cars.append(new_car)
25+
26+
def move_cars(self):
27+
for car in self.all_cars:
28+
car.backward(self.car_speed)
29+
30+
def level_up(self):
31+
self.car_speed += MOVE_INCREMENT

Turtle Crossing Game/main.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
screen.setup(width=600, height=600)
9+
screen.tracer(0)
10+
11+
player = Player()
12+
car_manager = CarManager()
13+
scoreboard = Scoreboard()
14+
15+
screen.listen()
16+
screen.onkey(player.go_up, "Up")
17+
18+
game_is_on = True
19+
while game_is_on:
20+
time.sleep(0.1)
21+
screen.update()
22+
23+
car_manager.create_car()
24+
car_manager.move_cars()
25+
26+
#Detect collision with car
27+
for car in car_manager.all_cars:
28+
if car.distance(player) < 20:
29+
game_is_on = False
30+
scoreboard.game_over()
31+
32+
#Detect successful crossing
33+
if player.is_at_finish_line():
34+
player.go_to_start()
35+
car_manager.level_up()
36+
scoreboard.increase_level()
37+
38+
39+
40+
screen.exitonclick()

Turtle Crossing Game/player.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from turtle import Turtle
2+
3+
STARTING_POSITION = (0, -280)
4+
MOVE_DISTANCE = 20
5+
FINISH_LINE_Y = 280
6+
7+
8+
class Player(Turtle):
9+
10+
def __init__(self):
11+
super().__init__()
12+
self.shape("turtle")
13+
self.penup()
14+
self.go_to_start()
15+
self.setheading(90)
16+
17+
def go_up(self):
18+
self.forward(MOVE_DISTANCE)
19+
20+
def go_to_start(self):
21+
self.goto(STARTING_POSITION)
22+
23+
def is_at_finish_line(self):
24+
if self.ycor() > FINISH_LINE_Y:
25+
return True
26+
else:
27+
return False

Turtle Crossing Game/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The required library is:
2+
3+
turtle

Turtle Crossing Game/scoreboard.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from turtle import Turtle
2+
3+
FONT = ("Courier", 24, "normal")
4+
5+
6+
class Scoreboard(Turtle):
7+
8+
def __init__(self):
9+
super().__init__()
10+
self.level = 1
11+
self.hideturtle()
12+
self.penup()
13+
self.goto(-280, 250)
14+
self.update_scoreboard()
15+
16+
def update_scoreboard(self):
17+
self.clear()
18+
self.write(f"Level: {self.level}", align="left", font=FONT)
19+
20+
def increase_level(self):
21+
self.level += 1
22+
self.update_scoreboard()
23+
24+
def game_over(self):
25+
self.goto(0, 0)
26+
self.write(f"GAME OVER", align="center", font=FONT)
3.71 MB
Loading

0 commit comments

Comments
 (0)