pythonturtle-graphics

How do I add the right amount of padding for my turtle function?


I wrote some python code that uses turtle/screen to draw a Hirst painting and i'm having some trouble getting the padding around where i am starting the turtle and drawing the dots.

Ideally, I would like about 50 spaces of padding so the screen is evenly spaced out around where the turtle is rendering the dots. I have tried a bunch of combinations but cant quite wrap my brain around the math of: ts.screensize(500, 500) ts.setworldcoordinates(0, 0, 450, 450) which has the turtle kind of hugging the edges of the screen instead of having an even space of 50 all around where it is drawing. here is the total code:

#import/set up turtle/screen
from turtle import Turtle, Screen
import random

t = Turtle()
ts = Screen()
ts.resetscreen()

t.shape("turtle")
t.color("DarkBlue")
ts.colormode(255)
#TODO 1. fix screen size/turt pos
ts.screensize(500, 500)
ts.setworldcoordinates(0, 0, 450, 450)

#generate random colors from color_list
def random_color():
    color_list = [(118, 185, 170), (115, 160, 200), (215, 228, 239), (192, 137, 173), (237, 225, 205), (190, 220, 208)]
    rand_color = random.choice(color_list)
    return rand_color

#maps out movement
#paints random colors with movement
t.up()
t.speed(0)

def each_line():
    t.dot(20, random_color())
    t.fd(50)

def go_back_up1():
    t.left(90)
    t.fd(50)
    t.left(90)
    t.fd(500)
    t.right(180)

for _ in range(10):
        for _ in range (10):
            each_line()
        go_back_up1()


#TODO 5. possible stretch goal:
#           pt1. export render as JPG
#           pt2. export render as high or low quality




Screen().getcanvas().winfo_toplevel().attributes("-topmost", True)
screen = Screen()
screen.exitonclick()  

Solution

  • If you need extra 50 around then you should add/substract it from current size

    ts.setworldcoordinates(0-50, 0-50, 450+50, 450+50)
    

    enter image description here