使用Python的turtle畫小豬喬治和哆啦 A 夢
本人是Python新人小白,歡迎各位大佬指正,本文介紹的是用Python的turtle畫一隻小豬喬治。
還有本人畫畫基礎超級差= =沒天賦,畫參考度娘上的一個圖片畫的。
先上總程式碼,然後說說我寫的思路,
import turtle as t
'''
t.pu() 提起畫筆
t.pd() 移動時繪製圖形,預設時也為繪製
t.seth 設定當前朝向為angle角度
t.begin_fill() 準備開始填充圖形
t.color 同時設定pencolor=color1, fillcolor=color2
t.goto 設定筆的座標
t.circle(70,20) 半徑 度數
15,124,215 喬治褲子顏色外面
66,163,242 喬治褲子顏色裡面
134 196 247 天空的顏色
123,245,95 草地的顏色
253,6,6 鞋子外面
253,70,70 鞋子裡面
130,119,100 泥坑
'''
r_a=0.8
wight = 1100
height = 700
#t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255,155,192),"pink")
t.setup(wight,height)
t.speed(10)
def move_pen(x,y):
t.pu()
t.goto(x-wight/2+50,y-height/2+50)
t.pd()
def pen_set(size,r1,g1,b1,r2=0,g2=0,b2=0):
t.pensize(size)
t.color((r1,g1,b1),(r2,g2,b2))
def draw_grid():
pen_set(1,0,0,0,0,0,0)
for i in range(20):
move_pen(0+i*50,0)
t.seth(90)
t.fd(600)
for i in range(12):
move_pen(0,0+i*50)
t.seth(0)
t.fd(1000)
def draw_bg():
#畫草地
move_pen(0,350)
pen_set(4,123,245,95, 123,245,95)
t.begin_fill()
t.seth(-90)
t.fd(350)
t.seth(0)
t.fd(1000)
t.seth(90)
t.fd(350)
t.end_fill()
#畫天空
move_pen(0,350)
pen_set(4,134,196,247, 134,196,247)
t.begin_fill()
t.seth(90)
t.fd(250)
t.seth(0)
t.fd(1000)
t.seth(-90)
t.fd(250)
a=-180 + r_a
for i in range(50):
a = a - r_a/50
t.seth(a)
t.fd(500/50)
a =180
for i in range(50):
a = a - r_a/50
t.seth(a)
t.fd(500/50)
t.end_fill()
def draw_mud_pit():
#畫泥坑
pen_set(5,130,119,100, 130,119,100)
move_pen(350,150)
t.begin_fill()
t.seth(-180)
t.circle(50,125)
t.seth(-20)
t.circle(350,60)
t.seth(20)
t.circle(50,30)
t.seth(10)
t.circle(50,30)
t.seth(0)
t.circle(50,30)
t.seth(40)
t.circle(50,90)
t.seth(170)
t.circle(500,45)
t.end_fill()
def draw_shoes():
pen_set(3,253,6,6, 253,70,70)
move_pen(400,100)
t.begin_fill()
t.seth(0)
t.fd(50)
t.seth(87)
t.fd(50)
t.seth(180)
t.fd(25)
t.seth(-93)
t.fd(20)
t.seth(-180)
t.fd(25)
t.seth(-120)
t.circle(45,38)
t.end_fill()
move_pen(470,100)
t.begin_fill()
t.seth(0)
t.fd(50)
t.seth(87)
t.fd(50)
t.seth(180)
t.fd(25)
t.seth(-93)
t.fd(20)
t.seth(-180)
t.fd(25)
t.seth(-120)
t.circle(45,38)
t.end_fill()
def draw_leg():
pen_set(6,255,155,192, 255,155,192)
move_pen(440,140)
t.seth(90)
t.fd(20)
move_pen(510,140)
t.seth(90)
t.fd(20)
def draw_trousers():
move_pen(400,300)
pen_set(6,15,124,215, 66,163,242)
t.begin_fill()
d_a = 100
a=-130
for i in range(60):
a = a + 2
t.seth(a)
t.fd(3)
for i in range(14):
a = a + 0.02
t.seth(a)
t.fd(2)
a = 0-a
for i in range(14):
a = a + 0.02
t.seth(a)
t.fd(2)
for i in range(60):
a = a + 2.2
t.seth(a)
t.fd(3)
t.end_fill()
def draw_tile():
move_pen(550,177)
pen_set(6,255,155,192, 255,155,192)
a=-60
for i in range(25):
a = a + 4
t.seth(a)
t.fd(1)
t.circle(5)
a = -a
for i in range(30):
a = a + 4
t.seth(a)
t.fd(1)
def draw_hands():
move_pen(550,250)
pen_set(6,255,155,192, 255,155,192)
t.seth(20)
t.fd(70)
move_pen(600,270)
t.seth(60)
t.fd(20)
move_pen(600,270)
t.seth(-20)
t.fd(20)
move_pen(380,250)
t.seth(160)
t.fd(50)
move_pen(350,260)
t.seth(100)
t.fd(20)
move_pen(350,260)
t.seth(-140)
t.fd(20)
def draw_face():
move_pen(400,360)
pen_set(4,255,155,192, 255,196,218)
t.begin_fill()
a=-120
for i in range(20):
a = a + 2.5
t.seth(a)
t.fd(2.2)
for i in range(130):
a = a + 1.3
t.seth(a)
t.fd(1.8)
for i in range(35):
a = a + 1.4
t.seth(a)
t.fd(2)
for i in range(50):
a = a + 0.35
t.seth(a)
t.fd(2)
for i in range(50):
a = a + 0.2
t.seth(a)
t.fd(2)
n=0.4
for i in range(180):
if 0<=i<30 or 60<=i<90 or 120<=i<150 :
n=n+0.08
t.lt(3) #向左轉3度
t.fd(n) #向前走a的步長
else:
n=n-0.08
t.lt(3)
t.fd(n)
a=-50
for i in range(20):
a = a + 2.8
t.seth(a)
t.fd(5)
t.end_fill()
def draw_other():
move_pen(310,440)
pen_set(6,255,145,192, 255,145,192)
t.begin_fill()
t.circle(3)
t.end_fill()
move_pen(330,430)
t.begin_fill()
t.circle(3)
t.end_fill()
pen_set(6,255,145,192, 255,255,255)
move_pen(410,425)
t.begin_fill()
t.circle(10)
t.end_fill()
move_pen(460,395)
t.begin_fill()
t.circle(10)
t.end_fill()
pen_set(6,0,0,0,0,0,0)
move_pen(405,429)
t.begin_fill()
t.circle(3)
t.end_fill()
move_pen(455,399)
t.begin_fill()
t.circle(3)
t.end_fill()
move_pen(510,310)
pen_set(6,255,155,192, 255,155,192)
t.begin_fill()
t.circle(25)
t.end_fill()
move_pen(410,340)
pen_set(6,255,145,192, 255,145,192)
a=-80
for i in range(20):
a = a + 6
t.seth(a)
t.fd(3)
move_pen(430,445)
pen_set(4,255,155,192, 255,196,218)
t.begin_fill()
a=120
for i in range(40):
a = a - 2
t.seth(a)
t.fd(1.2)
a=-a
for i in range(45):
a = a - 2
t.seth(a)
t.fd(1.2)
t.end_fill()
move_pen(480,430)
t.begin_fill()
a=70
for i in range(40):
a = a -1.5
t.seth(a)
t.fd(1.5)
a=-80
for i in range(45):
a = a -1.5
t.seth(a)
t.fd(1.5)
t.end_fill()
draw_bg()
draw_mud_pit()
#draw_grid()
draw_leg()
draw_shoes()
draw_trousers()
draw_tile()
draw_hands()
draw_face()
draw_other()
t.mainloop()
效果圖如下:
說說我的程式碼思路。
再嘗試畫圖之前,發現總是需要移動畫筆,抬起畫筆放下畫筆啥的,感覺操作重複太多了,程式碼不好看,於是自己寫了一個函式:
def move_pen(x,y):
t.pu()
t.goto(x-wight/2+50,y-height/2+50)
t.pd()
這個函式的意思就是先抬起畫筆,再移動畫筆,最後放下畫筆準備接下來的繪製。函式裡面第二行畫筆移動的位置是我人為的規定了左下角是畫筆的座標原點,座標後面加的東西類似座標系的平移,方便後面座標的計算。
還有一個就是設定畫筆屬性的函式,每次畫線之前都要用這兩個操作,所以我封裝了下面這個函式
def pen_set(size,r1,g1,b1,r2=0,g2=0,b2=0):
t.pensize(size)
t.color((r1,g1,b1),(r2,g2,b2))
我先畫了天空和草地,然後發現我畫畫的功底極差,沒什麼直覺,畫一個弧線啊都要式很久,所以我自己畫了一個座標網表,這個感覺超級有用,對於確定一些細節的東西(比如眼睛嘴巴手臂尾巴什麼的)的位置的調節幫助很大~建議大家畫別的東西的時候也可以用一用這個方法。
def draw_grid():
pen_set(1,0,0,0,0,0,0)
for i in range(21):
move_pen(0+i*50,0)
t.seth(90)
t.fd(600)
for i in range(13):
move_pen(0,0+i*50)
t.seth(0)
t.fd(1000)
就是這樣,剩下的每一個函式都是畫一個身體部分的,都是手動一點一點調的引數,因為自己的畫畫水平不行,不知道什麼時候畫什麼樣的弧線,都是按照感覺來的,調引數的時間花了不少。
嗯,就是這麼多了!
第一次寫的Python程式碼,還有一些c的感覺在裡面,沒有完全轉換好,有不合理的地方歡迎指出,不勝感激。
接下來就是哆啦 A 夢了~
事實上還有不少人通過 turtle 這個庫實現了有趣的圖,推薦去學習一下使用 turtle 這個庫,其實也不難,就是需要一點耐心~
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: dong
# @Date: 2018-07-05 19:37:42
# @Env: python 3.6
# @Github: https://github.com/PerpetualSmile
from turtle import *
# 無軌跡跳躍
def my_goto(x, y):
penup()
goto(x, y)
pendown()
# 眼睛
def eyes():
fillcolor("#ffffff")
begin_fill()
tracer(False)
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
lt(3)
fd(a)
else:
a += 0.05
lt(3)
fd(a)
tracer(True)
end_fill()
# 鬍鬚
def beard():
my_goto(-32, 135)
seth(165)
fd(60)
my_goto(-32, 125)
seth(180)
fd(60)
my_goto(-32, 115)
seth(193)
fd(60)
my_goto(37, 135)
seth(15)
fd(60)
my_goto(37, 125)
seth(0)
fd(60)
my_goto(37, 115)
seth(-13)
fd(60)
# 嘴巴
def mouth():
my_goto(5, 148)
seth(270)
fd(100)
seth(0)
circle(120, 50)
seth(230)
circle(-120, 100)
# 圍巾
def scarf():
fillcolor('#e70010')
begin_fill()
seth(0)
fd(200)
circle(-5, 90)
fd(10)
circle(-5, 90)
fd(207)
circle(-5, 90)
fd(10)
circle(-5, 90)
end_fill()
# 鼻子
def nose():
my_goto(-10, 158)
seth(315)
fillcolor('#e70010')
begin_fill()
circle(20)
end_fill()
# 黑眼睛
def black_eyes():
seth(0)
my_goto(-20, 195)
fillcolor('#000000')
begin_fill()
circle(13)
end_fill()
pensize(6)
my_goto(20, 205)
seth(75)
circle(-10, 150)
pensize(3)
my_goto(-17, 200)
seth(0)
fillcolor('#ffffff')
begin_fill()
circle(5)
end_fill()
my_goto(0, 0)
# 臉
def face():
fd(183)
lt(45)
fillcolor('#ffffff')
begin_fill()
circle(120, 100)
seth(180)
# print(pos())
fd(121)
pendown()
seth(215)
circle(120, 100)
end_fill()
my_goto(63.56,218.24)
seth(90)
eyes()
seth(180)
penup()
fd(60)
pendown()
seth(90)
eyes()
penup()
seth(180)
fd(64)
# 頭型
def head():
penup()
circle(150, 40)
pendown()
fillcolor('#00a0de')
begin_fill()
circle(150, 280)
end_fill()
# 畫哆啦A夢
def Doraemon():
# 頭部
head()
# 圍脖
scarf()
# 臉
face()
# 紅鼻子
nose()
# 嘴巴
mouth()
# 鬍鬚
beard()
# 身體
my_goto(0, 0)
seth(0)
penup()
circle(150, 50)
pendown()
seth(30)
fd(40)
seth(70)
circle(-30, 270)
fillcolor('#00a0de')
begin_fill()
seth(230)
fd(80)
seth(90)
circle(1000, 1)
seth(-89)
circle(-1000, 10)
# print(pos())
seth(180)
fd(70)
seth(90)
circle(30, 180)
seth(180)
fd(70)
# print(pos())
seth(100)
circle(-1000, 9)
seth(-86)
circle(1000, 2)
seth(230)
fd(40)
# print(pos())
circle(-30, 230)
seth(45)
fd(81)
seth(0)
fd(203)
circle(5, 90)
fd(10)
circle(5, 90)
fd(7)
seth(40)
circle(150, 10)
seth(30)
fd(40)
end_fill()
# 左手
seth(70)
fillcolor('#ffffff')
begin_fill()
circle(-30)
end_fill()
# 腳
my_goto(103.74, -182.59)
seth(0)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(-15, 180)
fd(90)
circle(-15, 180)
fd(10)
end_fill()
my_goto(-96.26, -182.59)
seth(180)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(15, 180)
fd(90)
circle(15, 180)
fd(10)
end_fill()
# 右手
my_goto(-133.97, -91.81)
seth(50)
fillcolor('#ffffff')
begin_fill()
circle(30)
end_fill()
# 口袋
my_goto(-103.42, 15.09)
seth(0)
fd(38)
seth(230)
begin_fill()
circle(90, 260)
end_fill()
my_goto(5, -40)
seth(0)
fd(70)
seth(-90)
circle(-70, 180)
seth(0)
fd(70)
#鈴鐺
my_goto(-103.42, 15.09)
fd(90)
seth(70)
fillcolor('#ffd200')
# print(pos())
begin_fill()
circle(-20)
end_fill()
seth(170)
fillcolor('#ffd200')
begin_fill()
circle(-2, 180)
seth(10)
circle(-100, 22)
circle(-2, 180)
seth(180-10)
circle(100, 22)
end_fill()
goto(-13.42, 15.09)
seth(250)
circle(20, 110)
seth(90)
fd(15)
dot(10)
my_goto(0, -150)
# 畫眼睛
black_eyes()
if __name__ == '__main__':
screensize(800,600, "#f0f0f0")
pensize(3) # 畫筆寬度
speed(9) # 畫筆速度
Doraemon()
my_goto(100, -300)
write('linuxidc.com', font=("Bradley Hand ITC", 30, "bold"))
mainloop()
效果圖如下:
更多Python相關資訊見 Python 專題頁面 https://www.linuxidc.com/topicnews.aspx?tid=17
Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址: https://www.linuxidc.com/Linux/2019-04/157959.htm