Python使用Turtle模块绘制五星红旗代码示例

yipeiwu_com6年前Python基础

在Udacity上课时学到了python的turtle方法,这是一个很经典的用来教小孩儿编程的图形模块,最早起源于logo语言。python本身内置了这个模块,其可视化的方法可以帮助小孩儿对编程的一些基本理念有所理解。

在作业提交的论坛里看到很多turtle画出来的精美图形,想不出什么要画的东西,于是决定拿五星红旗来练练手。

前期准备

五星红旗绘制参数

Turtle官方文档

turtle的基本操作

# 初始化屏幕
window = turtle.Screen()
# 新建turtle对象实例
import turtle
aTurtle = turtle.Turtle()
# 海龟设置
aTurtle.hideturtle() # 隐藏箭头
aTurtle.speed(10)  # 设置速度
# 前进后退,左转右转
aTurtle.fd(100)  # 前进100像素
aTurtle.right(90) # 右转90°
aTurtle.back(100)
aTurtle.left(90)
# 填充颜色
aTurtle.begin_fill()
aTurtle.fillcolor('yellow')
DoSomethinghere()
aTurtle.end_fill()
# 抬起笔和放下笔,这样进行的操作不会留下痕迹
aTurtle.penup()
aTurtle.goto(start_pos)
aTurtle.fd(radius)
aTurtle.pendown()

绘制五星红旗代码

github地址:https://gist.github.com/dc11287081ee67075da8

#!/usr/bin/env python   
# -*- coding: utf-8 –*-
''' 对于turtle类的一些封装方法,包括画正多边形,正多角形和五星红旗。'''
__author__ = 'Hu Wenchao'
 
import turtle
import math
 
def draw_polygon(aTurtle, size=50, n=3):
  ''' 绘制正多边形
  args:
    aTurtle: turtle对象实例
    size: int类型,正多边形的边长
    n: int类型,是几边形    
  '''
  for i in xrange(n):
    aTurtle.forward(size)
    aTurtle.left(360.0/n)
 
def draw_n_angle(aTurtle, size=50, num=5, color=None):
  ''' 绘制正n角形,默认为黄色
  args:
    aTurtle: turtle对象实例
    size: int类型,正多角形的边长
    n: int类型,是几角形  
    color: str, 图形颜色,默认不填色
  '''
  if color:
    aTurtle.begin_fill()
    aTurtle.fillcolor(color)
  for i in xrange(num):
    aTurtle.forward(size)
    aTurtle.left(360.0/num)
    aTurtle.forward(size)
    aTurtle.right(2*360.0/num)
  if color:
    aTurtle.end_fill()
 
def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):
  ''' 根据起始位置、结束位置和外接圆半径画五角星
  args:
    aTurtle: turtle对象实例
    start_pos: int的二元tuple,要画的五角星的外接圆圆心
    end_pos: int的二元tuple,圆心指向的位置坐标点
    radius: 五角星外接圆半径
    color: str, 图形颜色,默认不填色  
  '''
  aTurtle = aTurtle or turtle.Turtle()
  size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)
  aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
  aTurtle.penup()
  aTurtle.goto(start_pos)
  aTurtle.fd(radius)
  aTurtle.pendown()
  aTurtle.right(math.degrees(math.pi*9/10))
  draw_n_angle(aTurtle, size, 5, color)
 
def draw_5_star_flag(times=20.0):
  ''' 绘制五星红旗
  args:
    times: 五星红旗的规格为30*20, times为倍数,默认大小为10倍, 即300*200
  '''
  width, height = 30*times, 20*times
  # 初始化屏幕和海龟
  window = turtle.Screen()
  aTurtle = turtle.Turtle()
  aTurtle.hideturtle()
  aTurtle.speed(10)
  # 画红旗
  aTurtle.penup()
  aTurtle.goto(-width/2, height/2)
  aTurtle.pendown()
  aTurtle.begin_fill()
  aTurtle.fillcolor('red')
  aTurtle.fd(width)
  aTurtle.right(90)
  aTurtle.fd(height)
  aTurtle.right(90)
  aTurtle.fd(width)
  aTurtle.right(90)
  aTurtle.fd(height)
  aTurtle.right(90)  
  aTurtle.end_fill()
  # 画大星星
  draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow') 
  # 画四个小星星
  stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]
  for pos in stars_start_pos:
    draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow') 
  # 点击关闭窗口
  window.exitonclick()
if __name__ == '__main__':
    draw_5_star_flag()

结果:

总结

以上就是本文关于Python使用Turtle模块绘制五星红旗代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

详解Python的单元测试

如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生。 单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。 比如...

Python实现KNN(K-近邻)算法的示例代码

Python实现KNN(K-近邻)算法的示例代码

一、概述 KNN(K-最近邻)算法是相对比较简单的机器学习算法之一,它主要用于对事物进行分类。用比较官方的话来说就是:给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近...

编程语言Python的发展史

编程语言Python的发展史

Python是我喜欢的语言,简洁、优美、易用。前两天,我很激昂地向朋友宣传Python的好处。 “好吧,我承认Python不错,但它为什么叫Python呢?” “呃,似乎是一个电视剧的名...

python中numpy.zeros(np.zeros)的使用方法

翻译: 用法:zeros(shape, dtype=float, order='C') 返回:返回来一个给定形状和类型的用0填充的数组; 参数:shape:形状 dtype:数据类型,可...

python matplotlib中文显示参数设置解析

python matplotlib中文显示参数设置解析

最近在学习python著名的绘图包matplotlib时发现,有时候图例等设置无法正常显示中文,于是就想把这个问题解决了。 PS:本文仅针对Windows,其他平台仅供参考。 原因 大致...