python乌龟画花_使用Python的Swampy程序包中的“乌龟”画花朵,螺旋线,打字等
练习4-2
写一组合适的通用函数,用来画出下图所示的花朵图案。
这个是搬运过来的,代码如下:
from swampy.TurtleWorld import *
from math import *
def polyline(t,n,length,angle):
for i in range(n):
fd(t,length)
lt(t,angle)
def arc(t,r,angle):
arc_length = 2 * pi * r * abs(angle) /360
n = int(arc_length/4) + 1
step_length = arc_length / n
step_angle = float(angle) / n
lt(t,step_angle/2)
polyline(t,n,step_length,step_angle)
rt(t,step_angle/2)
def petal(t,r,angle):
for i in range(2):
arc(t,r,angle)
lt(t,180-angle)
def flower(t,n,r,angle):
for i in range(n):
petal(t,r,angle)
lt(t,360.0/n)
def move(t,length):
pu(t)
fd(t,length)
pd(t)
world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01
move(bob,-100)
flower(bob,7,60.0,60.0)
move(bob,100)
flower(bob,10,40.0,80.0)
move(bob,100)
flower(bob,20,140.0,20.0)
die(bob)
world.canvas.dump()
wait_for_user() 运行结果如下图所示:
由于对画图不是很感兴趣,感觉实习以后用不着,没花心思做,下面的代码还是搬运来的。
4-3 写一组合适的通用函数,用来画出下图所示的图形。
代码如下:
from math import *
from swampy.TurtleWorld import *
def draw_pie(t,n,r):
polypie(t,n,r)
pu(t)
fd(t,r*2+10)
pd(t)
def polypie(t,n,r):
angle = 360.0/n
for i in range(n):
isosceles(t,r,angle/2)
lt(t,angle)
def isosceles(t,r,angle):
y = r*sin(angle*pi/180)
rt(t,angle)
fd(t,r)
lt(t,90+angle)
fd(t,2*y)
lt(t,90+angle)
fd(t,r)
lt(t,180-angle)
world = TurtleWorld()
bob = Turtle()
bob.delay = 0
pu(bob)
bk(bob,130)
pd(bob)
size = 40
draw_pie(bob,5,size)
draw_pie(bob,6,size)
draw_pie(bob,7,size)
draw_pie(bob,8,size)
die(bob)
world.canvas.dump()
wait_for_user() 运行结果如下:
4-4 字母表中的字母可以使用一些基本元素来构成。如横线,竖线以及一些曲线。设计一个字体,可以使用最少的基本元素画出来,并编写函数来画出字母表中的所有的字母。
你应当给每个字母单独写一个函数,名为 draw_a,draw_b 等,并把这些函数放到letters.py文件中。可以从http://thinkpython.com/code/typewriter.py下载一个“乌龟打字机”来帮助测试你的代码。
letters.py代码如下 :
from swampy.TurtleWorld import *
from polygon import *
#level 0 primitives are provided by World.py
#They include fd,bk,lt,rt,pu and pd
#level 1 primitives are simple combinations of level 0 primitives
#They have no pre-or post-conditions
def fdlt(t,n,angle = 90):
"""forward and left"""
fd(t,n)
lt(t,angle)
def fdbk(t,n):
"""forward and back,ending at the original position"""
fd(t,n)
bk(t,n)
def skip(t,n):
"""lift the open and move"""
pu(t)
fd(t,n)
pd(t)
def stump(t,n,angle=90):
"""make a vertical line and leave the turtle at the top
facing right
"""
lt(t)
fd(t,n)
rt(t,angle)
def hollow(t,n):
"""move the turtle vertically and leave it at the top,
facing right"""
lt(t)
skip(t,n)
rt(t)
#level 2 primitives use primitives from level0 and 1
#to draw posts(vertical elements) and beams(horizontal elements)
#level 2 primitives always return the turtle to the original
#location and direction
def post(t,n):
"""make a vertical line and return to the original position"""
lt(t)
fdbk(t,n)
rt(t)
def beam(t,n,height):
"""make a horizontal line at the given height and return """
hollow (t,n*height)
fdbk(t,n)
hollow(t,-n*height)
def hangman(t,n,height):
"""make a vertical line to the given height and a horizontal line
at the given height and then return .
This is efficient to implement,and turns out to be useful,but
it is not so semantically clean."""
stump(t,n*height)
fdbk(t,n)
lt(t)
bk(t,n*height)
rt(t)
def diagonal(t,x,y):
"""make a diagonal line to the given x,y offsets and return"""
from math import atan2,sqrt,pi
angle = atan2(y,x)*180/pi
dist = sqrt(x**2+y**2)
lt(t,angle)
fdbk(t,dist)
rt(t,angle)
def vshape(t,n,height):
diagonal(t,-n/2,height*n)
diagonal(t,n/2,height*n)
def bump(t,n,height):
"""make a bump with radius n at height*n """
stump(t,n*height)
arc(t,n/2.0,180)
lt(t)
fdlt(t,n*height+n)
"""
The letter-drawing functions all have the precondtion
that the turtle is in the lower-left corner of the letter,
and postcondition that the turtle is in the lower-right
corner,facing in the direction it started in.
They all take a turtle as the first argument and a size(n)
as the second.Most letters are(n) units wide and(2n) units high
"""
def draw_a(t,n):
diagonal(t,n/2,2*n)
beam(t,n,1)
skip(t,n)
diagonal(t,-n/2,2*n)
def draw_b(t,n):
bump(t,n,1)
bump(t,n,0)
skip(t,n/2)
def draw_c(t,n):
hangman(t,n/2)
fd(t,n)
def draw_d(t,n):
bump(t,2*n,0)
skip(t,n)
def draw_ef(t,n):
hangman(t,n,2)
hangman(t,n,1)
def draw_e(t,n):
draw_ef(t,n)
fd(t,n)
def draw_f(t,n):
draw_ef(t,n)
skip(t,n)
def draw_g(t,n):
hangman(t,n,2)
fd(t,n/2)
beam(t,n/2,2)
fd(t,n/2)
post(t,n)
def draw_h(t,n):
post(t,2*n)
hangman(t,n,1)
skip(t,n)
post(t,2*n)
def draw_i(t,n):
beam(t,n/2)
fd(t,n/2)
post(t,2*n)
fd(t,n/2)
def draw_j(t,n):
beam(t,n,2)
arc(t,n/2,90)
fd(t,3*n/2)
skip(t,-2*n)
rt(t)
skip(t,n/2)
def draw_k(t,n):
post(t,2*n)
stump(t,n,180)
vshape(t,2*n,0.5)
fdlt(t,n)
skip(t,n)
def draw_l(t,n):
post(t,2*n)
fd(t,n)
def draw_n(t,n):
post(t,2*n)
skip(t,n)
diagonal(t,-n,2*n)
post(t,2*n)
def draw_m(t,n):
post(t,2*n)
draw_v(t,n)
post(t,2*n)
def draw_o(t,n):
skip(t,n)
circle(t,n)
skip(t,n)
def draw_p(t,n):
bump(t,n,1)
skip(t,n/2)
def draw_q(t,n):
draw_o(t,n)
diagonal(t,-n/2,n)
def draw_r(t,n):
draw_p(t,n)
diagonal(t,-n/2,n)
def draw_s(t,n):
fd(t,n/2)
arc(t,n/2,180)
arc(t,n/2,-180)
fdlt(t,n/2,-90)
skip(t,2*n)
lt(t)
def draw_t(t,n):
beam(t,n,2)
skip(t,n/2)
post(t,2*n)
skip(t,n/2)
def draw_u(t,n):
post(t,2*n)
fd(t,n)
post(t,2*n)
def draw_v(t,n):
skip(t,n/2)
vshape(t,n,2)
skip(t,n/2)
def draw_w(t,n):
draw_v(t,n)
draw_v(t,n)
def draw_x(t,n):
diagonal(t,n,2*n)
skip(t,n)
diagonal(t,-n,2*n)
def draw_y(t,n):
skip(t,n/2)
stump(t,n)
vshape(t,n,1)
rt(t)
fdlt(t,n)
skip(t,n/2)
def draw_z(t,n):
beam(t,n,2)
diagonal(t,n,2*n)
fd(t,n)
def draw_(t,n):
#draw a space
skip(t,n)
if _name_ == '_main_':
world = TurtleWorld()
#create and position the turtle
size = 20
bob = Turtle()
bob.delay = 0.01
for f in [draw_h,draw_e,draw_l,draw_o]:
f(bob,size)
skip(bob,size)
wait_for_user()
python乌龟画花_使用Python的Swampy程序包中的“乌龟”画花朵,螺旋线,打字等相关推荐
- 使用Python的Swampy程序包中的“乌龟”画图及有意思的习题
Swampy程序包下载地址为:http://thinkpython.com/swampy Swampy中有个模块"乌龟世界"(TurtleWorld),提供各种函数,可以控制一只乌 ...
- python绘制彩虹花_在python的pygame设置彩虹弦:使用汉字变量及代码分析
#---第1步---导出模块--- importpygame,sysfrom pygame.locals import * importmathimportcolorsys#---第2步--初始化游戏 ...
- 怎么python画好几朵玫瑰花_用Python画朵玫瑰,只要五分钟
又到一年母亲节,先祝福所有妈妈节日快乐,哈哈.最近一个项目在用python,想着这次不用java了,用python给妈妈个礼物吧.Turtle库是Python语言中一个非常强大的绘制图像的函数库,她提 ...
- 用python画明星_用python画一只可爱的皮卡丘实例
效果图片# !\/usr\/bin\/env python # - * -编码:utf - 8 - *\u2014\u2014从龟进口*\u201C画皮卡丘的头\u201Cdef的脸(x, y):\u ...
- python canvas画弧度_用Python画樱花?想得美就能画得美(下)
上一篇我们介绍了一种手绘玫瑰的方法,你当然也可以用类似的方法画一朵或者几朵樱花 咯,看你的艺术底子了. 不过今天我们用优美的数学方法来画樱花,也会很漂亮的. 先画朵太阳花暖暖身吧. import tu ...
- pythonturtle画房子_用python的turtle模块实现给女票画个小心心
晚上自习无聊 正好拿自己的平板电脑用python写了个小程序,运用turtle模块画一个小心心,并在心上画女票名字的首字母缩写,单纯只为红颜一笑. 代码贴出来,很简单 import turtle im ...
- python画图皮卡丘_用python画一只可爱的皮卡丘
#!/usr/bin/env python # -*- coding:utf-8 -*- from turtle import * ''' 绘制皮卡丘头部 ''' def face(x,y): &qu ...
- python画抛物线_在python中利用最小二乘拟合二次抛物线函数的方法
1.最小二乘也可以拟合二次函数 我们都知道用最小二乘拟合线性函数没有问题,那么能不能拟合二次函数甚至更高次的函数呢?答案当然是可以的.下面我们就来试试用最小二乘来拟合抛物线形状的的图像. 对于二次函数 ...
- python画樱桃小丸子_学python画图最快的方式——turtle小海龟画图
原标题:学python画图最快的方式--turtle小海龟画图 python中的画图方式主要有三种:turtle.tkinter.pygame,我们先从最简单的turtle开始. 学python画图最 ...
最新文章
- 太厉害了!目前 Redis 可视化工具最全的横向评测
- windows下vs2013使用C++访问redis
- QML的默认属性default property
- lapack安装_VASP环境安装-BoltzTraP-1.2.5安装
- Scala 中将方法、函数、函数式编程和面向对象编程关系分析图
- linux 安全审计功能,数据库安全审计在数据安全中的功能
- .net WebApi开发
- httpd的三种模式比较
- 再谈.NET Micro Framework移植
- linux mysql配置文件修改编码,linux修改mysql字符集编码
- Android NFC技术解析,附Demo源码
- 转:爬虫入门 手写一个Java爬虫
- vue中a的href写法
- 物理层(网线)、数据链路层(交换机)、网络层(IP协议、ARP协议、ICMP协议、路由器)、VLAN(虚拟局域网)、HSRP协议、ACL、NAT
- 《Photoshop+Lightroom数码摄影后期处理经典教程》—第1章1.5节准备将Lightroom和Photoshop结合起来...
- 实战PyQt5: 141-QChart图表之箱形图
- Java基础学习—— IO流
- repo中manifest解析
- centos7 图形界面
- Python爆破校园饮水机系统,喝水还要花钱?不存在的,免费饮水!
热门文章
- 使用Visual Studio编写计算器
- Windows2000的终端服务功能及应用
- 堡垒机怎么安装mysql_JumpServer堡垒机安装
- 08.01大疆创新2022数字芯片笔试
- pandas 读取指定列 usecols
- 服务器电源能支持高压直流吗,通信高压直流电源
- substance-Substance_Painter记录
- html微信拆红包,用React加CSS3实现微信拆红包动画_html/css_WEB-ITnose
- 电信中兴F460光猫sendcmd命令
- 证照之星XE7下载安装详细教程