Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Turtle size in pixels

How do I get the dimensions of Python's turtle in pixels?

like image 365
d0n.key Avatar asked Oct 20 '25 22:10

d0n.key


1 Answers

The turtle shapes are stored in a dictionary in the TurtleScreen class where the key for shape square, for example, contains:

Shape("polygon", ((10,-10), (10,10), (-10,10), (-10,-10)))

All of the cursor shapes appear to fit within this 20 x 20 square. What I do when I program with shapesize() is the following:

STAMP_SIZE = 20
...
turtle.shapesize(size / STAMP_SIZE)

Where size is how large (or small) I want the cursor to be in pixels. This doesn't have to be uniform, sometimes I make lines by doing:

turtle.shapesize(size / STAMP_SIZE, 1 / STAMP_SIZE)  # vertical line

Remember that the width and length arguments to shapesize() are relative to the turtle's orientation so width is in the direction the turtle is pointing and length is perpendicular to that.

If you really want to know the dimensions of the current turtle cursor (including after any transformations), you can use turtle.get_shapepoly(). But this gives you a polygon so you'll need to figure out height, width, area or what it is you need from the polygon verticies.

like image 193
cdlane Avatar answered Oct 23 '25 13:10

cdlane