day 23 - crossroads game

This commit is contained in:
Tanguy Deleplanque
2025-07-16 14:17:51 +02:00
parent 697d53f836
commit 59a3d6d830
4 changed files with 127 additions and 0 deletions

43
023/car_manager.py Normal file
View File

@ -0,0 +1,43 @@
import random
from turtle import Turtle
class CarManager:
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10
CAR_GENERATION_PROBABILITY = 5
def __init__(self):
self.cars = []
self.add_car()
def execute_round(self, level):
if self.should_generate_car_on_turn():
self.add_car()
speed = 5 + (level - 1) * 10
self.move(speed)
def should_generate_car_on_turn(self):
return random.randint(1,self.CAR_GENERATION_PROBABILITY) % self.CAR_GENERATION_PROBABILITY == 0
def move(self, speed):
for car in self.cars:
car.goto(x=car.xcor()-speed, y=car.ycor())
def add_car(self):
car = Turtle()
car.color(random.choice(self.COLORS))
car.shape("square")
car.shapesize(stretch_wid=1, stretch_len=2)
car.pu()
car.goto(x=300, y=random.randint(-260, 280))
self.cars.append(car)
def check_collision(self, player):
for car in self.cars:
if abs(player.ycor() - car.ycor()) < 10 and abs(player.xcor() - car.xcor()) < 30:
return True
return False