With Python 3.6 and the Turtle module, I would like to know how to print the coordinates of the turtle on screen.

My questions are: How do I print text on the screen, and how do I make that text the player's coordinates?

Here is my code.

from turtle import Turtle, Screenplay = Screen()play.bgcolor("black")play.screensize(250, 250)play.title("Turtle Keys")def position():coord = follow.coor()coord.color("white")coord.setposition(130, 100)run = Turtle("triangle")run.speed("fastest")run.color("white")run.penup()run.setposition(250, 250)print(position)

Thank you.

Edit I tried write, but it throws a name not defined error.

4

Best Answer


Here is how you can write the turtle position on screen: the write method from the turtle module, must be called on a Turtle object.

import turtleplayground = turtle.Screen() # use nouns for objects, play is a verbplayground.bgcolor("black")playground.screensize(250, 250)playground.title("Turtle Keys")tom = turtle.Turtle() # use nouns for objects, run is a verbtom.color("white")tom.setposition(130, 100)tom.speed("fastest")tom.color("white")p = tom.pos() # here you get the coordinates of the turtletom.write(str(p), True) # and you print a string representation on screentom.penup()print(p) # this prints to the consoleplayground.exitonclick()

enter image description here

The way I approach this type of problem is to dedicate a turtle to each fixed label on the screen. That way you can permanentely locate a turtle at that position and simply do undo() to clear the old text followed by write() to write the new.

Here's a dynamic example where you can drag the turtle around the screen with your mouse and the current position will be written to the center of the screen:

from turtle import Turtle, ScreenFONT = ('Arial', 24, 'normal')playground = Screen()playground.setup(500, 500)playground.bgcolor("black")runner = Turtle("triangle")runner.color("white")runner.speed("fastest")runner.penup()runner.setposition(200, 200)marker = Turtle(visible=False) # our virtual magic markermarker.penup()marker.color("yellow")marker.write(runner.position(), align='center', font=FONT) # so we can undo itdef drag_handler(x, y):runner.ondrag(None) # disable handler while in handlermarker.undo() # erase previous positionmarker.write((x, y), align='center', font=FONT)runner.setheading(runner.towards(x, y)) # head towards new locationrunner.setposition(x, y)runner.ondrag(drag_handler)runner.ondrag(drag_handler)playground.mainloop()

enter image description here

Use the functions xcor() and ycor() to get coordinates. Use the function write() to write on the screen.

See documentation: https://docs.python.org/3/library/turtle.html

Maybe this will help:

def write_pos(self, position):self.pu()self.bk(len(str(position))-len(str(position))/2)self.pd()self.write(str(position))self.pu()self.fd(len(str(position))-len(str(position))/2)self.pd()write_pos(run, run.position())

Ok. Here's the explanation.

So I defined a function writing the position of something (in this case, the turtle).

When I do:

self.pu()self.bk(len(str(position))-len(str(position))/2)

I:

  • Lift the pen up and
  • Move backward some steps, so I can center the text.

I then do:

self.pd()self.write(str(position))

Which:

  • Puts the pen down and
  • Writes (or outputs) the position

After that, when I do:

self.pu()self.fd(len(str(position))-len(str(position))/2)self.pd()

I:

  • Pick the pen up
  • Move it back to the original position and
  • Put the pen down again

EDIT:I just realized that you can do it a much more simple way of using write like this:

write(run.position(), False, center)

It writes the turtles position, it doesn't move, and it still writes the text but with the canter as the place where it aligns.

EDIT #2:

I already figured out the problem.

Your using write directly and not actually calling it from the object. You should call write like this:

run.write(run.position(), align="center")