Turtle
005_Colored_Slashes.py
import random
import os
from util import getTurtleAndScreen
# Random Colored slashes
# CAC, 2023
width=800
height = 600
cellSize=50
numCols=width//cellSize
numRows=height//cellSize
filename = os.path.basename(__file__)[0:-3]
turtle, screen = getTurtleAndScreen("Colored Slashes",width,height,filename,moveWorld=True)
screen.bgcolor("beige")
turtle.pensize(2)
def drawForwardSlash(turtle,x,y,cellSize):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.goto(x + cellSize, y + cellSize)
def drawBackwardSlash(leon, x, y, cellSize):
leon.penup()
leon.goto(x + cellSize, y)
leon.pendown()
leon.goto(x, y + cellSize)
def randomColor():
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
return color
for row in range (0,numRows):
for col in range(0,numCols):
x = col*cellSize
y = row*cellSize
color=randomColor()
turtle.color(color)
if(random.randint(0,1)==0):
drawForwardSlash(turtle, x, y, cellSize)
else:
drawBackwardSlash(turtle,x,y,cellSize)
turtle.penup()
turtle.goto(0,0)
screen.mainloop()
|