Repository navigation
Expand file tree
/
Copy pathcar_manager.py
More file actions
52 lines (40 loc) · 1.56 KB
/
Copy pathcar_manager.py
File metadata and controls
52 lines (40 loc) · 1.56 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
from turtle import Turtle
import random
## Resources
# Code is from and inspired by with some improvements:
# 100 Days of Code: The Complete Python Pro Bootcamp
# https://www.udemy.com/share/103J8C3@0R0_ZWb7kJ4do0X-4zRsulmwTyefJndLSF7-DCQhp_3ojIbfiWmdBIvIMOalNv_ECg==/
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10
class CarManager:
"""
A class to manage the cars in the Turtle Crossing game.
Handles the creation, movement, and speed of the cars.
Attributes:
all_cars (list): A list to store all the car objects.
car_speed (int): The current speed of the cars.
Methods:
create_car(): Creates a new car at a random y-position and adds
it to the list of cars.
move_cars(): Moves all the cars to the left by the current car speed.
level_up(): Increases the speed of the cars.
"""
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