1

I am trying use tiltangle, hovewer I cannot understand how it works exactly. Everytime I run following code, it gives 122.0, I do not know how can I deal with it:

def collide(self):
    initial_angle = self.tiltangle()
    self.right(initial_angle + 90)

How can I get angle of an object in turtle library of Python?

cdlane
  • 40,441
  • 5
  • 32
  • 81
Faruk
  • 23
  • 3
  • 4
    Please add a link to "turtle library of python" and full code which would run if copy pasted [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example). Because now it not clear what `def collide(self):` and `self.tiltangle()` means – Combinacijus Mar 13 '23 at 13:01
  • What is not clear from [the docs](https://docs.python.org/3/library/turtle.html#turtle.tiltangle): *"return the current tilt-angle, i. e. the angle between the orientation of the turtleshape and the heading of the turtle (its direction of movement)."*? – Tomerikoo Mar 13 '23 at 13:25
  • If you're subclassing `Turtle`, I suggest [not doing so](https://stackoverflow.com/a/71003069/6243352). But please share a [mcve] so we have full context for what you're doing here. Thanks. – ggorlen Mar 13 '23 at 17:35

2 Answers2

1

You can refer to this docs.

Let's take the following description.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.

That means, if you get the value of 122.0 every single time, it is because your initial_angle value is 32.0.

So, the value changes depending on the side of an angle. If you change the script like:

self.left(initial_angle + 90)

Then, you would get the value of 58.0.

HSL
  • 148
  • 6
0

You want heading(), not tiltangle(). The heading() method gives you the direction that the turtle is currently facing, which you can change with left(), right(), or setheading().

The tiltangle() method is for changing the visual appearance of the cursor, not its actual heading on the screen. For example, in a Space Invaders style game, I've used the turtle-shaped cursor with a heading of 0 (zero) degrees so I can use forward() and backward() to move it on the screen. But I want the turtle image itself to point upward towards the invaders. So, I set it's tiltangle() to 90 so it's facing upward but moving left and right.

def collide(self):
    initial_angle = self.heading()
    self.right(initial_angle + 90)

is the same as:

def collide(self):
    self.right(90)

As noted in the documentation, tiltangle() is "the angle between the orientation of the turtleshape and the heading of the turtle (its direction of movement)"

cdlane
  • 40,441
  • 5
  • 32
  • 81