Python畫圖練習(xí)案例分享
1.多邊形的繪制案例
# 多邊形的繪制案例
import turtle
def main():
turtle.color("green")
# steps代表多邊形的繪制
turtle.circle(50,steps=6)
turtle.exitonclick()
if __name__ == "__main__":
main()
2.太陽(yáng)花案例
# 太陽(yáng)花案例*******************************************************************
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.藝術(shù)圖片
# 藝術(shù)圖片*************************************************************************
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.繪制時(shí)鐘
#繪制時(shí)鐘************************************************************************************************
import turtle as tt
from datetime import *
# 當(dāng)前日期屬于一周的第幾天
def Week(t):
week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
return week[t.weekday()]
# 獲取當(dāng)前時(shí)間
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)
# 移動(dòng)畫筆,距離為distance
def movePen(distance):
tt.penup()
tt.pensize(5)
tt.pencolor("blue")
tt.fd(distance)
tt.pendown()
# 繪制表針
def makeHands(name, length):
# 清空窗口,重置turtule狀態(tài)為初始狀態(tài)
tt.reset()
movePen(-length * 0.1)
# 開(kāi)始記錄多邊形的頂點(diǎn)
tt.begin_poly()
tt.fd(length * 1.1)
# 停止記錄多邊形的頂點(diǎn)
tt.end_poly()
# 返回記錄的多邊形
handForm = tt.get_poly()
tt.register_shape(name, handForm)
# 初始化
def initial():
global secHand, minHand, hurHand, printer
# 重置方向向北(上),正角度為順時(shí)針
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狀態(tài)為初始狀態(tài)
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:
# 繪制指定半徑和顏色的點(diǎn)
tt.dot(5, "red")
movePen(-R)
tt.right(6)
# 表針的動(dòng)態(tài)顯示
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))
# 設(shè)置當(dāng)前畫筆位置為原點(diǎn),方向朝東
printer.home()
tt.tracer(True)
# 經(jīng)過(guò)100ms后繼續(xù)調(diào)用handsMove函數(shù)
tt.ontimer(handsMove, 100)
# 調(diào)用定義的函數(shù),打開(kāi)和關(guān)閉動(dòng)畫,為更新圖紙?jiān)O(shè)置延遲;
tt.tracer(False)
initial()
drawClock(200)
tt.tracer(True)
handsMove()
tt.mainloop()
7.繪制分形樹(shù)
# 繪制分形樹(shù)******************************************************************************
import turtle
def draw_branch(branch_length):
'''
繪制分形樹(shù)
'''
if branch_length > 5:
# 繪制右側(cè)樹(shù)枝
turtle.forward(branch_length)
print("向前:", branch_length)
turtle.right(20)
print("右轉(zhuǎn):20度")
draw_branch(branch_length - 15)
# 繪制左側(cè)樹(shù)枝
turtle.left(40)
print("左轉(zhuǎn):40度")
draw_branch(branch_length - 15)
# 返回之前的樹(shù)枝
turtle.right(20)
print("右轉(zhuǎn):20度")
turtle.backward(branch_length)
print("向后:", branch_length)
def main():
'''
主函數(shù)
'''
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()
到此這篇關(guān)于Python畫圖練習(xí)案例分享的文章就介紹到這了,更多相關(guān)Python畫圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CentOS 6.5中安裝Python 3.6.2的方法步驟
centos 6.5默認(rèn)自帶的python版本為2.6,而下面這篇文章主要給大家介紹了關(guān)于在CentOS 6.5中安裝Python 3.6.2的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Python機(jī)器學(xué)習(xí)NLP自然語(yǔ)言處理基本操作之Seq2seq的用法
Seq2Seq模型是輸出的長(zhǎng)度不確定時(shí)采用的模型,這種情況一般是在機(jī)器翻譯的任務(wù)中出現(xiàn),將一句中文翻譯成英文,那么這句英文的長(zhǎng)度有可能會(huì)比中文短,也有可能會(huì)比中文長(zhǎng),所以輸出的長(zhǎng)度就不確定了2021-10-10
Python上級(jí)目錄文件導(dǎo)入的幾種方法(from.import)
有時(shí)候我們可能需要import另一個(gè)路徑下的python文件,下面這篇文章主要給大家介紹了關(guān)于Python上級(jí)目錄文件導(dǎo)入的幾種方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Python?Pygame實(shí)戰(zhàn)之打磚塊游戲的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)經(jīng)典的游戲—打磚塊。玩家操作一根螢?zāi)簧纤降摹鞍糇印?,讓一顆不斷彈來(lái)彈去的“球”在撞擊作為過(guò)關(guān)目標(biāo)消去的“磚塊”的途中不會(huì)落到螢?zāi)坏紫隆8信d趣的小伙伴可以了解一下2022-03-03
python實(shí)現(xiàn)Dijkstra算法的最短路徑問(wèn)題
這篇文章主要介紹了python實(shí)現(xiàn)Dijkstra算法的最短路徑問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程
Python中我們一般使用SMTP模塊來(lái)首發(fā)郵件,而用email模塊來(lái)處理郵件編碼,本文我們就來(lái)詳細(xì)看一下Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程,需要的朋友可以參考下2016-07-07
python調(diào)用百度語(yǔ)音識(shí)別api
這篇文章主要介紹了python調(diào)用百度語(yǔ)音識(shí)別api,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Kmeans聚類算法python sklearn用戶畫像教程
這篇文章主要介紹了Kmeans聚類算法python sklearn用戶畫像教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

