700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python turtle画彩虹的代码_Python turtle画图库画姓名实例

python turtle画彩虹的代码_Python turtle画图库画姓名实例

时间:2018-12-10 12:11:30

相关推荐

python turtle画彩虹的代码_Python turtle画图库画姓名实例

*****看一下我定义的change()和run()函数******

绘图坐标体系:

作用:设置主窗体的大小和位置

?

1

turtle.setup(width, height, startx, starty)

#width : 窗口宽度,若值是整数,表示像素值;若是小数,表示窗口宽度与屏幕的比例。

#height : 窗口高度,若值是整数,表示像素值;若是小数,表示窗口高度与屏幕的比例。

#startx : 窗口左侧与屏幕左侧的像素距离,若值是None,窗口位于屏幕水平中央。

#starty : 窗口顶部与屏幕顶部的像素距离,若值是None,窗口位于屏幕垂直中央。

画笔控制函数:

turtle中的画笔(即小海龟)可以通过一组函数来控制;

?

1

2

3

4

5

6

turtle.penup();#抬起画笔(画笔移动时没有痕迹)

turtle.pendown();#放下画笔(画笔移动时有痕迹,即作画中)

turtle.pensize(width)#设置画笔的尺寸

turtle.width()#设置画笔宽度,当无参数输入时返回当前画笔宽度

turtle.pencolor(colorstring)#设置画笔颜色

turtle.goto(x, y)#将画笔移动至(x,y)处

形状绘制函数:

?

1

2

3

turtle.fd(distance)#画笔移动distance距离

turtle.seth(angle)#设置当前画笔的方向为angle

turtle.circle(radius, extent=None)#根据半径radius绘制extent角度的弧形

我为了精简代码,定义了两个函数:

change(x, y) : 将画笔的位置设置在(x, y)

run(angle, length) : 方向为angle ,画笔前进length个单位长度

?

1

2

3

4

5

6

7

8

def run(angle, lenth):

turtle.seth(angle)

turtle.fd(lenth)

def change(x, y):

turtle.penup()

turtle.goto(x, y)

turtle.pendown()

这里我使用 汉字 “中” 来举个例子:

我们把“中” 字抽象成点。(如图所示的 A、B、C、D、E、F 六个点)

然后,我们就是要把他们连接起来啊。 我们先连接ABCD四个点。

变成下图

然后再连接EF两点。完成。

对应的代码:

?

1

2

3

4

5

6

7

8

9

#zhong

change(-100,80)#将画笔放在坐标A(-100, 80)

run(0,200)#从A点画到B点

run(-90,120)#从B画到C点

run(180,200)#从C点画到D点

run(90,120)#从D点画到A点

change(0,150)#将画笔放在坐标E(0, 150)

run(-90,300)#从E点画到F点

画自己的名字嘤嘤嘤

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

import turtle

def run(angle, lenth):

turtle.seth(angle)

turtle.fd(lenth)

def change(x, y):

turtle.penup()

turtle.goto(x, y)

turtle.pendown()

def init():

turtle.pensize(10)

turtle.pencolor("purple")

turtle.setup(800,400,200,200)

init()

#wu

change(-350,100)

run(0,100)

run(-90,70)

run(180,100)

run(90,70)

change(-350,0)

run(0,100)

change(-400,-50)

run(0,200)

change(-300,0)

run(-90,50)

run(-125,100)

change(-300,-50)

run(-55,100)

run(0,10)

#feng

change(-65,70)

run(0,130)

change(-50,10)

run(0,100)

change(-100,-70)

run(0,200)

change(0,-160)

run(90,280)

#yuan

change(125,100)

run(-35,40)

change(125,25)

run(-35,40)

change(125,-125)

run(55,90)

change(220,100)

run(0,110)

change(220,100)

run(-95,230)

change(285,85)

run(-145,40)

change(250,65)

run(0,70)

run(-90,80)

run(180,70)

run(90,80)

change(250,25)

run(0,70)

change(285,-15)

run(-90,120)

run(135,20)

change(250,-50)

run(-125,40)

change(315,-50)

run(-55,50)

以上这篇Python turtle画图库&&画姓名实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:/l1833970/article/details/84070971

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。