在Udacity上課時(shí)學(xué)到了python的turtle方法,這是一個(gè)很經(jīng)典的用來教小孩兒編程的圖形模塊,最早起源于logo語言。python本身內(nèi)置了這個(gè)模塊,其可視化的方法可以幫助小孩兒對(duì)編程的一些基本理念有所理解。
在作業(yè)提交的論壇里看到很多turtle畫出來的精美圖形,想不出什么要畫的東西,于是決定拿五星紅旗來練練手。
前期準(zhǔn)備
五星紅旗繪制參數(shù)
Turtle官方文檔
turtle的基本操作
# 初始化屏幕 window = turtle.Screen() # 新建turtle對(duì)象實(shí)例 import turtle aTurtle = turtle.Turtle() # 海龜設(shè)置 aTurtle.hideturtle() # 隱藏箭頭 aTurtle.speed(10) # 設(shè)置速度 # 前進(jìn)后退,左轉(zhuǎn)右轉(zhuǎn) aTurtle.fd(100) # 前進(jìn)100像素 aTurtle.right(90) # 右轉(zhuǎn)90° aTurtle.back(100) aTurtle.left(90) # 填充顏色 aTurtle.begin_fill() aTurtle.fillcolor('yellow') DoSomethinghere() aTurtle.end_fill() # 抬起筆和放下筆,這樣進(jìn)行的操作不會(huì)留下痕跡 aTurtle.penup() aTurtle.goto(start_pos) aTurtle.fd(radius) aTurtle.pendown()
繪制五星紅旗代碼
github地址:https://gist.github.com/dc11287081ee67075da8
#!/usr/bin/env python # -*- coding: utf-8 ?C*- ''' 對(duì)于turtle類的一些封裝方法,包括畫正多邊形,正多角形和五星紅旗。''' __author__ = 'Hu Wenchao' import turtle import math def draw_polygon(aTurtle, size=50, n=3): ''' 繪制正多邊形 args: aTurtle: turtle對(duì)象實(shí)例 size: int類型,正多邊形的邊長 n: int類型,是幾邊形 ''' for i in xrange(n): aTurtle.forward(size) aTurtle.left(360.0/n) def draw_n_angle(aTurtle, size=50, num=5, color=None): ''' 繪制正n角形,默認(rèn)為黃色 args: aTurtle: turtle對(duì)象實(shí)例 size: int類型,正多角形的邊長 n: int類型,是幾角形 color: str, 圖形顏色,默認(rèn)不填色 ''' if color: aTurtle.begin_fill() aTurtle.fillcolor(color) for i in xrange(num): aTurtle.forward(size) aTurtle.left(360.0/num) aTurtle.forward(size) aTurtle.right(2*360.0/num) if color: aTurtle.end_fill() def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None): ''' 根據(jù)起始位置、結(jié)束位置和外接圓半徑畫五角星 args: aTurtle: turtle對(duì)象實(shí)例 start_pos: int的二元tuple,要畫的五角星的外接圓圓心 end_pos: int的二元tuple,圓心指向的位置坐標(biāo)點(diǎn) radius: 五角星外接圓半徑 color: str, 圖形顏色,默認(rèn)不填色 ''' aTurtle = aTurtle or turtle.Turtle() size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5) aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0]))) aTurtle.penup() aTurtle.goto(start_pos) aTurtle.fd(radius) aTurtle.pendown() aTurtle.right(math.degrees(math.pi*9/10)) draw_n_angle(aTurtle, size, 5, color) def draw_5_star_flag(times=20.0): ''' 繪制五星紅旗 args: times: 五星紅旗的規(guī)格為30*20, times為倍數(shù),默認(rèn)大小為10倍, 即300*200 ''' width, height = 30*times, 20*times # 初始化屏幕和海龜 window = turtle.Screen() aTurtle = turtle.Turtle() aTurtle.hideturtle() aTurtle.speed(10) # 畫紅旗 aTurtle.penup() aTurtle.goto(-width/2, height/2) aTurtle.pendown() aTurtle.begin_fill() aTurtle.fillcolor('red') aTurtle.fd(width) aTurtle.right(90) aTurtle.fd(height) aTurtle.right(90) aTurtle.fd(width) aTurtle.right(90) aTurtle.fd(height) aTurtle.right(90) aTurtle.end_fill() # 畫大星星 draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow') # 畫四個(gè)小星星 stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)] for pos in stars_start_pos: draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow') # 點(diǎn)擊關(guān)閉窗口 window.exitonclick() if __name__ == '__main__': draw_5_star_flag()
結(jié)果:
總結(jié)
以上就是本文關(guān)于Python使用Turtle模塊繪制五星紅旗代碼示例的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
