tft每日頭條

 > 生活

 > python turtle 繪制對聯

python turtle 繪制對聯

生活 更新时间:2024-08-18 07:59:01

今天主要講一下怎麼通過python和turtle繪制國旗。先看效果圖。

python turtle 繪制對聯(python和turtle繪制國旗)1

運行效果圖

第一步:通過cad或caxa繪制出國旗形狀(必須按照國家标準規定的尺寸進行)。

python turtle 繪制對聯(python和turtle繪制國旗)2

用cad或caxa繪制出國旗

第二步:通過cad或caxa将需要繪制的國旗尺寸進行标注,通過python繪制時需要用到。

python turtle 繪制對聯(python和turtle繪制國旗)3

标注尺寸

第三步:打開pycharm,創建python文件,結合第二步的尺寸定義畫布坐标(大小)并實現繪制國旗代碼(調用turtle庫)的編寫,實現繪制國旗的效果。

import turtle t=turtle.Pen() t.hideturtle() #隐藏畫筆的形狀 #畫國旗背景圖 t.color('red') t.fillcolor('red') t.penup() t.goto(-480,320) t.pendown() t.begin_fill() t.forward(960) t.right(90) t.forward(640) t.right(90) t.forward(960) t.right(90) t.forward(640) t.end_fill() #繪制中心五角星 t.penup() t.goto(-320,256) t.right(162) t.pendown() t.color('yellow') t.fillcolor('yellow') t.begin_fill() t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.end_fill() #繪制右上第一個小五角星 t.penup() t.goto(-147.5,285.4) t.right(167.04) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #繪制右上第二個小五角星 t.penup() t.goto(-110.1,220.7) t.right(94.83) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #繪制右上第三個小五角星 t.penup() t.goto(-96,128) t.right(170.13) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #繪制右上第四個小五角星 t.penup() t.goto(-148.7,61.9) t.right(164.66) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #添加文字 t.penup() t.color('red') t.goto(-100,-380) t.pendown() t.write("我愛你中國",font=("Times",30,"bold"))

第四步:通過觀察代碼,發現繪制五角星時,其中有幾步是重複的,可以通過for語句進行循環,減少代碼數量。

import turtle as t #隐藏畫筆的形狀 t.hideturtle() #設置畫布大小 t.screensize(1,1) #畫國旗背景圖 t.color('red') t.fillcolor('red') t.penup() t.goto(-480,320) t.pendown() t.begin_fill() t.forward(960) t.right(90) t.forward(640) t.right(90) t.forward(960) t.right(90) t.forward(640) t.end_fill() #繪制中心五角星 t.penup() t.goto(-320,256) t.right(162) t.pendown() t.color('yellow') t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循環4次 t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.end_fill() #繪制右上第一個小五角星 t.penup() t.goto(-147.5,285.4) t.right(167.04) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循環4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #繪制右上第二個小五角星 t.penup() t.goto(-110.1,220.7) t.right(94.83) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循環4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #繪制右上第三個小五角星 t.penup() t.goto(-96,128) t.right(170.13) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循環4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #繪制右上第四個小五角星 t.penup() t.goto(-148.7,61.9) t.right(164.66) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循環4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #添加文字 t.penup() t.color('red') t.goto(-100,-380) t.pendown() t.write("我愛你中國",font=("Times",30,"bold"))

第五步:再次觀察代碼,可以通過将for語句創建為函數,并通過調用函數的形式繪制五角星,以此達到再次優化代碼的效果。

import turtle as t #隐藏畫筆的形狀 t.hideturtle() #設置畫布大小 t.screensize(1,1) #設置背景循環函數 def bj(): t.pendown() t.begin_fill() for i in range(1,3): t.forward(960) t.right(90) t.forward(640) t.right(90) t.end_fill() #設置中心五角星循環函數 def wj1(): t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1, 5): # 循環4次 t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.end_fill() #設置小五角星循環函數 def wj2(): t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1, 5): # 循環4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #畫國旗背景圖 t.color('red') t.fillcolor('red') t.penup() t.goto(-480,320) bj() #繪制中心五角星 t.penup() t.goto(-320,256) t.right(72) t.color('yellow') wj1() #繪制右上第一個小五角星 t.penup() t.goto(-147.5,285.4) t.right(167.04) wj2() #繪制右上第二個小五角星 t.penup() t.goto(-110.1,220.7) t.right(94.83) wj2() #繪制右上第三個小五角星 t.penup() t.goto(-96,128) t.right(170.13) wj2() #繪制右上第四個小五角星 t.penup() t.goto(-148.7,61.9) t.right(164.66) wj2() #添加文字 t.penup() t.color('red') t.goto(-100,-380) t.pendown() t.write("我愛你中國",font=("Times",30,"bold"))

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved