TIL: Python Basics Day 18 - Importing Modules, Turtle Projects

이다연·2020년 12월 27일
0

Udemy Python Course

목록 보기
18/64

Importing Modules

from random import *

avoid using asterisk *,as to its origin, it's very confusing

If you are going to use the module mutiple times,

from turtle import Turtle()
tom = Turtle()

If it's for just once, specify the module name, which contains the class when making a new object. To make things clear, expressive

import turtle
tim = turtle.Turtle()

Aliasing Modules

from turtle as t
terry = t.Turtle()

Tuple

  • Tuple : you cannot change the value. Immutable
  • why using it?: for the values, you don't want others to change at all
  • change to list: list(my_tuple)

Turtle project

Goal was to read the documentation and find the solution. I used command + F for the keyword searching. It was an interesting topic. It was useful to recap my knowledge.

from turtle import Turtle, Screen
import turtle
tim = Turtle()
tim.shape("classic")

import random

# random colour list
#colour = ["DarkSeaGreen1", "pink", "coral", "CadetBlue", "grey", "cyan"]


tim.width(2)
tim.speed(50)
direction = [0, 90, 180, 270]

#----------RGB colours -----------------------
turtle.colormode(255)
def random_color():
    r = random.randint(0, 150)
    g = random.randint(0, 150)
    b = random.randint(0, 250)
    tu = (r, g, b)
    return tu

#----------Spirograph -----------------------
 def draw_spiro(number):
     tim.circle(100)
     tim.setheading(number)
     number += 10

number = 10
for i in range(100):
    tim.pencolor(random_color())
    tim.circle(100)
    tim.setheading(number)
    number += 15



 for i in range(200):
     tu = random_color()
     tim.pencolor(tu)
     tim.forward(20)
     tim.setheading(random.choice(direction))



#-----------drawing Polygons---------------------------------
# def shapes(angle):
#     shape = 360/angle
#     for i in range(angle):
#         tim.forward(50)
#         tim.right(shape)
#
# for angle in range(3, 11):
#     tim.color(random.choice(colour))
#     shapes(angle)



screen = Screen()
screen.exitonclick()

turtle art 1. Spirograph

turtle art 2. Random Walk

turtle art 3.Polygons

Final Project. turtle art 4. Damien Hurst's Dots

It's quite satisfying looking at my turtle working hard lol

  • I downloaded an image of "Dots" by Damien Hurst. By using colorgram, which helps me to extract colours used frequently in the image.
  • I extracted RGB values and made it into a tuple format.
  • I looped through coloured dots randomly to create a masterpiece.

import colorgram
from turtle import Turtle, Screen
import random
import turtle

# colors = colorgram.extract('colordots.jpg', 10)
#
# rgb_colors = []
# 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)
#     # rgb = color.rg
#     # rgb_colors.append(rgb)
#
#
# print(rgb_colors)
#


# first_color = colors[0]
# rgb = first_color.rgb
# print(rgb)

tur = Turtle()

# tur.shape("circle")
# tur.speed(5)
# tur.dot(20, "blue")
# tur.color("blue")
# tur.stamp()
# tur.forward(120)
color_list = [(202, 216, 243), (138, 165, 199), (216, 147, 109), (30, 39, 60), (205, 135, 143), (52, 107, 153), (140, 182, 165)]



turtle.colormode(255)

def random_color():
    num = random.randint(0, 6)
    co = color_list[num]
    return co

def dots():
    for dot in range(10):
        tur.pencolor(random_color())
        tur.dot(20)
        tur.penup()
        tur.fd(50)

tur.shape("turtle")
tur.penup()
i = -200
for dot in range(10):
    tur.setposition(-200, i)
    dots()
    i += 50

tur.hideturtle()


screen = Screen()
screen.exitonclick()
profile
Dayeon Lee | Django & Python Web Developer

0개의 댓글