개발 꿀팁/PYTHON

파이썬은 여러 가지 재미있는 그림과 소스를 그립니다

Jammie 2022. 12. 13. 14:42
반응형

Turtle 라이브러리는 Python 언어에서 널리 사용되는 그래픽 매핑의 함수 라이브러리로 가로축이 x이고 세로축이 y인 좌표계의 원점(0,0) 위치에서 시작하는 작은 거북이를 상상하며 함수 명령의 제어에 따라 이 평면 좌표계를 이동하여 기어가는 경로에 그래픽을 그립니다

1.안드로이드 소인이요

#!/usr/bin/env python
import turtle
aj=turtle.Pen()
y=0
aj.speed(5)
#turtle.screensize(200,800)
turtle.bgcolor("black")
#aj.shape("turtle")
def head():
    aj.color("green")
    aj.fd(160)
    x=aj.xcor()
    aj.seth(90)
    aj.begin_fill()
    #aj.color("green")
    aj.circle(x/2,180)
    aj.end_fill()
    aj.penup()
    aj.goto(33,37)
    aj.pendown()
    aj.dot(13,"black")
    aj.penup()
    aj.goto(126,37)
    aj.pendown()
    aj.dot(13,"black")
    aj.penup()
    aj.home()
    aj.pendown()
    aj.hideturtle()
    aj.fd(160)
    aj.seth(90)
    aj.circle(x/2,60)
    aj.right(90)
    aj.pensize(5)
    aj.fd(30)
 
    aj.penup()
    aj.home()
    #aj.pendown()
    aj.hideturtle()
    aj.fd(160)
    aj.seth(90)
    aj.circle(x/2,120)
    aj.right(90)
    aj.pensize(5)
    aj.pendown()
    aj.fd(30)
    aj.penup()
    aj.home()
    aj.penup()
 
def body():
    aj.pensize(0)
    aj.home()
    aj.showturtle()
    aj.goto(0,-7)
    aj.pendown()
    aj.begin_fill()
    aj.fd(160)
    aj.right(90)
    aj.fd(120)
    aj.right(90)
    aj.fd(160)
    y=aj.ycor()
    aj.right(90)
    aj.fd(120)
    aj.end_fill()
 
def legs():
    aj.penup()
    #turtle.color("red")
    aj.goto(33,-169)
    aj.pendown()
    aj.pensize(32)
    aj.fd(43)
    aj.penup()
    aj.goto(130,-169)
    aj.pendown()
    aj.fd(43)
    aj.penup()
def hands():
    aj.home()
    aj.pensize(30)
    aj.goto(-18,-77)
    aj.pendown()
    aj.left(90)
    aj.fd(65)
    aj.penup()
    aj.goto(179,-77)
    aj.pendown()
    aj.fd(65)
    aj.penup()
    aj.hideturtle
    aj.fd(100)
    aj.hideturtle()
    aj.circle(100)
    aj.circle(100,360,59)
    aj.reset()
    turtle.bgcolor("black")
    turtle.pencolor("green")
    turtle.hideturtle()
    turtle.goto(-300,0)
    aj.hideturtle
    turtle.write("Thank you for watching....", font = ("Bodoni MT Black", 28, "bold"))
    turtle.penup()
    turtle.goto(-40,-170)
    turtle.pendown()
    turtle.pencolor("yellow")
    turtle.write("Developed by 버그 잘 쓰는 안태늑대입니다", font = ("Palatino Linotype", 22, "bold"))
head()
body()
legs()
hands()
turtle.done()

효과도:

2. 드래곤 커브 (Dragon Curve) 입니다.
프랙탈 용이라고도 하며, 자기 유사 파쇄 곡선의 총칭으로 용의 굽이굽이 구부러져 있다고 해서 붙여진 이름입니다

# -*- coding: utf-8 -*-
 
from turtle import *
length = 5
angle  = 90
setup(1280,720)
up()
goto(300,-100)
 
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'f':
            import random
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            fd(length)
        elif symbol == '-':
            lt(angle)
        elif symbol == '+':
            rt(angle)
 
def apply_path(rules,path):
    lit = [x for x in path]
    for i in range(len(lit)):
        symbol = lit[i]
        if symbol == 'x':
            lit[i] = rules[symbol]
        elif symbol == 'y':
            lit[i] = rules[symbol]
    path = ''.join(lit)
    return path
 
rules = {
    'x':'x+yf+',
    'y':'-fx-y'
}
path = 'fx'
speed(0)
for i in range(13):
    path = apply_path(rules,path)
draw_path(path)
done()

효과도:

3. 앵두나무입니다.

# # -*- coding: utf-8 -*-

import turtle

toplevel = 8 # 총 6계층 재귀입니다
angle = 30
rangle = 15


def drawTree(length, level):
turtle.left(angle) # 왼쪽 가지를 그립니다
turtle.color("black")
turtle.forward(length)

if level == toplevel: # 잎입니다
turtle.color("pink")
turtle.circle(2, 360)

if level < toplevel: # 왼쪽 가지를 되돌리기 전에 재귀합니다.
drawTree(length - 10, level + 1)
turtle.back(length)

turtle.right (angle + rangle) # 오른쪽 가지를 그립니다
turtle.color("black")
turtle.forward(length)

if level == toplevel: # 잎입니다
turtle.color("pink")
turtle.circle(2, 360)

if level < toplevel: # 오른쪽 가지를 되돌리기 전에 재귀합니다.
drawTree(length - 10, level + 1)
turtle.color("black")
turtle.back(length)
turtle.left(rangle)


turtle.left(90)
turtle.penup()
turtle.back(300)
turtle.pendown()
turtle.forward(100)
turtle.speed('fastest')
drawTree(80, 1)

turtle.done()

효과도:

4, 코흐 눈꽃입니다.
(더 많은 자료를 얻으려면 아래로 내려주세요)

import turtle as t
from turtle import *
import random
 
def draw_path(path):
    t.colormode(255)
    t.color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
    for symbol in path:
        if symbol == 'F':
            forward(length)
        elif symbol == '-':
            right(angle)
        elif symbol == '+':
            left(angle)
 
def apply_rule(path):
    rule = 'F+F--F+F'
    return path.replace('F',rule)
 
length = .5
angle  = 60
setup(1280,720)
bgcolor('black')
up()
color("#0fe6ca")
goto(0,0)
down()
path = 'F--F--F'
speed(0)
up()
goto(-440,-250)
down()
for i in range(5):
    path = apply_rule(path)
draw_path(path)
draw_path(path)
draw_path(path)
a,b = pos()
for i in range(3):
    up()
    a += 250
    goto(a,b)
    down()
    draw_path(path)
    draw_path(path)
    draw_path(path)
b += 220
for i in range(2):
    up()
    a -= 250
    goto(a,b)
    down()
    draw_path(path)
    draw_path(path)
    draw_path(path)
b += 220
for i in range(2):
 
    draw_path(path)
    draw_path(path)
    draw_path(path)
    up()
    a += 130
    goto(a,b)
    down()

효과도:

5, 비주얼 쇼크 1입니다.

import turtle as t
from turtle import *


angle = 60 #각도를 바꾸어 다양한 다각형을 그립니다
t.setup(1280,720)
t.bgcolor('black')
t.pensize(2)
randomColor = ['red','blue','green','purple','gold','pink']
t.speed(0)
for i in range(600):
t.color(randomColor[i%6])
t.fd(i)
t.rt(angle+1)
up()
color("#0fe6ca")
goto(0,0)
down()
t.done()

효과도:

6, 비주얼 쇼크 2입니다

# # -*- coding: utf-8 -*-

import turtle as t
from turtle import *

angle = 60 #각도를 바꾸어 다양한 다각형을 그립니다
t.bgcolor('black')
t.pensize(2)
randomColor = ['red','blue','green','purple','gold','pink']
t.speed(0)
for i in range(200):
t.color(randomColor[i%6])
t.circle(i)
t.rt(angle+1)
up()
color("#0fe6ca")
goto(0,0)
down()

효과도:

7. 비주얼 3입니다

from turtle import *
import time
 
speed(0)
colormode(255)
clrs = ["MidnightBlue", "Navy", "DarkBlue", "MediumBlue", "RoyalBlue", "MediumSlateBlue", "CornflowerBlue",
        "DodgerBlue", "DeepskyBlue", "LightSkyBlue", "SkyBlue", "LightBlue"]
 
time.sleep(2)
 
for j in range(120):
 
    cn = 0
    c = 30
    f = 70
 
    for i in range(12):
        pencolor(clrs[cn])
        circle(c)
        left(90)
        penup()
        forward(f)
        right(90)
        pendown()
        c = c * 0.8
        f = f * 0.8
        circle(c)
        cn = cn + 1
 
    penup()
    goto(0, 0)
    forward(5)
    right(3)
    pendown()

효과도:

8. 힐베르트 곡선:

# -*- coding: utf-8 -*-
 
from turtle import *
import random
length = 10
angle  = 90
setup(1280,720)
up()
 
goto(-640,-360)
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'f':
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            fd(length)
        elif symbol == '+':
            lt(angle)
        elif symbol == '-':
            rt(angle)
 
def apply_path(rules,path):
    lit = [x for x in path]
    for i in range(len(lit)):
        symbol = lit[i]
        if symbol == 'x':
            lit[i] = rules[symbol]
        elif symbol == 'y':
            lit[i] = rules[symbol]
    path = ''.join(lit)
    return path
 
rules = {
    'x':'+yf-xfx-fy+',
    'y':'-xf+yfy+fx-'
}
path = 'x'
speed(0)
for i in range(7):
    path = apply_path(rules,path)
draw_path(path)
done()

효과도:

9. Sierpi ski 화살표 곡선입니다

# -*- coding: utf-8 -*-
 
 
from turtle import *
length = 5
angle = -60
setup(1280,720)
up()
 
goto(-640,-350)
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'A' or symbol == 'B':
            import random
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            forward(length)
        elif symbol == '-':
            right(angle)
        elif symbol == '+':
            left(angle)
    ht()
 
def apply_rules(path,rules):
    lit = [_ for _ in path]
    for i in range(len(lit)):
        symbol = lit[i]
        if symbol in rules:
            lit[i] = rules[symbol]
    path = ''.join(lit)
    return path
 
 
rules = {
    'A':'B-A-B',
    'B':'A+B+A'
}
path = 'A'
speed(0)
for i in range(7):
    path = apply_rules(path,rules)
draw_path(path)
up()
goto(0,-340)
angle = 60
down()
draw_path(path)
up()
goto(0,-340)
angle = -60
down()
draw_path(path)

효과도:

10.Koch 라인입니다

# -*- coding: utf-8 -*-
 
from turtle import *
import random
length = 2
angle  = 90
setup(1280,720)
up()
goto(-600,-350)
down()
def draw_path(path):
    for symbol in path:
        if symbol == 'F':
            colormode(255)
            color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
            forward(length)
        elif symbol == '-':
            right(angle)
        elif symbol == '+':
            left(angle)
    ht()
 
def apply_rule(path):
    rule = 'F+F-F-F+F'
    return path.replace('F',rule)
 
path = 'F'
speed(0)
for i in range(5):
    path = apply_rule(path)
for i in range(5):
    draw_path(path)
up()
goto(-478,-228)
down()
for i in range(4):
    draw_path(path)
up()
goto(-356,-106)
down()
for i in range(3):
    draw_path(path)
up()
goto(-235,16)
down()
for i in range(2):
    draw_path(path)
up()
goto(-115,137)
down()
draw_path(path)

효과도:

 

반응형