Skip to content Skip to sidebar Skip to footer

Python Turtle Positional Errors

I've been trying to scale a Turtle drawing by a single axis and after some testing, I managed the following function: def DrawSquare(length=50.0, Yscale=2): setheading(0) f

Solution 1:

Although Python's turtle graphics provide all the usual transformations (scale, shear, tilt, etc.) for the turtle image itself, it doesn't provide them for the images it draws! Rather than add a scaling factor to every drawing routine you define, let's try to manipulate the image scale independent of your drawing routines:

from turtle import *
import time

SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400defDrawSquare(length=1):

    oldPos = pos()
    setheading(0)
    pendown()

    for n inrange(0, 4):
        forward(length)
        left(90)

    setpos(oldPos)

defScale(x=1, y=1):
    screen = Screen()
    screen.setworldcoordinates(- (SCREEN_WIDTH / (x * 2)), - (SCREEN_HEIGHT / (y * 2)), (SCREEN_WIDTH / (x * 2)), (SCREEN_HEIGHT / (y * 2)))

setup(SCREEN_WIDTH, SCREEN_HEIGHT)
mode("world")

penup()
goto(-25, -25)

# TESTS

Scale(1, 1) # normal size
DrawSquare(50)
time.sleep(2)

Scale(1, 2)  # twice as tall
time.sleep(2)

Scale(2, 1)  # twice as wide
time.sleep(2)

Scale(2, 2)  # twice as big
time.sleep(2)

Scale(1, 1)  # back to normal

done()

Just set Scale(1, 2) to make anything you draw twice as big in the Y dimension. Either before or after you draw it.

Post a Comment for "Python Turtle Positional Errors"