Repository navigation
Expand file tree
/
Copy pathturtle_cross_game
More file actions
148 lines (97 loc) · 2.62 KB
/
Copy pathturtle_cross_game
File metadata and controls
148 lines (97 loc) · 2.62 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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.car_list = []
def create_car(self):
y = random.randint(-250, 250)
car = Turtle(shape='square')
car.color(random.choice(COLORS))
car.shapesize(stretch_wid=1, stretch_len=2)
car.penup()
car.goto(310,y)
self.car_list.append(car)
def drive(self):
for i in self.car_list:
i.backward(MOVE_INCREMENT)
def increase_speed(self):
global MOVE_INCREMENT
MOVE_INCREMENT += 1
def collision(self,player,scoreboard):
global MOVE_INCREMENT
for j in self.car_list:
if player.distance(j) < 30:
MOVE_INCREMENT = 0
player.game_over()
scoreboard.game_over()
# 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.penup()
self.setheading(90)
self.goto(STARTING_POSITION)
def up(self):
self.forward(MOVE_DISTANCE)
def reset(self):
self.goto(STARTING_POSITION)
def game_over(self):
global MOVE_DISTANCE
MOVE_DISTANCE = 0
#scoreboard.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.penup()
self.setheading(90)
self.goto(STARTING_POSITION)
def up(self):
self.forward(MOVE_DISTANCE)
def reset(self):
self.goto(STARTING_POSITION)
def game_over(self):
global MOVE_DISTANCE
MOVE_DISTANCE = 0
# main.py
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)
player = Player()
car_manager = CarManager()
scoreboard = Scoreboard()
screen.listen()
screen.onkey(player.up,'Up')
game_is_on = True
counter = 0
while game_is_on:
time.sleep(0.1)
screen.update()
if counter % 6 == 0:
car_manager.create_car()
car_manager.drive()
car_manager.collision(player,scoreboard)
if player.ycor() > 270:
scoreboard.next_level()
player.reset()
car_manager.increase_speed()
counter += 1
screen.exitonclick()