Day 18-Turtle Graph & Dot painting


Posted by pei_______ on 2022-04-28

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


#  Drawing Different Shapes in random color

import random
import turtle
timmy = turtle.Turtle()

turtle.colormode(255)
timmy.shape("turtle")

def random_color():
    r = random.randint(150, 255)
    g = random.randint(150, 255)
    b = random.randint(150, 255)
    return (r, g, b)

for number_of_sides in range(3, 11):
    timmy.color(random_color())
    for walk_times in range(number_of_sides):
        timmy.right(360 / number_of_sides)
        timmy.forward(100)

screen = turtle.Screen()
screen.exitonclick()
#  Random walking in random color

timmy.speed("fastest")
timmy.pensize(10)

def random_turn():
    if random.randint(0,1) == 0:
        timmy.right(90)
    else:
        timmy.left(90)

for _ in range(1, 200):
    timmy.color(random_color())
    timmy.forward(20)
    random_turn()

# Use setheading() to substitude random_turn()

directions = [0, 90, 180, 270]
timmy.setheading(directions[random.randint(0, 3)])
#  Circle Graphic in random color

timmy.shape("classic")
timmy.speed("fastest")

def draw_spirograph(size_of_gap):
    for _ in range(int(360/size_of_gap)):
        timmy.color(random_color())
        timmy.circle(90)
        timmy.setheading(timmy.heading()+size_of_gap)

draw_spirograph(5)
## Draw Dot painting in random color


# Gram colors in a picture

import colorgram
colors = colorgram.extract('20_001.jpg', 20)
rgb_colors = []
for color in colors:
    r = color.rgb.r
    g = color.rgb.g
    b = color.rgb.b
    rgb_colors.append((r, g, b))
print(rgb_colors)


# Draw Dot painting

import turtle
from random import randint
turtle.colormode(255)
timmy = turtle.Turtle()
timmy.pensize(25)
timmy.hideturtle()
timmy.speed("fastest")

color_list = [(199, 175, 117), (124, 36, 24), (210, 221, 213), (168, 106, 57), (222, 224, 227), (186, 158, 53), (6, 57, 83), (109, 67, 85), (113, 161, 175), (22, 122, 174), (64, 153, 138), (39, 36, 36), (76, 40, 48), (9, 67, 47), (90, 141, 53), (181, 96, 79), (132, 40, 42), (210, 200, 151)]


def draw_a_row():
    for _ in range(1, 11):
        timmy.dot(20, color_list[randint(0, 17)])
        timmy.penup()
        timmy.forward(50)


for _ in range(1, 11):
    timmy.penup()
    if _ == 1:
        timmy.sety(-215)
    else:
        timmy.left(90)
        timmy.forward(50)
        timmy.right(90)
    timmy.setx(-230)
    draw_a_row()


screen = turtle.Screen()
screen.exitonclick()

#Python #課堂筆記 #100 Days of Code







Related Posts

讓我們來談談 CSRF

讓我們來談談 CSRF

關於 物件 Object - 取值、新增、刪除

關於 物件 Object - 取值、新增、刪除

變數(Variables)與型態(Types)

變數(Variables)與型態(Types)


Comments