使用Python的turtle模块画国旗
Python  /  管理员 发布于 7年前   217
Python的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 timeimport turtleimport os'''想要学习Python?Python学习交流群:984632579满足你的需求,资料都已经上传群文件,可以自行下载!'''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 projectturtle.speed(5)turtle.penup()#draw the rectanglestar_x=-320star_y=-260len_x=660len_y=440draw_rectangle(star_x,star_y,len_x,len_y)#draw the big starpice=660/30big_center_x=star_x+5*picebig_center_y=star_y+len_y-pice*5turtle.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 starturtle.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 starturtle.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 thirdturtle.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 finalturtle.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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号