python turtle库画树_python_turtle库_应用之画圣诞树

turtle库介绍

1、Turtle中的turtle.setup()函数用于启动一个图形窗口,它有四个参数

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

分别是:启动窗口的宽度和高度表示窗口启动时,窗口左上角在屏幕中的坐标位置。

我们所使用的显示屏幕也是一个坐标系,该坐标系以左上角为原点,向左和向下分别是x轴和y轴。蟒蛇程序代码启动一个1300像素宽、800像素高的窗口,该窗口的左上角是屏幕的左上角。

(startx,starty)表示画的初始点,(0,0)表示位于电脑屏幕中心

2、Turtle中的turtle.pensize()函数表示小乌龟运动轨迹的宽度。

3、Turtle中的turtle.pencolor()函数表示小乌龟运动轨迹的颜色。

它包含一个输入参数,这里我们把它设为蓝色,blue,其他颜色单词也可以使用。Turtle采用RGB方式来定义颜色,如果希望获得和图片中颜色一致的小蛇,请输入turtle.pencolor(“#3B9909”)

4、Turtle中的turtle.seth(angle)函数表示小乌龟启动时运动的方向。它包含一个输入参数,是角度值。

其中,0表示向东,90度向北,180度向西,270度向南;负值表示相反方向。程序中,我们让小乌龟向-40度启动爬行,即:向东南方向40度。

5、turtle.circle()函数让小乌龟沿着一个圆形爬行

参数rad描述圆形轨迹半径的位置,这个半径在小乌龟运行的左侧,rad远位置处。如果 rad为负值,则半径在小乌龟运行的右侧, 参数angle表示小乌龟沿着圆形爬行的弧度值。

6、turtle.fd()函数也可以用turtle.forward()表示乌龟向前直线爬行移动表示小乌龟向前直线爬行移动,

它有一个参数表示爬行的距离

7、详细参数描述

程序1

import turtle

def drawSnake(rad,angle,num,neckrad):

for i in range(num):

turtle.circle(rad,angle)

turtle.circle(-rad,angle)

turtle.circle(rad,angle/2)

turtle.fd(rad)

turtle.circle(neckrad+1,180)

turtle.fd(rad*2/3)

def main():

turtle.setup(1300,800,0,0)

pythonsize=30

turtle.pensize(pythonsize)

turtle.pencolor('blue')

turtle.seth(-40)

drawSnake(40,80,3,pythonsize/2)

main()

2、更改颜色

3、三角形

import turtle

turtle.setup(1000,1000,0,0)

size=20

turtle.pensize(size)

turtle.color("red")

length=200

turtle.seth(0)

turtle.fd(length)

turtle.seth(120)

turtle.fd(length)

turtle.seth(240)

turtle.fd(length)

4、五角星

from turtle import *

color("yellow","red")

pensize(10)

begin_fill()#和end_fill成对出现,填充起点和终点

while True:

forward(200)

right(144)

if abs(pos())<1:#获取位置的绝对值

break

end_fill()

import turtle

turtle.pensize(10)

turtle.fillcolor("red")

turtle.begin_fill()

for i in range(5):

turtle.fd(200)

turtle.right(144)

turtle.end_fill()

比较上面两段代码,第一种通过计算位置距离结束while循环,第二种通过确定数目的if循环结束程序。

第一种通过from turtle import * 引入turtle库,程序中调用函数,不用再加turtle.前缀,第二种通过import turtle引入turtle库,调用函数需要加turtle.前缀。

5、太阳花

1 from turtle import *

2 color("yellow","red")

3 pensize(3)

4 begin_fill()#和end_fill成对出现,填充起点和终点

5 while True:

6 forward(200)

7 right(165)

8 if abs(pos())<1:#获取位置的绝对值

9 break

10 end_fill()

更改旋转角度,得到漂亮的花

#最简单的圣诞树

1height= 5

23stars= 14for i inrange(height):5 print((' ' * (height - i)) + ('*' *stars))6 stars += 27print((' ' * height) + '|')

#turtlr方法一

1import turtle

2screen = turtle.Screen()

3screen.setup(800,600)

4circle = turtle.Turtle()

5circle.shape('circle')

6circle.color('red')

7circle.speed('fastest')

8circle.up()

9square = turtle.Turtle()

10square.shape('square')

11square.color('green')

12square.speed('fastest')

13square.up()

14circle.goto(0,280)

15circle.stamp()

16k = 0

17for i in range(1, 17):

18    y = 30*i

19    for j in range(i-k):

20        x = 30*j

21        square.goto(x,-y+280)

22        square.stamp()

23        square.goto(-x,-y+280)

24        square.stamp()

25    if i % 4 == 0:

26        x = 30*(j+1)

27        circle.color('red')

28        circle.goto(-x,-y+280)

29        circle.stamp()

30        circle.goto(x,-y+280)

31        circle.stamp()

32        k += 2

33    if i % 4 == 3:

34        x = 30*(j+1)

35        circle.color('yellow')

36        circle.goto(-x,-y+280)

37        circle.stamp()

38        circle.goto(x,-y+280)

39        circle.stamp()

40square.color('brown')

41for i in range(17,20):

42    y = 30*i

43    for j in range(3):

44        x = 30*j

45        square.goto(x,-y+280)

46        square.stamp()

47        square.goto(-x,-y+280)

48        square.stamp()

49turtle.exitonclick()

#turtle方法2

1from turtle import *2importrandom

3importtime45n= 80.0

67speed("fastest")

8screensize(bg='seashell')

9left(90)

10forward(3*n)

11color("orange", "yellow")

12begin_fill()

13left(126)1415for i in range(5):16 forward(n/5)17 right(144)18 forward(n/5)19 left(72)

20end_fill()

21right(126)2223color("dark green")

24backward(n*4.8)

25deftree(d, s):26 if d <= 0: return

27forward(s)28 tree(d-1, s*.8)29 right(120)30 tree(d-3, s*.5)31 right(120)32 tree(d-3, s*.5)33 right(120)34backward(s)

35tree(15, n)

36backward(n/2)3738for i in range(200):39 a = 200 - 400 *random.random()40 b = 10 - 20 *random.random()41up()42forward(b)43 left(90)44forward(a)45down()46 if random.randint(0, 1) ==0:47 color('tomato')48 else:49 color('wheat')50 circle(2)51up()52backward(a)53 right(90)54backward(b)

55time.sleep(60)

View Code

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是用Pythonturtle库画圣诞树的代码: ```python import turtle # 设置画布大小和背景颜色 turtle.setup(800, 600) turtle.bgcolor("white") # 定义画笔颜色和粗细 turtle.pensize(3) turtle.pencolor("green") # 画圣诞树的主干 turtle.penup() turtle.goto(, -200) turtle.pendown() turtle.setheading(90) turtle.forward(200) # 画圣诞树的枝干 turtle.left(90) turtle.forward(100) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.forward(100) # 画圣诞树的叶子 turtle.penup() turtle.goto(, ) turtle.pendown() turtle.begin_fill() turtle.color("green") turtle.circle(100) turtle.end_fill() turtle.penup() turtle.goto(, 100) turtle.pendown() turtle.begin_fill() turtle.color("green") turtle.circle(80) turtle.end_fill() turtle.penup() turtle.goto(, 180) turtle.pendown() turtle.begin_fill() turtle.color("green") turtle.circle(60) turtle.end_fill() turtle.penup() turtle.goto(, 240) turtle.pendown() turtle.begin_fill() turtle.color("green") turtle.circle(40) turtle.end_fill() # 画圣诞树的星星 turtle.penup() turtle.goto(, 280) turtle.pendown() turtle.begin_fill() turtle.color("yellow") for i in range(5): turtle.forward(50) turtle.right(144) turtle.end_fill() # 隐藏画笔 turtle.hideturtle() # 显示画布 turtle.done() ``` 运行上述代码,即可在Pythonturtle画布上看到一个圣诞树的图案。 ### 回答2: Python是一种强大的编程语言,有许多有用的可供使用。其中一个有趣的Turtle,可以与Python一起使用来创建简单的图形。我们可以使用这个来画一个圣诞树。 首先,我们需要导入Turtle。我们可以这样做: ```python import turtle ``` 接下来,我们可以创建一个Turtle对象,这个对象将帮助我们创建的各个部分。 ```python pen = turtle.Turtle() ``` 接下来,我们需要设置Turtle对象的速度和颜色。 ```python pen.speed(0) pen.color("green") ``` 然后,我们可以使用循环语句创建每个层次,从底部开始。 ```python for i in range(3): pen.forward(100) pen.left(120) ``` 接下来,我们可以使用循环创建的每个分支。 ```python for j in range(3): pen.forward(50) pen.left(120) pen.forward(50) pen.right(120) ``` 接下来,我们可以使用Turtle来创建小星星。 ```python pen.color("yellow") pen.penup() pen.goto(0, 70) pen.pendown() for k in range(5): pen.forward(20) pen.right(144) ``` 最后,我们可以写一个函数来画整个,该函数接受一些参数,如高度、分支数量等。 ```python def draw_tree(height, branches): pen.penup() pen.goto(0, -300) pen.pendown() for h in range(height): pen.width(h) pen.color("green") for i in range(branches): pen.forward(100) pen.left(360/branches) for j in range(branches): pen.color("brown") pen.pensize(h // 2) pen.forward(50) pen.color("green") pen.pensize((h // 2) + 1) pen.right(360/branches) pen.forward(50) pen.right(180-(360/branches)) pen.color("yellow") pen.penup() pen.goto(0, height*50) pen.pendown() for k in range(5): pen.forward(20) pen.right(144) ``` 最后,我们可以调用这个函数,像这样: ```python draw_tree(4, 4) ``` 这将创建一个高4个级别的圣诞树,每个级别有4个分支,底部有一个小星星。可以根据需要更改参数来创建不同的形。 这就是使用Turtle创建简单圣诞树的过程。素材提供了丰富的素材,大家可以自己尝试去创造。 ### 回答3: Pythonturtle是一个非常有趣和有用的工具,它可以帮助我们学习编程和图形绘制。在这里,我们将使用turtle来绘制一个圣诞树。 首先,我们需要导入turtle。在Python中,我们可以使用如下命令导入它: ```python import turtle ``` 接下来,我们将使用turtle来绘制一个三角形,它将成为我们圣诞树的主体。我们可以使用turtle中的如下命令来绘制三角形: ```python turtle.forward(100) turtle.left(120) turtle.forward(100) turtle.left(120) turtle.forward(100) ``` 以上命令完成了一个等边三角形的绘制。我们可以根据需要调整三角形的大小和位置。 为了使三角形变成圣诞树,我们需要给它添加干和装饰品。我们可以使用turtle中的如下命令来绘制干: ```python turtle.right(90) turtle.forward(50) turtle.right(90) turtle.forward(20) turtle.right(90) turtle.forward(50) ``` 以上命令完成了一个垂直向下的干的绘制。我们可以根据需要调整干的大小和位置。 接下来,我们可以用如下命令在三角形上添加一些圆形的装饰品: ```python turtle.penup() turtle.goto(-30, 70) turtle.pendown() turtle.circle(10) turtle.penup() turtle.goto(0, 80) turtle.pendown() turtle.circle(10) turtle.penup() turtle.goto(30, 70) turtle.pendown() turtle.circle(10) ``` 以上命令在三角形上方添加了三个圆形,表示圣诞树上的装饰品。我们可以根据需要添加更多的装饰品。 最后,我们可以使用如下命令调整一些设置,使得整个图形更加美观和逼真: ```python turtle.color('green') turtle.fillcolor('green') turtle.begin_fill() turtle.end_fill() turtle.hideturtle() turtle.done() ``` 以上命令调整了颜色和填充,并使用如下命令隐藏了turtle图标并显示绘制结果: ```python turtle.hideturtle() turtle.done() ``` 综上,以上代码实现了一个简单的圣诞树图形的绘制,但这只是一个基础的版本,你可以根据需要升级和优化代码,以获得更加复杂和逼真的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值