前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用Python的turtle模块画国旗

用Python的turtle模块画国旗

作者头像
DevinGeng
发布2019-04-09 17:27:41
3K0
发布2019-04-09 17:27:41
举报
文章被收录于专栏:云+技术云+技术

最近在学Python,发现Python的海龟绘图非常有趣,就分享一下!话不多说,先来Python turtle的官方文档链接: Python turtle。这里面有turtle的各类指令。turtle画国旗主要用到两个函数:draw_rentangle和draw_star。

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模块画国旗用Python的turtle模块画国旗

xxx
xxx

感觉画的还不赖:)

本文转载自:http://blog.sina.com.cn/s/blog_ea828d2a0102wb4p.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-12-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档