利用turtle模块画图

代码:

import turtle
#导入画图模块,查看已安装的模块dir(‘modules‘)或者help(‘modules‘)命令,查看模块详情help(‘turtle‘)
t=turtle.Turtle()
#调用模块里面的Turtle工具,调用模块:模块名.函数名    turtle.Turtle()
t.speed(0)
#画笔速度1-9依次变快,0为最快速度
def setpen(x,y):
      t.penup()
      t.goto(x,y)
      t.pendown()
      t.setheading(0)
#定义函数,定义画笔的起点及朝向(x,y)为起点 t.setheading(0)设置朝向为0
#penup提笔,pundown落笔,goto移动
#也可直接跟参数定义函数。
# def setpen():
#     t.penup()
#     t.goto(10,10)
#     t.pendown()
#     t.sedheading(0)
# setpen()   调用
def circle(x,y,r,color):
      n=36
      angle=360/n
      p=3.1415926
      c=2*p*r
      l=c/n
      point_x=x-l/2
      point_y=y+r
      setpen(point_x,point_y)
      t.pencolor(color)
      t.fillcolor(color)
      t.begin_fill()
      for i in range(n):
           t.forward(l)
           t.right(angle)
      t.end_fill()
#定义圆,pencolor画笔颜色,fillcolor填充颜色,range(n)区间,边数大于36以上时,可认为是圆形。
#for循环一般形式:
#for <variable> in <sequence>:
#    <statements>
#else:
#    <statements>

#while循环语句一般形式:
#while 判断条件:
#      语句
#else:
#      语句
circle(0,0,200,‘red‘)
circle(0,0,155,‘white‘)
circle(0,0,110,‘red‘)
circle(0,0,65,‘blue‘)
#调用定义的函数。
def five_star():
      setpen(0,0)
      t.setheading(161)
      t.forward(65)
      t.setheading(0)
      t.fillcolor(‘white‘)
      t.begin_fill()
      t.hideturtle()
      t.penup()
      for i in range(5):
          t.forward(124)
          t.right(144)
      t.end_fill()
#定义五角星,hideturtle隐藏画笔
five_star()
#调用定义的函数
turtle.done()
#完成

原文地址:https://blog.51cto.com/13689359/2411439

时间: 2024-07-29 21:59:33

利用turtle模块画图的相关文章

使用Python的turtle(海龟)模块画图

使用Python的turtle(海龟)模块画图 第一步:让Python引入turtle模块,引入模块就是告诉Python你想要用它. import turtle 第二步:创建画布.调用turtle中的Pen函数. t = turtle.Pen() 第三步:移动海龟. t.forward(50) forward的中文意思是"向前地:促进".所以这行代码的意思是海龟向前移动50个像素: t.left(90) 让海龟左转90度 现在我们可以尝试画一个方块,思路就是前进-转向90度-前进,循环

#18 turtle模块

前言 这一节继续记录模块,本节将记录Python中一个非常重要的画图模块——turtle,Here we go! 一.turtle模块 turtle(海龟)模块是Python中强大的内置画图模块,可以模拟真实的画图环境以及画图步骤来画图.通常,我们画图需要两种工具,一个是画布,另一个是画笔:在turtle中,同样需要这两种工具,首先来学习画布的设置 1. 画布 画布无非不过设置画布大小.背景颜色.画布在桌面的位置,turtle模块中,有两种画布的设置方法,需要时可将它们结合起来使用: turtl

用Python的Turtle模块绘制五星红旗

Edit 用Python的Turtle模块绘制五星红旗 在Udacity上课时学到了python的turtle方法,这是一个很经典的用来教小孩儿编程的图形模块,最早起源于logo语言.python本身内置了这个模块,其可视化的方法可以帮助小孩儿对编程的一些基本理念有所理解. 在作业提交的论坛里看到很多turtle画出来的精美图形,想不出什么要画的东西,于是决定拿五星红旗来练练手. 前期准备 五星红旗绘制参数 Turtle官方文档 turtle的基本操作 # 初始化屏幕 window = turt

利用perl模块发邮件

vim sendmail.pl #!/usr/bin/perl -w use MIME::Lite; sub perl_sendmail {         my $mail_to=shift;         #my $mail_to="$_[0]";         my $from=shift;         #my $from = "$_[1]";         my $subject=shift;         #my $subject = &quo

[Python] 利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要函数: 1. commands.getoutput('shell command') 执行shell命令,返回结果(string类型) >>> commands.getoutput('pwd') '/home/oracle' 2. commands.getstatus('file') 该函数

[stm32] 利用uc-gui封装画图和画线函数移植51上的模拟动画

>_<:这里的动画是黄色矩形区域中一个模仿俯视图的起重机运作动画,一个是模仿主视图的吊钩的运动.通过改变初始Init函数中的数据b_x,b_y实现矩形区域的移动.当实时采集时要首先根据起重机的实际情况改变比例,当传感器传来数据时就相当于这里的run函数,只要把传感器数据接收函数和相关函数结合即可. >_<:main code: 1 #include "stdlib.h" 2 #include "GUI.H" 3 /* 4 颜色 5 */ 6

nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录

geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_module模块可以用来创建变量,其值依赖于客户端IP地址.geo指令语法: geo [$address] $variable { ... }默认值: -配置段: http定义从指定的变量获取客户端的IP地址.默认情况下,nginx从$remote_addr变量取得客户端IP地址,但也可以从其他变量获得.例

后端Nodejs利用node-xlsx模块读取excel

后端Nodejs(利用node-xlsx模块) /** * Created by zh on 16-9-14. */ var xlsx = require("node-xlsx"); var list = xlsx.parse("/home/zh/桌面/API_BTN_DS2_zh_excel_v2.xlsx"); console.log(list[2]['name']); for(var rowid in list[2]['data']){ row=list[2]

使用turtle模块绘制多边图形

python 版本:3.4.3 import 模块:turtle 使用turtle模块绘制多边图形,代码如下:  #-*- charset:utf-8 -*- import turtle #设置画笔笔迹宽度 turtle.pensize(3) #画三边形 turtle.color("red") turtle.penup() turtle.goto(-200,-50) turtle.pendown() turtle.circle(40, steps=3) #画四边形 turtle.col