歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用Python的turtle模塊畫國旗

用Python的turtle模塊畫國旗

日期:2017/3/1 9:11:08   编辑:Linux編程

最近在學Python,發現Python的海龜繪圖非常有趣,就分享一下!

話不多說,先來Python turtle的官方文檔鏈接: Python turtle。這裡面有turtle的各類指令。turtle畫國旗主要用到兩個函數:draw_rentangle和draw_star。至於函數的調用就和我們學的C,C++是一樣的。對於turtle畫國旗的程序中,首先是查找國旗的畫法,才能用程序實現。自己在實現的過程中主要是對turtle.circle()沒有准確掌握,所以花了一些不必要的時間。turtle.circle畫弧時,海龜(turtle)的方向就是弧的切線方向,也就是說turtle的垂直方向就是圓心在的直線上,給定參數radius就可以畫了,程序中第二注意的地方就是小五角星和大五角星的位置關系,主要是程序中的turtle.left(turtle.towards(center_x,center_y)-turtle.heading()),當然,我看有的人用了round()函數來獲取近似值,但是,默認的已經足夠了。下面是本人寫的程序和結果演示。

程序:

import time
import turtle
import os
def draw_rectangle(start_x,start_y,rec_x,rec_y):
turtle.goto(start_x,start_y)
turtle.color('red')
turtle.fillcolor('red')
turtle.begin_fill()
for i in range(2):
turtle.forward(rec_x)
turtle.left(90)
turtle.forward(rec_y)
turtle.left(90)
turtle.end_fill()


def draw_star(center_x,center_y,radius):
turtle.setpos(center_x,center_y)
#find the peak of the five-pointed star
pt1=turtle.pos()
turtle.circle(-radius,72)
pt2=turtle.pos()
turtle.circle(-radius,72)
pt3=turtle.pos()
turtle.circle(-radius,72)
pt4=turtle.pos()
turtle.circle(-radius,72)
pt5=turtle.pos()
#draw the five-pointed star
turtle.color('yellow','yellow')
turtle.fill(True)
turtle.goto(pt3)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.fill(False)


#start the project
turtle.speed(5)
turtle.penup()
#draw the rectangle
star_x=-320
star_y=-260
len_x=660
len_y=440
draw_rectangle(star_x,star_y,len_x,len_y)
#draw the big star
pice=660/30
big_center_x=star_x+5*pice
big_center_y=star_y+len_y-pice*5
turtle.goto(big_center_x,big_center_y)
turtle.left(90)
turtle.forward(pice*3)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice*3)
#draw the small star
turtle.goto(star_x+10*pice,star_y+len_y-pice*2)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the second star
turtle.goto(star_x+pice*12,star_y+len_y-pice*4)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the third
turtle.goto(star_x+pice*12,star_y+len_y-7*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the final
turtle.goto(star_x+pice*10,star_y+len_y-9*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)


turtle.ht()
time.sleep(3)
os._exit(1)

結果:

用Python的turtle模塊畫國旗

感覺畫的還不賴

Ubuntu 14.04安裝Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Copyright © Linux教程網 All Rights Reserved