Turtle
040_Quartered_Squares.py
import random
import os
from palettes import randomPalette
from util import getTurtleAndScreen
# Quartered Squares
# CAC, 4/19/2023
def fillSquare(turtle,x,y,radius):
turtle.penup()
turtle.goto(x-radius,y-radius)
turtle.pendown()
turtle.begin_fill()
for i in range(4):
turtle.forward(2*radius)
turtle.left(90)
turtle.end_fill()
def fillSquareLL(turtle,x,y,side):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
for i in range(4):
turtle.forward(side)
turtle.left(90)
turtle.end_fill()
def fillQuarteredSquare(turtle,x,y,side,colors):
rad = side//2
turtle.color(random.choice(colors))
fillSquareLL(turtle,x-rad,y-rad,rad)
turtle.color(random.choice(colors))
fillSquareLL(turtle,x-rad,y,rad)
turtle.color(random.choice(colors))
fillSquareLL(turtle,x,y,rad)
turtle.color(random.choice(colors))
fillSquareLL(turtle,x,y-rad,rad)
def Draw():
title = "Quartered Squares"
width = 800
height = 600
cellSize = 100
numCols = width // cellSize
numRows = height // cellSize
filename = os.path.basename(__file__)[0:-3]
turtle, screen = getTurtleAndScreen(title,width,height,filename,moveWorld=True)
colors = randomPalette()
turtle.pensize(1)
for row in range (0,numRows):
for col in range(0,numCols):
radius = cellSize//2
x = col*cellSize + radius
y = row*cellSize + radius
newSide = cellSize*.9
fillQuarteredSquare(turtle, x, y, newSide, colors)
turtle.penup()
turtle.goto(0,0)
screen.mainloop()
# ----------------------------------------------------------------
if __name__ == '__main__':
Draw()
|