How can I make my turtle move to my cursor?

Robotechnology

I am trying to move the turtle to my cursor to draw something so that every time I click, the turtle will go there and draw something.

I have already tried onscreenclick(), onclick, and many combinations of both, I feel like I am doing something wrong, but I don't know what.

from turtle import*
import random
turtle = Turtle()
turtle.speed(0)
col  = ["red","green","blue","orange","purple","pink","yellow"]
a = random.randint(0,4)
siz = random.randint(100,300)
def draw():
    for i in range(75):
        turtle.color(col[a])
        turtle.forward(siz)
        turtle.left(175)

TurtleScreen.onclick(turtle.goto)

Any help would be great, thank you for your time ( If you help me! ;)

cdlane

It's not so much what method you're invoking, but on what object you're invoking it:

TurtleScreen.onclick(turtle.goto)

TurtleScreen is a class, you need to call it on a screen instance. And since you want to call draw in addition to turtle.goto you need to define your own function that calls both:

screen = Screen()
screen.onclick(my_event_handler)

Here's a rework of your code with the above fixes and other tweaks:

from turtle import Screen, Turtle, mainloop
from random import choice, randint

COLORS = ["red", "green", "blue", "orange", "purple", "pink", "yellow"]

def draw():
    size = randint(100, 300)

    # make turtle position center of drawing
    turtle.setheading(0)
    turtle.setx(turtle.xcor() - size/2)

    turtle.pendown()

    for _ in range(75):
        turtle.color(choice(COLORS))
        turtle.forward(size)
        turtle.left(175)

    turtle.penup()

def event_handler(x, y):
    screen.onclick(None)  # disable event handler inside event handler

    turtle.goto(x, y)

    draw()

    screen.onclick(event_handler)

turtle = Turtle()
turtle.speed('fastest')
turtle.penup()

screen = Screen()
screen.onclick(event_handler)

mainloop()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make my character move only forward in Unity?

How can I just make my widget move in Kivy?

How can I move my cursor horizontally by whitespace in VS-Code?

How can i make my header not move along with my slides in fullpagejs

How do I make my monster move randomly in my game

How can I change the size of my python turtle window?

How can I change the speed that my turtle rotates?

How can I fix my Spinner and move on

How can I move my /boot partition?

How can I make keyboard cursor move faster?

How can i move my function to the top center of my page?

How can I move my icons to the left in my HTML layout?

How can i make my HTML canvas dots move more naturally?

How can I make my python script wait for a shutil.move to be actually completed?

How can I change the direction of my CSS underline transition and make it move from right to left?

How can I make my anchor tags move to the right side of navbar?

How can I make my player move whilst still being able to rotate?

How can I make my lines line up, move less then 1px?

How can I make my Unity cameraController move overhead or below instead of clamping to 90/-90 degrees?

How can I make my loop move on to the next workbook without repeating itself in the first opened workbook?

How can I make all my sprites move instead of stopping when a new one spawns?

How I can make my constructor synchronized?

How can I make my REGEX general?

How can i make my variable defined?

How can I make my program faster?

How can I make my HorizontalBarChart scrollable?

How do I make my GridLayout clickable and will move to another activity?

How do I make my player object move in the direction it is facing?

How do i make my progress bar move smoothly?