python_turtlr,,Python tur


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 turtledef 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 turtleturtle.setup(1000,1000,0,0)size=20turtle.pensize(size)turtle.color("red")length=200turtle.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:#获取位置的绝对值        breakend_fill()
技术分享图片

技术分享图片

技术分享图片
import turtleturtle.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         break10 end_fill()
技术分享图片

技术分享图片更改旋转角度,得到漂亮的花

#最简单的圣诞树1height = 523stars = 14for i in range(height):5    print((‘ ‘ * (height - i)) + (‘*‘ * stars))6    stars += 27print((‘ ‘ * height) + ‘|‘)







#turtlr方法一
1importturtle
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
17foriinrange(1,17):
18y=30*i
19forjinrange(i-k):
20x=30*j
21square.goto(x,-y+280)
22square.stamp()
23square.goto(-x,-y+280)
24square.stamp()
25ifi%4==0:
26x=30*(j+1)
27circle.color(‘red‘)
28circle.goto(-x,-y+280)
29circle.stamp()
30circle.goto(x,-y+280)
31circle.stamp()
32k+=2
33ifi%4==3:
34x=30*(j+1)
35circle.color(‘yellow‘)
36circle.goto(-x,-y+280)
37circle.stamp()
38circle.goto(x,-y+280)
39circle.stamp()
40square.color(‘brown‘)
41foriinrange(17,20):
42y=30*i
43forjinrange(3):
44x=30*j
45square.goto(x,-y+280)
46square.stamp()
47square.goto(-x,-y+280)
48square.stamp()
49turtle.exitonclick()
技术分享图片
#turtle方法21from turtle import * 2import random 3import time 4 5n = 80.0 6 7speed("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)25def tree(d, s):26    if d <= 0: return27    forward(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)34    backward(s)35tree(15, n)36backward(n/2)3738for i in range(200):39    a = 200 - 400 * random.random()40    b = 10 - 20 * random.random()41    up()42    forward(b)43    left(90)44    forward(a)45    down()46    if random.randint(0, 1) == 0:47            color(‘tomato‘)48    else:49        color(‘wheat‘)50    circle(2)51    up()52    backward(a)53    right(90)54    backward(b)55time.sleep(60)
View Code

python_turtlr

评论关闭