from turtle import * import math import random screen = Screen() screen.tracer(0) screen.addshape("track.gif") screen.bgpic("track.gif") screen.listen() racers = [] aicar = [] current_wp = {} bot_speed = {} bot_path = {} bot_maxspeed = {} bot_accel = {} bot_path_id = {} lap = {} passed_finish = {} hit = {} winner = None result_shown = False racestart = False def setup(): global names, waypoints, waypoints2, countdouner names = [ "RedBull", "Mclaren", "Ferrari", "Mercedes", "Racing Bulls", "AstonMartin", "Williams", "Haas", "Sauber", "Alpine" ] colors = [ "midnight blue", "orange", "red", "sky blue", "light gray", "forest green", "blue", "gray", "#50C878", "#FB47C7" ] drivers = list(zip(names, colors)) waypoints = [ (-335,179), (-322,208), (-296,229), (13,243), (69,216), (95,184), (109,129), (75,63), (20,80), (-22,90), (-71,77), (-67,42), (57,-93), (127,-121), (194,16), (197,158), (229,228), (292,156), (333,68), (338,52), (372,-62), (364,-138), (332,-234), (105,-242), (-87,-247), (-287,-261), (-316,-254), (-298,-185), (-340,-125), (-342,-38), (-339,52), (-345,122) ] waypoints2 = [ (-337,201), (-268,216), (-47,261), (75,196), (99,155), (104,54), (10,77), (-71,90), (-9,-55), (102,-130), (143,-135), (155,-54), (196,10), (186,214), (265,176), (377,-70), (369,-226), (237,-229), (58,-237), (-145,-271), (-310,-281), (-293,-226), (-342,-140), (-337,122) ] random.shuffle(drivers) for i in range(10): name, color = drivers[i] t = Turtle() t.shape("turtle") t.penup() t.color(color) t.nickname = name t.goto(-350 + (i // 5) * 20, 70 + (i % 5) * 15) t.setheading(90) racers.append(t) countdouner = Turtle() countdouner.hideturtle() countdouner.penup() countdouner.goto(0, -40) screen.update() def decideRacer(): global choice choice = screen.textinput( "Choose who you think will win", "Pick team you think will win:\n" + ", ".join(names) ) if choice is None: decideRacer() return choice = choice.lower() for idx, t in enumerate(racers): aicar.append(t) current_wp[t] = 0 if idx % 2 == 0: bot_path[t] = waypoints else: bot_path[t] = waypoints2 bot_speed[t] = random.randint(10, 20) bot_accel[t] = random.uniform(0.09, 0.21) bot_maxspeed[t] = random.randint(25, 30) lap[t] = 0 passed_finish[t] = False hit[t] = 0 def countdown(n): global racestart countdouner.clear() if n > 0: countdouner.write(str(n), font=("Arial", 60, "bold"), align="center") screen.ontimer(lambda: countdown(n - 1), 1000) else: countdouner.write("GO!", font=("Arial", 60, "bold"), align="center") screen.ontimer(start_race, 500) def start_race(): global racestart countdouner.clear() racestart = True move_bots() def move_bots(): if not racestart: return for bot in aicar: aim = current_wp[bot] path = bot_path[bot] tx, ty = path[aim] dx = tx - bot.xcor() dy = ty - bot.ycor() target_angle = math.degrees(math.atan2(dy, dx)) current = bot.heading() turn = (target_angle - current + 180) % 360 - 180 bot.setheading(current + turn * 0.15) dist = bot.distance(tx, ty) if dist < 25: bot_speed[bot] = max(3, bot_speed[bot] - bot_accel[bot]) else: bot_speed[bot] = min( bot_maxspeed[bot], bot_speed[bot] + bot_speed[bot] * bot_accel[bot] ) bot.forward(bot_speed[bot] * 0.1) if dist < 12: current_wp[bot] = (aim + 1) % len(path) lap_system(bot) bot_hitbox(bot) screen.update() screen.ontimer(move_bots, 16) def lap_system(bot): global racestart, winner, result_shown if current_wp[bot] == 0: if not passed_finish[bot]: lap[bot] += 1 passed_finish[bot] = True print(bot.nickname, "Lap:", lap[bot]-1) if lap[bot] == 4 and not result_shown: racestart = False winner = bot result_shown = True countdouner.clear() if choice == winner.nickname.lower(): countdouner.write("You Were Right!!!", font=("Arial", 60, "bold"), align="center") else: countdouner.write("You Were Wrong!!!", font=("Arial", 60, "bold"), align="center") else: passed_finish[bot] = False def bot_hitbox(bot): for other in aicar: if other is bot: continue if bot.distance(other) < 30: possibility = random.randint(1, 3) if possibility == 1 and bot.distance(other) < 15: dx = bot.xcor() - other.xcor() dy = bot.ycor() - other.ycor() angle = math.degrees(math.atan2(dy, dx)) bot.setheading(angle) bot.forward(4) hit[bot] += 1 if possibility >= 2: bot_speed[bot] = max(15, bot_speed[bot] - bot_accel[bot]) setup() decideRacer() countdown(3) screen.mainloop()