首頁 > 軟體

Python畫圖練習案例分享

2022-07-06 14:02:21

1.多邊形的繪製案例

# 多邊形的繪製案例
import turtle
def main():
turtle.color("green")
# steps代表多邊形的繪製
turtle.circle(50,steps=6)
turtle.exitonclick()
if __name__ == "__main__":
main()

2.太陽花案例

# 太陽花案例*******************************************************************
import turtle
import time
turtle.color("red","yellow")
turtle.begin_fill()
for _ in range(50):
turtle.speed(0)
turtle.forward(200)
turtle.left(170)
turtle.end_fill()
turtle.mainloop()

3.顏色五角星案例

# 顏色五角星案例******************************************************************
import turtle
import time
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150,-120)
turtle.color("violet")
turtle.write("Done",font=("Arial"))
turtle.mainloop()

4.藝術圖片

# 藝術圖片*************************************************************************
import turtle
turtle.speed(0)
turtle.delay(0)
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
turtle.color(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
turtle.done()

5.黑六邊形

# #黑六邊形*****************************************************************************
import turtle
def bye(x,y):
turtle.bye()
s = turtle.Screen()
s.bgcolor("black")
s.screensize(800,800)
s.title("Class Using")
s.onscreenclick(bye)
p=turtle.Turtle()
p.speed(0)
p.hideturtle()
p.pencolor("red")
p.pensize(3)
p.circle(50,360,6)
turtle.done()

前方高能

6.繪製時鐘

#繪製時鐘************************************************************************************************
import turtle as tt
from datetime import *
# 當前日期屬於一週的第幾天
def Week(t):
week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
return week[t.weekday()]
# 獲取當前時間
def Date(t):
y = t.year
m = t.month
d = t.day
cur_hour = t.hour;
cur_min = t.minute;
cur_sec = t.second;
return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec)
# 移動畫筆,距離為distance
def movePen(distance):
tt.penup()
tt.pensize(5)
tt.pencolor("blue")
tt.fd(distance)
tt.pendown()
# 繪製錶針
def makeHands(name, length):
# 清空視窗,重置turtule狀態為初始狀態
tt.reset()
movePen(-length * 0.1)
# 開始記錄多邊形的頂點
tt.begin_poly()
tt.fd(length * 1.1)
# 停止記錄多邊形的頂點
tt.end_poly()
# 返回記錄的多邊形
handForm = tt.get_poly()
tt.register_shape(name, handForm)
# 初始化
def initial():
global secHand, minHand, hurHand, printer
# 重置方向向北(上),正角度為順時針
tt.mode("logo")
# 建立並初始化錶針
makeHands("secHand", 180)
makeHands("minHand", 150)
makeHands("hurHand", 110)
secHand = tt.Turtle()
secHand.shape("secHand")
minHand = tt.Turtle()
minHand.shape("minHand")
hurHand = tt.Turtle()
hurHand.shape("hurHand")
for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 4)
hand.speed(0)
# 輸出文字
printer = tt.Turtle()
# 隱藏畫筆
printer.hideturtle()
printer.penup()
# 繪製錶盤外框
def drawClock(R):
# 清空視窗,重置turtule狀態為初始狀態
tt.reset()
# 畫筆尺寸
tt.pensize(5)
for i in range(60):
movePen(R)
if i % 5 == 0:
tt.fd(20)
movePen(-R - 20)
movePen(R + 20)
if i == 0:
# 寫文字
tt.write(int(12), align="center", font=("Consolas", 14, "bold"))
elif i == 30:
movePen(25)
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-25)
elif (i == 25 or i == 35):
movePen(20)
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-20)
else:
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-R - 20)
else:
# 繪製指定半徑和顏色的點
tt.dot(5, "red")
movePen(-R)
tt.right(6)
# 錶針的動態顯示
def handsMove():
t = datetime.today()
second = t.second + t.microsecond * 0.000001
minute = t.minute + second / 60.0
hour = t.hour + minute / 60.0
secHand.seth(6 * second)
minHand.seth(6 * minute)
hurHand.seth(30 * hour)
tt.tracer(False)
printer.fd(65)
tt.pencolor("green")
printer.write(Week(t), align="center", font = ("黑體", 14))
printer.back(130)
printer.write(Date(t), align="center", font = ("Consolas", 14))
# 設定當前畫筆位置為原點,方向朝東
printer.home()
tt.tracer(True)
# 經過100ms後繼續呼叫handsMove函數
tt.ontimer(handsMove, 100)
# 呼叫定義的函數,開啟和關閉動畫,為更新圖紙設定延遲;
tt.tracer(False)
initial()
drawClock(200)
tt.tracer(True)
handsMove()
tt.mainloop()

7.繪製分形樹

# 繪製分形樹******************************************************************************
import turtle
def draw_branch(branch_length):
'''
繪製分形樹
'''
if branch_length > 5:
# 繪製右側樹枝
turtle.forward(branch_length)
print("向前:", branch_length)
turtle.right(20)
print("右轉:20度")
draw_branch(branch_length - 15)

# 繪製左側樹枝
turtle.left(40)
print("左轉:40度")
draw_branch(branch_length - 15)
# 返回之前的樹枝
turtle.right(20)
print("右轉:20度")
turtle.backward(branch_length)
print("向後:", branch_length)
def main():
'''
主函數
'''
turtle.speed(0.5)
turtle.pensize(3)
turtle.left(90)
turtle.color('green')
turtle.penup()
turtle.backward(150)
turtle.pendown()
turtle.bgcolor("black")
draw_branch(100)
turtle.exitonclick()
if __name__ == "__main__":
main()

8.彩虹線繪製案例

# 彩虹線繪製案例***************************************************************************
import turtle as t
from random import randint as rint
t.shape("turtle")
t.pensize(5)
t.colormode(255)
t.bgcolor("black")
t.tracer(False)
for x in range(700):
t.color(rint(0,255),rint(0,255),rint(0,255))
t.circle(2*(1+x/4),5)
t.speed(0)
t.tracer(True)
t.exitonclick()

到此這篇關於Python畫圖練習案例分享的文章就介紹到這了,更多相關Python畫圖內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com