100 Days Of Python - Day 23
Day 23
turtle crossing game
Create the turtle player
The turtle player is going to have the following properties:
- a shape of a turtle
- a color of black
- a starting position of (0, -280)
- a heading of 90
- a pen size of 1
- a speed of 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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.penup()
self.goto(STARTING_POSITION)
self.setheading(90)
self.speed("fastest")
def move(self):
self.forward(MOVE_DISTANCE)
def reset_position(self):
self.goto(STARTING_POSITION)
Create the cars
The cars are going to have the following properties:
- a shape of a square
- a color of a random color from the
COLORS
list - a starting position of a random position along the y-axis
- a heading of 180
- a pen size of 1
- a speed of 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# car_manager.py
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
Detect when the turtle player collides with a car
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# main.py
from turtle import Screen
from player import Player
from car_manager import CarManager
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.listen()
player = Player()
car_manager = CarManager()
screen.onkey(player.move, "Up")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.create_car()
car_manager.move_cars()
# Detect collision with car
for car in car_manager.all_cars:
if car.distance(player) < 20:
game_is_on = False
screen.exitonclick()
Detect when the turtle player has reached the top edge of the screen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# main.py
from turtle import Screen
from player import Player
from car_manager import CarManager
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.listen()
player = Player()
car_manager = CarManager()
screen.onkey(player.move, "Up")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.create_car()
car_manager.move_cars()
# Detect collision with car
for car in car_manager.all_cars:
if car.distance(player) < 20:
game_is_on = False
# Detect when turtle player has reached the top edge of the screen
if player.is_at_finish_line():
player.go_to_start()
car_manager.level_up()d
screen.exitonclick()
# 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.penup()
self.goto(STARTING_POSITION)
self.setheading(90)
self.speed("fastest")
def move(self):
self.forward(MOVE_DISTANCE)
def is_at_finish_line(self):
if self.ycor() > FINISH_LINE_Y:
return True
else:
return False
def go_to_start(self):
self.goto(STARTING_POSITION)
Create a scoreboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# scoreboard.py
from turtle import Turtle
FONT = ("Courier", 24, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.level = 1
self.hideturtle()
self.penup()
self.goto(-280, 260)
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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# main.py
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.listen()
player = Player()
car_manager = CarManager()
scoreboard = Scoreboard()
screen.onkey(player.move, "Up")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.create_car()
car_manager.move_cars()
# Detect collision with car
for car in car_manager.all_cars:
if car.distance(player) < 20:
game_is_on = False
# Detect when turtle player has reached the top edge of the screen
if player.is_at_finish_line():
player.go_to_start()
car_manager.level_up()
scoreboard.increase_level()
screen.exitonclick()
Detect when the turtle player collides with a car
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# main.py
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.listen()
player = Player()
car_manager = CarManager()
scoreboard = Scoreboard()
screen.onkey(player.move, "Up")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.create_car()
car_manager.move_cars()
# Detect collision with car
for car in car_manager.all_cars:
if car.distance(player) < 20:
game_is_on = False
scoreboard.game_over()
# Detect when turtle player has reached the top edge of the screen
if player.is_at_finish_line():
player.go_to_start()
car_manager.level_up()
scoreboard.increase_level()
screen.exitonclick()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# scoreboard.py
from turtle import Turtle
FONT = ("Courier", 24, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.level = 1
self.hideturtle()
self.penup()
self.goto(-280, 260)
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("GAME OVER", align="center", font=FONT)
This post is licensed under CC BY 4.0 by the author.