Python 3.7 turtle drawings are wider than they should be

Viewed 81

I'm learning how to use Turtle for Python, so I'm drawing basic shapes for now. I noticed that when I try to draw a perfect square or other perfect polygon, the result looks "squished". The shape is always wider than it is tall, even though I have one set length value.

Is something wrong with my logic of how I draw the shapes?

Here's the code I have:

import turtle

bgColor = "teal"
worldX = 100
worldY = 100

turtle.screensize(bg=bgColor)
turtle.setworldcoordinates(0,worldX,worldY,0)
#Canvas is 100 x 100, with (0,0) being the upper left corner and (100,100) being the lower right corner

tr = turtle.Turtle()
tr.speed(6)
tr.color("black")

length = 5 #Length of sides you want
sides = 4 #Number of sides for shape

tr.penup()
tr.goto(worldX/2, worldY/2) #Go to center of canvas
tr.pendown()
for i in range(sides):
    tr.forward(length)
    tr.right(360/sides)
turtle.done()
1 Answers

I figured it out, it had to do with my screen size. If my screen size is more wide than tall, then my world coordinates are off. For example, suppose my screen size is 400x800. Because my screen is twice as wide compared to it's height, moving my turtle one point to the right or left would require the turtle to move twice the distance compared to moving the turtle one point up or down. Meaning that if I draw a 5x5 square based on my coordinate system, it would actually be a rectangle, even though it's 5x5 on my coordinate system.

Anyway, here's my new code that takes this into account:

import turtle
import random

bgColor = "teal"
worldX = 100
worldY = 100
screenX = 400
screenY = 400

screen = turtle.Screen()
screen.setup(screenX,screenY)

turtle.screensize(bg=bgColor)
turtle.setworldcoordinates(0,worldX,worldY,0)
#Canvas is 100 x 100, with (0,0) being the upper left corner and (100,100) being the lower right corner

print(turtle.screensize())
tr = turtle.Turtle()
tr.speed(6)
tr.color("black")

length = 20 #Length of sides you want
sides = 4 #Number of sides for shape
tr.penup()
tr.goto(worldX/2, worldY/2) #Go to center of canvas
tr.pendown()
for i in range(sides):
    tr.forward(length)
    tr.right(360/sides)
turtle.done()
Related