Turtle
from turtle import Turtle, Screen
timmy_the_turtle = Turtle()
timmy_the_turtle.shape("turtle")
timmy_the_turtle.color("Lightslateblue")
timmy_the_turtle.forward(100)
timmy_the_turtle.right(90)
screen = Screen()
screen.exitonclick()
.shape()
, .right()
.shape()
: Set turtle shape to shape with given name or, if name is not given, return name of current shape.
.right()
: Turn turtle right by angle units.
Draw a Square
for _ in range(4):
timmy_the_turtle.forward(100)
timmy_the_turtle.left(90)
timmy_the_turtle.forward(100)
timmy_the_turtle.left(90)
timmy_the_turtle.forward(100)
timmy_the_turtle.left(90)
timmy_the_turtle.forward(100)
timmy_the_turtle.left(90)
timmy_the_turtle.forward(100)
timmy_the_turtle.left(90)
Importing Modules
if using something from a module many times, so more than three times, then we can think about using from _ import _
.
but, if using only it once or twice, then just import the whole module and write out module.method()
import _ as _
what this means is that if to create new project from the module, instead of writing out module's name every single time, we can just write t(or any letter) and that it will represent the entire module.
Draw a Dashed Line
for _ in range(15):
tim.forward(10)
tim.penup()
tim.forward(10)
tim.pendown()
Drawing Different Shapes
import turtle as t
import random
tim = t.Turtle()
colours = ["CornflowerBlue", "Orchid", "IndianRed", "pink", "gold", "wheat", "SlateGray", "SeaGreen"]
def draw_shape(num_sides):
angle = 360 / num_sides
for _ in range(num_sides):
tim.forward(100)
tim.right(angle)
for shape_side_n in range(3, 10):
tim.color(random.choice(colours))
draw_shape(shape_side_n)
Random Walk
import turtle as t
import random
tim = t.Turtle()
colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
directions = [0, 90, 180, 270]
tim.pensize(15)
tim.speed("slow")
for _ in range(200):
tim.color(random.choice(colours))
tim.forward(30)
tim.setheading(random.choice(directions))
screen = t.Screen()
screen.exitonclick()
Tuple
a tuple is data type in Python and it looks like (1, 3, 8)
.
a tuple is going to be carved in stone so we can't change the value like we can with lists.
tuple change to list: list(tuple)
Random RGB colours
import turtle as t
import random
tim = t.Turtle()
t.colormode(255)
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
random_color = (r, g, b)
return random_color
directions = [0, 90, 180, 270]
tim.pensize(15)
tim.speed("fastest")
for _ in range(200):
tim.color(random_color())
tim.forward(30)
tim.setheading(random.choice(directions))
screen = t.Screen()
screen.exitonclick()
Draw a spirogragh
import turtle as t
import random
tim = t.Turtle()
t.colormode(255)
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_spirogragh(size_of_gap):
for _ in range(int(360 / size_of_gap)):
tim.color(random_color())
tim.circle(100)
tim.setheading(tim.heading() + size_of_gap)
draw_spirogragh(5)
screen = t.Screen()
screen.exitonclick()
Extract RGB Values from Images
interpreter "colorgram"
import colorgram
rgb_colors = []
colors = colorgram.extract('img.png', 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)
# output
[(202, 164, 110), (236, 239, 243), (149, 75, 50), (222, 201, 136), (53, 93, 123), (170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35), (145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77), (183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102), (12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102)]
Drawing the Dots
import turtle as turtle_module
import random
turtle_module.colormode(255)
tim = turtle_module.Turtle()
tim.speed("fastest")
tim.penup()
tim.hideturtle()
color_list = [(202, 164, 110), (149, 75, 50), (222, 201, 136), (53, 93, 123), (170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35), (145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77), (183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102), (12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102)]
tim.setheading(225)
tim.forward(300)
tim.setheading(0)
number_of_dots = 100
for dot_count in range(1, number_of_dots + 1):
tim.dot(20, random.choice(color_list))
tim.forward(50)
if dot_count % 10 == 0:
tim.setheading(90)
tim.forward(50)
tim.setheading(180)
tim.forward(500)
tim.setheading(0)
screen = turtle_module.Screen()
screen.exitonclick()
뭐야.. 개멋있어.....
이맛에 코딩하고 이맛에 파이썬 하나봅니다....