import turtle, math width = 400 height = 400 # set up the screen to use coordinates I like # 8 pixels of padding all around the main coordinate system (for aesthetics) # (-8,-8) is in the upper left corner so that (0,0) is in a nice space. # one pixel is equal to one 'turtle' unit. def mysetup(): global sc,t pad = 8 screenw = width + 2*pad screenh = height + 2*pad turtle.setup(width=screenw, height=screenh) #make the window a good size turtle.resizemode('noresize') sc = turtle.getscreen() sc.setworldcoordinates(-1*pad, screenh, screenw, -1*pad) #set up my coordinates. sc.colormode(255) #color values range from 0 to 255 (inclusive) t = turtle.getturtle() t.speed(0) t.penup() t.hideturtle() sc.tracer(0,0) #speed up animation homeboy # tried to make this as fast as possible, # while still being able to specify size in pixels def draw_rect(pos, sz=10, color='black', fill=False): #3 default values. x = pos[0] y = pos[1] t.color(color) t.shape('square') if fill: t.begin_fill() t.goto(x,y) t.pendown() t.goto(x + sz, y) t.goto(x+sz, y+sz) t.goto(x, y+sz) t.goto(x,y) t.penup(); if fill: t.end_fill() # https://krazydad.com/tutorials/makecolors.php def prettyColor(k): pi = math.pi #so I don't have to keep typing 'math' def s(o, x): #inner function mag = 255 minbright = 128 return int(mag/2*(math.sin(o + 1/3*x)) + minbright) return s(0, k), s(pi*2/3, k), s(4*pi/3, k) # Call this function to draw the picture def main(): mysetup() sz = 10 for c in range(width * height // sz**2): x = c % (width // sz) y = c // (width // sz) draw_rect((x*sz,y*sz), sz = sz, color=prettyColor(c), fill=True) sc.update() #update screen sc.exitonclick() main()