[Python] How can I move the blue turtle to x = 280 using repeated upward semicircular ‘hops’...

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I’m building a turtle race. I want the blue turtle to move to x=280 by repeating this pattern:


    • draw a downward semicircle (“bowl”)


    • then a short straight stride to the right


    • repeat until it reaches x=280

    What I tried either makes slanted lines or stacks bowls vertically. How do I implement the hop cleanly?

    from turtle import Turtle, Screen
    import random

    # These are the Functions that are later used in the code to call and make changes in the track of the turtles
    def move_right(color, angle):
    for turtle in all_turtles:
    if turtle.pencolor() == color:
    turtle.right(angle)
    turtle.fd(10)
    turtle.setheading(0)


    def move_back(color):
    for turtle in all_turtles:
    if turtle.pencolor() == color:
    turtle.setheading(-180)
    turtle.fd(15)
    turtle.seth(0)


    def move_in_circle(color, angle):
    for turtle in all_turtles:
    if turtle.pencolor() == color:
    turtle.circle(50, angle)
    turtle.setheading(0)


    # Now adding a pop up of taking user entry in a pop up window.
    # will be using above Screen pop up as eg.

    is_race_on = False
    scn = Screen()
    scn.setup(width=900, height=700)
    user_bet = scn.textinput(title='Make your Bet',
    prompt='What color Turtle would you like? \nEnter Please: ')

    colors = ['red', 'orange', 'black', 'blue', 'yellow', 'pink']
    y_pos = [-70, -10, -40, 20, 50, 80]
    all_turtles = []
    start_x, finish_x = -280, 280

    for c in range(0, 6):
    new_turtle = Turtle(shape='turtle')
    new_turtle.speed('fastest')
    new_turtle.color(colors[c])
    new_turtle.penup()
    new_turtle.goto(x=start_x, y=y_pos[c])
    all_turtles.append(new_turtle)

    if user_bet:
    is_race_on = True

    red_count = 0
    blu_count = 0
    blue_nudged_back = False

    while is_race_on:
    for turtles in all_turtles:
    if turtles.xcor() >= finish_x:
    is_race_on = False
    winning_color = turtles.pencolor()

    if winning_color == user_bet:
    print(f"You've Won!!! {winning_color} is the winner")
    break
    else:
    print(f"You've Lost, Sorry {winning_color} is the winner")
    break

    rand_distance = random.randint(0, 10)
    turtles.forward(rand_distance)

    if turtles.pencolor() == 'red':
    turtles.forward(1)

    if red_count < 3:
    move_right(color='red', angle=45)
    red_count += 1
    if red_count == 3:
    turtles.pendown()

    if turtles.pencolor() == 'blue' and blu_count < 1:
    if not blue_nudged_back:
    move_back('blue')
    blue_nudged_back = True
    move_in_circle('blue', 90)
    blu_count += 1

    scn.exitonclick()

    Continue reading...

Compartilhe esta Página