[Python] Turtle onscreenclick not behaving as expected

Discussão em 'Python' iniciado por Stack, Setembro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a basic Turtle Race. My only requirement is that the turtles must move with each click on the screen. I am trying to use onscreenclick to call a function that moves the turtles forward a random amount and then checks their x-coordinates to determine if someone has won.

    import turtle
    import random
    import time

    def raceRound():
    firstRacer.forward(random.randint(1, 10))
    secondRacer.forward(random.randint(1, 10))
    checkWinner()


    def firstRacerSetup():
    # setup turtle

    def secondRacerSetup():
    # set up second turtle

    def screenSetup():
    # create screen
    def checkWinner():
    if firstRacer.xcor() >= 80:
    announcer.write("Congratualtions! You won!")
    if secondRacer.xcor() >= 80:
    announcer.write("Sorry... You lost")

    wn = turtle.Screen()
    racerName = wn.textinput("Name Entry", "What would you like your turtle's name to be?")
    screenSetup()

    announcer = turtle.Turtle()
    announcer.hideturtle()
    announcer.penup()
    announcer.goto(0, -140)
    announcer.write("Welcome to the race!", align='center')
    time.sleep(3)

    firstRacerSetup()
    secondRacerSetup()
    announcer.write("Click to start the race!", align='center')

    wn.onscreenclick(raceRound)
    turtle.done()


    I'm just not sure where I am going wrong and some assistance would be greatly appreciated

    Continue reading...

Compartilhe esta Página