使用Python的turtle模块画国旗

yipeiwu_com5年前Python基础

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 time
import turtle
import 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 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设计】。

相关文章

python实现稀疏矩阵示例代码

python实现稀疏矩阵示例代码

工程实践中,多数情况下,大矩阵一般都为稀疏矩阵,所以如何处理稀疏矩阵在实际中就非常重要。本文以Python里中的实现为例,首先来探讨一下稀疏矩阵是如何存储表示的。 1.sparse模块初...

在Django中使用Sitemap的方法讲解

sitemap 是你服务器上的一个XML文件,它告诉搜索引擎你的页面的更新频率和某些页面相对于其它页面的重要性。 这个信息会帮助搜索引擎索引你的网站。 例如,这是 Django 网站(h...

浅谈pandas筛选出表中满足另一个表所有条件的数据方法

浅谈pandas筛选出表中满足另一个表所有条件的数据方法

今天记录一下pandas筛选出一个表中满足另一个表中所有条件的数据。例如: list1 结构:名字,ID,颜色,数量,类型。 list1 = [['a',1,255,100,'03'],...

python使用smtplib模块通过gmail实现邮件发送的方法

本文实例讲述了python使用smtplib模块通过gmail实现邮件发送的方法。分享给大家供大家参考。具体实现方法如下: import smtplib from email.MIM...

python遍历文件夹找出文件夹后缀为py的文件方法

大学毕业, 想看看大学写了多少行代码。 #coding=utf-8 import os class Solution: def __init__(self): self.dir...