DEV Community

dongdiri
dongdiri

Posted on

DAY13: Python day 18

Exploration of Turtle

  • successfully built geometric shapes generator, random walk generator, spirograph drawer
  • I particularly liked the random walk generator the most
timmy = Turtle()
turtle.colormode(255)

direction = [0, 90, 180, 270]
timmy.pensize(10)
timmy.speed(0)
while True:
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    timmy.pencolor(r, g, b)
    timmy.setheading(choice(direction))
    timmy.forward(20)
Enter fullscreen mode Exit fullscreen mode

Various ways to import packages

  • import
  • from...import
  • from...import* (not recommended)
  • import...as... (aliasing)

Python Tuples

  • like a list but in ()

  • wrote in stone(immutable)

End of the lesson project: Hirst Painting

import colorgram
from turtle import Turtle, Screen
import turtle
from random import choice
turtle.colormode(255)
# rgb_colors = []
# colors = colorgram.extract('image.jpg', 18 * 20)
# 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)

color_list =[(236, 224, 80), (197, 7, 71), (195, 164, 13), (201, 75, 15), (231, 54, 132), (110, 179, 216), (217, 163, 101), (27, 105, 168), (35, 186, 109), (19, 29, 168), (13, 23, 66), (212, 135, 177), (233, 223, 7), (199, 33, 132), (13, 183, 212), (230, 166, 199), (126, 189, 162), (8, 49, 28), (40, 132, 77), (128, 219, 232), (58, 12, 25), (67, 22, 7), (114, 90, 210), (146, 216, 199), (179, 17, 8), (233, 66, 34), (11, 97, 52), (169, 181, 232), (241, 169, 155), (252, 7, 40), (10, 84, 100), (63, 98, 8), (14, 51, 250), (250, 11, 8)]

timmy = Turtle()
current_x = -200
current_y = -200
timmy.shape("turtle")
timmy.penup()
timmy.hideturtle()
timmy.goto(current_x, current_y)
timmy.pensize(20)

for _ in range(10):
    for _ in range(10):
        timmy.pencolor(choice(color_list))
        timmy.pendown()
        timmy.speed(0)
        timmy.forward(0)
        timmy.penup()
        timmy.forward(50)
    current_y += 50
    timmy.goto(current_x, current_y)


screen = Screen()
screen.exitonclick()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)