Python_Day01(Hello和Turtle库)

Python_Day01(Hello和Turtle库)

Hello world

// hello.py
print('Hello world!')
print('你好,世界!')
print('你好','世界')
print('hello', 'world', sep=', ', end='!')
print('goodbye, world', end='!\n')

Turtle库的方法

1.forward() | fd():向前移动指定的距离。参数:一个数字(integer or float))。

turtle.forward(25)

2.backward() | bk() | back():向后移动指定的距离。参数:一个数字(integer or float))。

turtle.backward(30)

3…right() | rt():以角度单位向右转动。参数:一个数字(integer or float))。单位默认为度数,可以通过degrees()和radians()进行设置。

turtle.right(45)

4.left() | lt():以角度单位向左转动。参数:一个数字(integer or float))。单位默认为度数,可以通过degrees()和radians()进行设置。

tuetle.left(45)

5.goto() | steps() | setposition():移动到绝对位置,如果笔落下,画线,不改变方向。参数:x-一个数字或一对数字。y-一个数字或None。

turtle.setpos(60,30)
#(60.00,30.00)

6.setx():设置第一个坐标的值即X方向。参数:一个数字(integer or float))。

turtle.setx(10)
    #(10.000.00

7.sety():设置第二个坐标的值即Y方向。参数:一个数字(integer or float))。

turtle.sety(10)
    #(0.0010.00

8.setheading() | seth(): 将方向设置为to_angle.就是东西南北方向。具体如下:

标准模式:0 - 东 90 - 北 180 - 西 270 - 南 标志模式 0- 北 90- 东 180- 南 270 - 西

turtle.setheading(90)

9.home() : 移动到原点 - 坐标(0,0):并将其标题设置为其起始方向(取决于模式)。

turtle.home()
# (0.000.00

10.circle():绘制一个给定半径的圆。参数:radius-一个数字(半径,如果值为正则逆时针,负数为顺时针),extent-一个数字()steps- 执行的步数。

turtle.circle(120,180,5)

11.dot() :用颜色画出一个直径大小的圆点。参数:size-一个大于1的整数,可None。默认用的是pensize+4和2*pensize的最大值,color-颜色值

turtle.dot(20, "blue")

12.stamp():将当前位置上的形状复制到画布上返回stamp_id.可以调用,可以删除。

turtle.stamp()

13.clearstamp():删除stamp()返回来的值。参数:stampid。

14.clearstamps():删除全部stamp()的值。参数:stampid。

15.undo():撤销最后的动作。

16.speed():将速度设置为0…10范围内整数。如果没有参数,则返回当前速度。如果大于10或者小于0.5,则速度设置为0 。

“最快”:0 :直接成图,没有动画效果
“快”:10:大概一秒
“正常”:6:
“慢”:3
“最慢”:1

17:position() | pos(): 返回当前的位置。

turtle.pos()
# (0.00,0.00)

18.towards(): 返回当前位置同指定位置之间的角度。参数:x-一个数字或一对数字或一个实例的向量,y-如果x是数字,则为数字,否则为None。

turtle.goto(10,10)
tw = turtle.towards(0,0)
print(tw)
# 225

19.xcor():返回x坐标。

 ycor():返回y坐标。

20.heading(): 返回当前的方向值。

21.distance():返回x,y两个点的直线距离

22.degrees():设置一整圈的度数。默认是360度。

23.radians():将角度测量单位设置为弧度。相当于 degrees(2*math.pi)

24.pendown() | pd() | down():放下笔,移动的时候绘图。

25.penup() | pu() | up():将提起笔,移动时无图。

26.pensize():设置线条的粗细。参数:width-一个正数

27.pen():使用键值对设置笔的属性:

“shown”: True/False
“pendown”: True/False
“pencolor”: 颜色字符串或者颜色值
“fillcolor”: 颜色字符串或者颜色值
“pensize”: 正数
“speed”: 速度范围为0..10的数字
“resizemode”: “auto” or “user” or “noresize”
“stretchfactor”: (positive number, positive number)
“outline”: 正数
“tilt”: 正数

28.isdown():如果笔停止返回True,反之False

29.pencolor():设置笔的颜色。

30.fillcolor():笔的填充色。

31.color():同时设置pencolor和fillcolor

32.filling():返回fillstate状态,如果填充则返回True,反之False。

33.begin_fill():在绘制要填充的形状前调用。当然在调用完还需要end_fill()。

34.reset():重置,将屏幕中的图纸删除,重新居中并将所有变量设置为默认值。

35.clear():删除图纸。对属性不做操作。

36.write():写文本。参数:arg-要写入的对象。move-是否移动。align-对齐方式:left,right,center。font-字体。fontname,fontsize,fonttype。

37.hideturtle() | ht() :删隐藏乌龟的形状,在做复杂绘图的时候隐藏的时候有利于提高绘图速度。

38.showturtle() | st():显示乌龟的形状。

39.isvisible():乌龟是否可见。如果可见返回True,反之则False。

40.shape():设置乌龟的图形形状,取值:“arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”

41.isvisible():乌龟是否可见。如果可见返回True,反之则False。

42.resizemode():参数:rmode取值:“auto”,“user”,“noresize”.

43.shapesize() | turtlesize() : 返回笔的属性。

44.shearfactor(): 设置或者返回但钱的剪切因子。

45.tilt():旋转由turtle shape角度从当前的倾斜角度。

46.settiltangle():无论当前的倾斜角度如何,旋转乌龟指向 angle 指定的方向。参数:angle -数字。已弃用

47.tiltangle():设置或者返回当前的倾斜角度。参数:angle - 数字

48.shapetransform():设置或返回乌龟的形状的当前转换矩阵。

49.get_shapepoly():返回当前形状的坐标。

50.onclick():鼠标点击事件。参数:fun-一个带有两个参数的函数,这些参数将与画布上单击点的坐标一个调用。num-鼠标按钮的数量,默认为1(左键)。add- True的时候将添加新的绑定。否则替换以前的绑定。

51.onrelease():鼠标释放事件。参数同点击事件。

52.ondrag():鼠标移动事件。参数同点击事件。

53.begin_poly(): 开始记录多边形的顶点。

54.end_poly():停止记录多边形的顶点。

55.get_poly():返回最后记录的多边形。

56.clone():创建并返回具有相同位置等等属性的乌龟克隆。

57.getturtle() | getpen() :获取trutle对象本身。

58.getscreen():返回正在绘制的对象。

59.setundobuffer(): 设置或禁用中断器。参数: size-整数。如果大小是None,则禁用缓冲区。

60.undobufferentries():返回undobuffer中的条目数。

61.bgcolor():设置或者返回当前的TurtleScreen的背景颜色。

62.bgpic():设置背景图片。参数: picname-文件名。

62.delay(): 设置或返回以毫秒为单位的绘制延迟,延迟越大,绘图越慢。

63.ontimer():定时器。

64.mainloop() | done() :开始循环 。

65.textinput() | numinput():弹出一个输入字符串和数字的窗口。

66.mode(): 三种方式:“standard”, “logo” or “world”

海龟库的实例

"""
用Python的turtle模块绘制国旗
"""
import turtle


def draw_rectangle(x, y, width, height):
    """绘制矩形"""
    turtle.goto(x, y)
    turtle.pencolor('red')
    turtle.fillcolor('red')
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
    turtle.end_fill()
def draw_star(x,y,radius):
    """绘制五角星"""
    turtle.setpos(x, y)
    pos1 = turtle.pos()
    turtle.circle(-radius, 72)
    pos2 = turtle.pos()
    turtle.circle(-radius, 72)
    pos3 = turtle.pos()
    turtle.circle(-radius, 72)
    pos4 = turtle.pos()
    turtle.circle(-radius, 72)
    pos5 = turtle.pos()
    turtle.color('yellow', 'yellow')
    turtle.begin_fill()
    turtle.goto(pos3)
    turtle.goto(pos1)
    turtle.goto(pos4)
    turtle.goto(pos2)
    turtle.goto(pos5)
    turtle.end_fill()

def main():
    """主程序"""
    turtle.speed(5)
    turtle.penup()
    x, y = -270, -180
    # 画国旗主体
    width, height = 540, 360
    draw_rectangle(x, y, width, height)
    # 画大星星
    pice = 22
    center_x, center_y = x + 5 * pice, y + height - pice * 5
    turtle.goto(center_x, center_y)
    turtle.left(90)
    turtle.forward(pice * 3)
    turtle.right(90)
    draw_star(turtle.xcor(), turtle.ycor(), pice * 3)
    x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9]
    # 画小星星
    for x_pos, y_pos in zip(x_poses, y_poses):
        turtle.goto(x + x_pos * pice, y + height - y_pos * pice)
        turtle.left(turtle.towards(center_x, center_y) - turtle.heading())
        turtle.forward(pice)
        turtle.right(90)
        draw_star(turtle.xcor(), turtle.ycor(), pice)
    # 隐藏海龟
    turtle.ht()
    # 显示绘图窗口
    turtle.mainloop()


if __name__ == '__main__':
    main()

在这里插入图片描述

小猪佩奇

"""
绘制小猪佩奇
"""
from turtle import *


def nose(x,y):
    """画鼻子"""
    penup()
    # 将海龟移动到指定的坐标
    goto(x,y)
    pendown()
    # 设置海龟的方向(0-东、90-北、180-西、270-南)
    setheading(-30)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i <90:
            a = a + 0.08
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.08
            left(3)
            forward(a)
    end_fill()
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    # 设置画笔的颜色(红, 绿, 蓝)
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()
    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()


def head(x, y):
    """画头"""
    color((255, 155, 192), "pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300, -30)
    circle(100, -60)
    circle(80, -100)
    circle(150, -20)
    circle(60, -95)
    setheading(161)
    circle(-300, 15)
    penup()
    goto(-100, 100)
    pendown()
    setheading(-30)
    a = 0.4
    for i in range(60):
        if 0<= i < 30 or 60 <= i < 90:
            a = a + 0.08
            lt(3) #向左转3度
            fd(a) #向前走a的步长
        else:
            a = a - 0.08
            lt(3)
            fd(a)
    end_fill()


def ears(x,y):
    """画耳朵"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 56)
    end_fill()


def eyes(x,y):
    """画眼睛"""
    color((255, 155, 192), "white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    color((255, 155, 192), "white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()


def cheek(x,y):
    """画脸颊"""
    color((255, 155, 192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()


def mouth(x,y):
    """画嘴巴"""
    color(239, 69, 19)
    penup()
    goto(x, y)
    pendown()
    setheading(-80)
    circle(30, 40)
    circle(40, 80)


def setting():
    """设置参数"""
    pensize(4)
    # 隐藏海龟
    hideturtle()
    colormode(255)
    color((255, 155, 192), "pink")
    setup(840, 500)
    speed(10)


def main():
    """主函数"""
    setting() 
    nose(-100, 100)
    head(-69, 167)
    ears(0, 160)
    eyes(0, 140)
    cheek(80, 10)
    mouth(-20, 30)
    done()


if __name__ == '__main__':
    main()

在这里插入图片描述
参考文档

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值