Python使用Turtle模塊繪制國旗的方法示例
turtle模塊
turtle模塊:python內(nèi)置的繪圖工具
turtle(海龜)模塊,我們是用它來進(jìn)行畫圖的,基本上就是畫簡單的直線,點(diǎn),和曲線。
你可以把它想成一個小海龜,在沙灘上行走,然后留下的各種痕跡,使用Turtle模塊可以繪制很多精美的圖形。
基本操作(Turtle方法)
- turtle.forward(step):前進(jìn)step個像素
- turtle.back(step):后退step個像素
- turtle.right():右轉(zhuǎn)一個角度
- turtle.left():左轉(zhuǎn)一個角度
- turtle.pencolor(“string”):畫筆顏色
- turtle.fillcolor(“string”):填充顏色
- turtle.speed(int):運(yùn)動速度
其他的turtle方法可以參見python官網(wǎng)
https://docs.python.org/zh-cn/3.7/library/turtle.html
具體代碼實(shí)現(xiàn)
# 繪畫 # 中國國旗 # 轉(zhuǎn)載請標(biāo)明出處?。? import turtle import time def draw__stars(tur, step, x, y, arg): """ 繪制五角星 :param tur: turtle object :param step: 五角星一條邊的長度 :param x: 開始繪制五角星的起點(diǎn)x坐標(biāo) :param y: 開始繪制五角星的起點(diǎn)y坐標(biāo) :param arg: :return: """ tur.pencolor('yellow') tur.fillcolor('yellow') tur.up() tur.goto(x, y) tur.begin_fill() tur.down() tur.right(arg) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.end_fill() def draw__flag(tur, wide, height): """ 繪制國旗的長方形形狀 :param tur: turtle object :param wide: the width of the flag :param height: the height of the flag :return: None """ tur.pencolor('red') tur.fillcolor('red') tur.goto(- wide / 2, height / 2) tur.begin_fill() tur.forward(wide) tur.right(90) tur.forward(height) tur.right(90) tur.forward(wide) tur.right(90) tur.forward(height) tur.end_fill() if __name__ == '__main__': """ main 函數(shù) """ # tur = turtle.Turtle() turtle.screensize(canvwidth=3000, canvheight=2000, bg=None) # 繪制star的turtle對象 tur_star = turtle.Turtle() # 繪制flag的turtle對象 tur_flag = turtle.Turtle() tur_flag.speed(3) tur_star.speed(3) # 隱藏turtle對象 tur_star.hideturtle() tur_flag.hideturtle() # 間隔3秒,可以沒有,這個是我調(diào)試時加上去的 time.sleep(3) # 繪制長方形 draw__flag(tur_flag, 630, 420) # 繪制五角星,在合適的位置進(jìn)行繪制五角星 # 調(diào)用五次函數(shù)繪制五顆五角星 draw__stars(tur_star, step=60, x=-280, y=155, arg=0) draw__stars(tur_star, step=25, x=-182, y=177, arg=- 25) draw__stars(tur_star, step=25, x=-175, y=125, arg=41) draw__stars(tur_star, step=25, x=-208, y=79, arg=23) draw__stars(tur_star, step=25, x=-265, y=75, arg=48) # 使畫面鎖定 turtle.done()
運(yùn)行結(jié)果
總結(jié)
到此這篇關(guān)于Python使用Turtle模塊繪制國旗的文章就介紹到這了,更多相關(guān)Python Turtle模塊繪制國旗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python保留小數(shù)函數(shù)的幾種使用總結(jié)
本文主要介紹了python保留小數(shù)函數(shù)的幾種使用總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python實(shí)現(xiàn)冒泡,插入,選擇排序簡單實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)冒泡,插入,選擇排序簡單實(shí)例,很適合Python初學(xué)者學(xué)習(xí)參考之用,需要的朋友可以參考下2014-08-08如何解決cmd運(yùn)行python提示不是內(nèi)部命令
在本篇文章里小編給大家整理了關(guān)于如何解決cmd運(yùn)行python提示不是內(nèi)部命令的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-07-07Python Numpy學(xué)習(xí)之索引及切片的使用方法
數(shù)組中的元素可以通過索引以及切片的手段進(jìn)行訪問或者修改,和列表的切片操作一樣。本文將詳細(xì)為大家介紹一下Python中的科學(xué)計(jì)算庫-Numpy的索引及切片的使用方法2022-01-01在主機(jī)商的共享服務(wù)器上部署Django站點(diǎn)的方法
這篇文章主要介紹了在主機(jī)商的共享服務(wù)器上部署Django站點(diǎn)的方法,Django是最具人氣的Python框架,需要的朋友可以參考下2015-07-07使用Python實(shí)現(xiàn)簡單的服務(wù)器功能
socket的使用并不局限于Python語言,你可以用C或者Java來寫出同樣的socket服務(wù)器,而所有語言使用socket的方式都類似(Apache就是使用C實(shí)現(xiàn)的服務(wù)器)2017-08-08python實(shí)現(xiàn)梯度下降算法的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python實(shí)現(xiàn)梯度下降算法的實(shí)例詳解內(nèi)容,需要的朋友們可以參考下。2020-08-08