使用Python的turtle模块画国旗
作者:嗨学编程 发布时间:2021-10-22 12:31:10
标签:python,turtle,国旗
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)
来源:https://blog.csdn.net/fei347795790/article/details/88723735


猜你喜欢
- 闲的无聊。。。网上一堆,正好练手(主要是新手)# coding=utf-8 import requests from bs4 import
- 早上看了一个贴子,是一个哥们推广自己一个智能的数据库备份系统,他总结了数据库备份过程中所有可能出错的情况,可以借鉴。如果你做DBA时间不长,
- 下面就来介绍一下这些在后台辛勤工作的进程们。系统检测器(System Monitor,SMON)、进程监视器(Process Monitor
- 模糊数据库指能够处理模糊数据的数据库。一般的数据库都是以二直逻辑和精确的数据工具为基础的,不能表示许多模糊不清的事情。随着模糊数学理论体系的
- 昨天同事给你我一个有问题的数据库,叫我修复一下因为客户那边需要这个数据库,这个数据库只有一个mdf文件和一个ldf文件,当我附加数据库的时候
- Css Reset是什么? 有些同行叫 "css复位",有些可能叫 "默认css".....相信看完
- 1. 生命游戏是什么生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。它包括一个二维矩形世界,这个世界中的每个方格居住着一个
- SQL Server 2000 清理日志精品教程SQL Server 2000 数据库日志太大!如何清理SQL Server 2000的日志
- 适配器设计模式是懒得改动某些代码,或者某些接口不方便改动的时候,使用一个特定的封装,一些特定的编写办法,使不同的接口可以使用同种调用方式使用
- hashlibhashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256,
- 准备我测试使用的Python版本为2.7.10,如果你的版本是Python3.5的话,这里就不太适合了。 使用Speech API原理我们的
- python对XML文件的操作1、xml 创建import xml.etree.ElementTree as ETnew_xml=ET.El
- 最近迁移了wordpress,系统升级为CentOS 6,很奇怪的一个问题,在原来CentOS 5.8下用的很正常的定时备份数据库并通过邮件
- MySQL和MariaDB的关系MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。MariaDB
- 在网上查找大量资料,经过自己的不懈努力,终于测试成功了。原来要在服务器上安装mysql odbc 3.51 ,还有数据库用户名及密码,用下面
- <%@LANGUAGE="xxx" CODEPAGE="936"%>一般又分为<%
- import urllib, httplib import utils import json &nbs
- 虽然ting88没有注册的用户不能下载歌曲,但搞定它也非难事啊:)进入www.ting88.com的网站,把歌手专辑页面的URL复制到文本框
- 我们经常见到很多网站留言系统的显示访客的IP地址都是隐藏了一部分,以达到隐蔽访客真实地理位置的功能。如:111.222.333.*,当然在系
- 在使用缓存时,容易发生缓存击穿。缓存击穿:一个存在的key,在缓存过期的瞬间,同时有大量的请求过来,造成所有请求都去读dB,这些请求都会击穿