From eaebdba4bb1f672da08797ba0f04b8238117b650 Mon Sep 17 00:00:00 2001 From: Tanguy Deleplanque Date: Mon, 30 Jun 2025 12:43:42 +0200 Subject: [PATCH] day 19 - turtles again T_T --- 019/main.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 019/sandbox.py | 33 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 019/main.py create mode 100644 019/sandbox.py diff --git a/019/main.py b/019/main.py new file mode 100644 index 0000000..6ea9dce --- /dev/null +++ b/019/main.py @@ -0,0 +1,55 @@ +from turtle import Turtle, Screen +import random + +screen = Screen() + +screensize = screen.screensize() +current_top_position = -screen.window_width() / 2 + 20 +ending_point = -current_top_position + +colors = ["red", "green", "blue", "purple", "orange", "yellow", "black", "pink"] +turtles = [] +winner = None + +def init_turtles(turtles): + for i in range(8): + t = Turtle() + t.pu() + t.color(colors[i]) + t.shape("turtle") + t.teleport(x=current_top_position, y = i * 50 - 200) + turtles.append(t) + return turtles + +def race(turtles, current_top_position): + for i in range(8): + speed = random.randint(1,20) + turtles[i].forward(speed) + + if turtles[i].xcor() > current_top_position: + current_top_position = turtles[i].xcor() + if turtles[i].xcor() >= ending_point: + print("finished") + return current_top_position, i + + return current_top_position, None + + +user_bet = screen.textinput("Place your bet", "Which turtle color will you bet on?") + +init_turtles(turtles) +while winner == None: + current_top_position, winner = race(turtles, current_top_position) + + + + +winning_turtle = turtles[winner].color()[0] +print(f"Race ended ! Winner is {winning_turtle} !") + +if winning_turtle == user_bet: + print("You won your bet!") +else: + print("You lost your bet!") + +screen.exitonclick() \ No newline at end of file diff --git a/019/sandbox.py b/019/sandbox.py new file mode 100644 index 0000000..107e7dd --- /dev/null +++ b/019/sandbox.py @@ -0,0 +1,33 @@ +from turtle import Turtle, Screen + +turtle = Turtle() +screen = Screen() + +def move_forward(): + turtle.forward(10) + +def move_backward(): + turtle.backward(10) + +def rotate_right(): + heading = turtle.heading() - 10 + turtle.setheading(heading) + +def rotate_left(): + heading = turtle.heading() + 10 + turtle.setheading(heading) + +def clear(): + turtle.clear() + turtle.pu() + turtle.home() + turtle.pd() + + +screen.listen() +screen.onkey(move_forward, "Up") +screen.onkey(move_backward, "Down") +screen.onkey(rotate_left, "Left") +screen.onkey(rotate_right, "Right") +screen.onkey(clear, "c") +screen.exitonclick() \ No newline at end of file