-1

I'm trying to make a pointer(turtle) look at the moving direction, but it doesn't work properly. I debugged it, and it gave me the correct angle, but when i'm trying to turn my turtle to this angle - it doesnt work and turtle is in the same position. Please help me to solve this problem. Here is my code -

import pyautogui as cor
import turtle as pen
from pynput.mouse import Listener


def on_click(x, y, button, pressed):
    if pressed:
        x, y = cor.position()
        print("x =", x, ",", "y =", y)
        degree = pen.towards(x, y)
        print(degree)
        pen.setheading(degree)
        pen.goto(x-969, -y+575)


def on_scroll(x, y, dx, dy):
    if dy > 0:
        print('pen up')
        pen.up()

    if dy < 0:
        print('pen down')
        pen.down()


with Listener(on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()

martineau
  • 119,623
  • 25
  • 170
  • 301
krvvko
  • 11
  • 2
  • It's possible the the `turtle` module is incompatible with `pyautogui` and/or `pynput`. I would suggest running some very simple tests to see. – martineau Apr 22 '21 at 19:25
  • Got it. Is there any other libraries that can track my mouse coordinates, and can track clicks compatible with turtle? – krvvko Apr 22 '21 at 19:31
  • Well, there's a [`turtle.onclick()`](https://docs.python.org/3/library/turtle.html#turtle.onclick) and answers to the question [turtle - How to get mouse cursor position in window?](https://stackoverflow.com/questions/35732851/turtle-how-to-get-mouse-cursor-position-in-window). – martineau Apr 22 '21 at 19:44

1 Answers1

0

Let's start simple and completely within turtle:

from turtle import Screen, Turtle

def on_click(x, y):
    turtle.setheading(turtle.towards(x, y))

turtle = Turtle()

screen = Screen()
screen.onclick(on_click)
screen.mainloop()

Wherever you click on the window, the arrow cursor (turtle) in the middle of the screen will turn to point at where you clicked.

What more are you trying to do beyond this?

cdlane
  • 40,441
  • 5
  • 32
  • 81