Python基础知识及Turtle库的使用

Python打卡1

练习

1.在Python的交互式环境中输入下面的代码并查看结果,并尝试将看到的内容翻译成中文。

import this

输出结果:

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it. A

lthough that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than right now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

2.学习使用turtle在屏幕上绘制图形。

说明:turtle是Python内置的一个非常有趣的模块,特别适合对计算机程序设计进行初体验的小伙伴,它最早是Logo语言的一部分,Logo语言是Wally Feurzig和Seymour Papert在1966发明的编程语言。

import turtle
​
turtle.pensize(4)
turtle.pencolor('red')
​
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
​
turtle.mainloop()

说明:绘制的图形为正方形。

3.turtle库详解。

(1) 画笔运动命令

命令说明
turtle.forward(distance)向当前画笔方向移动(distance)像素长度
turtle.backward(distance)向当前画笔相反方向移动(distance)像素长度
turtle.right(degree)顺时针移动degree°
turtle.left(degree)逆时针移动degree°
turtle.pendown()移动时绘制图形,缺省时也为绘制
turtle.goto(x,y)将画笔移动到坐标为x,y的位置
turtle.penup()提起笔移动,不绘制图形,用于另起一个地方绘制
turtle.circle()画半径为()的圆,表示圆心在画笔的左边(右边)画圆
setx()将当前x轴移动到指定位置
sety()将当前y轴移动到指定位置
setheading(angle)设置当前朝向为angle角度
home()设置当前画笔位置为原点,朝向东
dot(r)绘制一个指定直径和颜色的圆点

(2) 画笔控制命令

命令说明
turtle.fillcolor(colorstring)绘制图形的填充颜色
turtle.color(color1,color2)同时设置pencolor=color2,fillcolor=color2
turtle.filling()返回当前是否在填充状态
turtle.begin_fill()准备开始填充图形
turtle.end_fill()填充完成
turtle.hideturtle()隐藏画笔的turtle形状
turtle.showturtle()显示画笔的turtle形状

(3) 全局控制命令

命令说明
turtle.clear()清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset()清空窗口,重置turtle状态为起始状态
turtle.undo()撤销上一个turtle动作
turtle.isvisible()返回当前turtle是否可见
stamp()复制当前图形
turtle.write(s [,font=("font-name",font_size,"font_type")])写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值