Skip to content

Commit 197da0b

Browse files
Update Day-23
1 parent 85e963b commit 197da0b

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

Day-23-CrossyRoad/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Crossy-Road
2+
3+
### An implementation of CrossyRoad.
4+
5+
<img src= 'https://user-images.githubusercontent.com/65078610/105710192-c4d67e80-5f3c-11eb-92af-ec49d084612a.gif' width="500">

Day-23-CrossyRoad/car_manager.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from turtle import Turtle
2+
import random
3+
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
4+
STARTING_MOVE_DISTANCE = 5
5+
6+
class CarManager:
7+
8+
def __init__(self):
9+
self.cars = []
10+
11+
def create_car(self):
12+
random_chance = random.randint(1,6)
13+
if random_chance == 1:
14+
self.new_car()
15+
16+
def new_car(self):
17+
new_car = Turtle(shape='square')
18+
new_car.penup()
19+
new_car.shapesize(stretch_wid=1, stretch_len=2)
20+
new_car.color(random.choice(COLORS))
21+
new_car.goto(self.position())
22+
self.cars.append(new_car)
23+
24+
def move(self):
25+
for car in self.cars:
26+
car.forward(STARTING_MOVE_DISTANCE)
27+
28+
def position(self):
29+
# x = random.randrange(-275,275,20)
30+
y = random.randrange(-200,200,20)
31+
return (-300,y)
32+
33+
def redirect(self):
34+
for car in self.cars:
35+
if car.xcor() > 300:
36+
car.setx(-car.xcor())
37+
car.sety(random.randrange(-200,200,20))

Day-23-CrossyRoad/main.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.title("Crossy Road")
9+
screen.setup(width=600, height=600)
10+
screen.tracer(0)
11+
screen.listen()
12+
13+
player = Player()
14+
car_manager = CarManager()
15+
scoreboard = Scoreboard()
16+
screen.onkey(player.up,'Up')
17+
18+
game_is_on = True
19+
while game_is_on:
20+
time.sleep(player.pace)
21+
screen.update()
22+
23+
# Create new car
24+
car_manager.create_car()
25+
26+
# Car starts motion
27+
car_manager.move()
28+
29+
# Car leaving screen
30+
# car_manager.redirect()
31+
32+
# Detect collision with car
33+
for car in car_manager.cars:
34+
if (player.ycor() == car.ycor()):
35+
if player.distance(car) < 25:
36+
game_is_on = False
37+
scoreboard.game_over()
38+
39+
# Player reaches finish line
40+
if player.is_finish():
41+
scoreboard.increment()
42+
43+
screen.exitonclick()

Day-23-CrossyRoad/player.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from turtle import Turtle
2+
STARTING_POSITION = (0, -280)
3+
MOVE_DISTANCE = 20
4+
FINISH_LINE_Y = 280
5+
6+
7+
class Player(Turtle):
8+
def __init__(self):
9+
super().__init__()
10+
self.shape('turtle')
11+
self.penup()
12+
self.left(90)
13+
self.goto(STARTING_POSITION)
14+
self.pace = 0.1
15+
16+
def up(self):
17+
self.forward(MOVE_DISTANCE)
18+
19+
def is_finish(self):
20+
if self.ycor() == FINISH_LINE_Y:
21+
self.goto(STARTING_POSITION)
22+
self.pace *= 0.9
23+
return True
24+
return False

Day-23-CrossyRoad/scoreboard.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from turtle import Turtle
2+
ALIGNMENT = 'center'
3+
FONT = ("Courier", 20, "bold")
4+
POSITION = (-220,240)
5+
6+
class Scoreboard(Turtle):
7+
8+
def __init__(self):
9+
super().__init__()
10+
self.hideturtle()
11+
self.penup()
12+
self.goto(POSITION)
13+
self.score = 0
14+
self.write(f'Level: {self.score}', align=ALIGNMENT, font=FONT)
15+
16+
def increment(self):
17+
self.score += 1
18+
self.clear()
19+
self.write(f'Level: {self.score}', align=ALIGNMENT, font=FONT)
20+
21+
def game_over(self):
22+
self.goto(0,0)
23+
self.write(f'Game Over', align=ALIGNMENT, font=FONT)

0 commit comments

Comments
 (0)