- 18.1 Turtle draw a square
from turtle import Turtle, Screen
tim = Turtle()
tom = Turtle()
tim.shape("turtle")
tom.shape("turtle")
tim.color("LimeGreen")
tom.color("blue")
for _ in range(4):
tim.forward(100)
tim.right(90)
for _ in range(4):
tom.forward(100)
tom.left(90)
screen = Screen()
screen.exitonclick()
- 18.2 Turtle draw a dashed line
import turtle as t
from turtle import Screen
tim = t.Turtle()
tim = t.shape("turtle")
tim = t.color("DarkMagenta")
for _ in range(15):
tim = t.down()
tim = t.forward(10)
tim = t.up()
tim = t.forward(10)
screen = Screen()
screen.exitonclick()
- 18.3 Turtle drawing different shapes
import random
import turtle as t
from turtle import Screen
from random import choice
tim = t.Turtle()
tim = t.shape("turtle")
colours = ["LimeGreen", "OrangeRed", "Indigo", "Yellow", "Blue", "Green", "Orange", "Purple", "Violet", "Red"]
def draw_shape(num_sides):
angle = 360 / num_sides
for _ in range(num_sides):
tim = t.forward(100)
tim = t.right(angle)
for shape_side_n in range(3, 11):
tim = t.color(random.choice(colours))
draw_shape(shape_side_n)
screen = Screen()
screen.exitonclick()
- 18.4 Turtle random walk
import turtle as t
import random
from turtle import Screen
tim = t.Turtle()
t.colormode(255)
tim.screen.bgcolor("grey")
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color_random = (r, g, b)
return color_random
direction = [0, 90, 180, 270]
tim.pensize(4)
tim.speed("fastest")
for _ in range(200):
tim.color(random_color())
tim.forward(30)
tim.setheading(random.choice(direction))
screen = Screen()
screen.exitonclick()
- 18.5 Draw a spirograph
import random
import turtle as t
from turtle import Screen
tim = t.Turtle()
t.colormode(255)
tim.screen.bgcolor("black")
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = (r, g, b)
return color
tim.speed("fastest")
def draw_spirograph(size_of_gap):
for i in range(int(360 / size_of_gap)):
tim.color(random_color())
tim.circle(100)
tim.setheading(tim.heading() + size_of_gap)
draw_spirograph(5)
screen = Screen()
screen.exitonclick()
Project The Hirst Painting
Part 1
import colorgram
# Installed colorgram.py using pip install
# Copied a Damien hurst polka dot image from google search and pasted to the project. Named as image.jpg
# Used the following code to extract colors from the copied image and create the list color_list
# Format the list and used https://www.w3schools.com/colors/colors_rgb.asp to check the colors as we don't want the background colors
rgb_colors = []
colors = colorgram.extract("image.jpg", 30)
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
new_color = (r, g, b)
rgb_colors.append(new_color)
print(rgb_colors)
color_list = [
(212, 149, 95), (215, 80, 62), (47, 94, 142), (231, 218, 92), (148, 66, 91), (22, 27, 40), (155, 73, 60),
(122, 167, 195), (40, 22, 29), (39, 19, 15), (209, 70, 89), (192, 140, 159), (39, 131, 91), (125, 179, 141),
(75, 164, 96), (229, 169, 183), (15, 31, 22), (51, 55, 102), (233, 220, 12), (159, 177, 54), (99, 44, 63),
(35, 164, 196), (234, 171, 162), (105, 44, 39), (164, 209, 187), (151, 206, 220)
]
- Part 2 (The code)
import random
import turtle as t
from turtle import Screen
tim = t.Turtle()
t.colormode(255)
tim.hideturtle()
tim.penup()
tim.speed("fastest")
tim.setheading(220)
tim.forward(330)
tim.setheading(0)
number_of_dots = 101
color_list = [
(212, 149, 95), (215, 80, 62), (47, 94, 142), (231, 218, 92), (148, 66, 91), (22, 27, 40), (155, 73, 60),
(122, 167, 195), (40, 22, 29), (39, 19, 15), (209, 70, 89), (192, 140, 159), (39, 131, 91), (125, 179, 141),
(75, 164, 96), (229, 169, 183), (15, 31, 22), (51, 55, 102), (233, 220, 12), (159, 177, 54), (99, 44, 63),
(35, 164, 196), (234, 171, 162), (105, 44, 39), (164, 209, 187), (151, 206, 220)
]
for dot_count in range(1, number_of_dots):
tim.dot(20, random.choice(color_list))
tim.forward(50)
if dot_count % 10 == 0:
tim.left(90)
tim.forward(50)
tim.setheading(180)
tim.forward(500)
tim.setheading(0)
screen = Screen()
screen.exitonclick()
Top comments (0)