Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addes a Turtle crossing Game #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions Python Project/Turtle Crossing Game/car_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from turtle import Turtle
import random

COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10

class CarManager():
def __init__(self):
self.all_cars = []
self.car_speed = STARTING_MOVE_DISTANCE

def create_car(self):
random_chance = random.randint(1, 6)
if random_chance == 1:
new_car = Turtle("square")
new_car.shapesize(stretch_wid=1, stretch_len=2)
new_car.penup()
new_car.color(random.choice(COLORS))
random_y = random.randint(-250, 250)
new_car.goto(300, random_y)
self.all_cars.append(new_car)

def move_cars(self):
for car in self.all_cars:
car.backward(self.car_speed)

def level_up(self):
self.car_speed += MOVE_INCREMENT
40 changes: 40 additions & 0 deletions Python Project/Turtle Crossing Game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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)

timy = Player()
car_manager = CarManager()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(timy.go_up, "Up")

game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.create_car()
car_manager.move_cars()

#player collides with a car
for car in car_manager.all_cars:
if car.distance(timy) < 20:
game_is_on = False
scoreboard.game_over()

#player has reached the top edge of the screen
if timy.is_at_finish_line():
timy.go_to_start()
car_manager.level_up()
scoreboard.increase_level()



screen.exitonclick()

24 changes: 24 additions & 0 deletions Python Project/Turtle Crossing Game/player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.setheading(90)
self.penup()
self.go_to_start()

def go_up(self):
self.forward(MOVE_DISTANCE)

def go_to_start(self):
self.goto(STARTING_POSITION)

def is_at_finish_line(self):
if self.ycor() > FINISH_LINE_Y:
return True
else:
return False
23 changes: 23 additions & 0 deletions Python Project/Turtle Crossing Game/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from turtle import Turtle
FONT = ("Courier", 20, "normal")

class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.level = 1
self.hideturtle()
self.penup()
self.goto(-280, 260)
self.update_score()

def update_score(self):
self.clear()
self.write(f"Level: {self.level}", align = "left", font= FONT)

def increase_level(self):
self.level += 1
self.update_score()

def game_over(self):
self.goto(0,0)
self.write(f"GAME OVER", align = "center", font= FONT)