turtle库 使用

 turtle_sunflower

# coding=utf-8
import turtle, time
# 同时设置pencolor=color1, fillcolor=color2
turtle.pensize(5)
turtle.color("#ff8585", "#ff4646")
turtle.speed('fast')
turtle.begin_fill()
#To be called just before drawing a shape to be filled.
#要填充之前就必须调用
for _ in range(9):
    turtle.forward(200)
    turtle.left(160)
    #逆时针旋转
turtle.end_fill()
#Fill the shape drawn after the last call to begin_fill()
#填充结束之后调用

time.sleep(0.5)
  
turtle.penup()
turtle.goto(-150,-120)
turtle.color("#ffb396")
turtle.write("Done", font=('Arial', 40, 'normal'))

#turtle.bye()
#Shut the turtlegraphics window.
turtle.mainloop()
'''Starts event loop - calling Tkinter’s mainloop function. 
Must be the last statement in a turtle graphics program.
loop事件,必须放在程序的最后
'''

turtle_螺旋

'''
#普通螺旋
import turtle
turtle.speed('fastest')
for i in range(100): 
    turtle.forward(i)
    turtle.left(91)
turtle.exitonclick()
'''

#彩虹螺旋
import turtle
colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']
turtle.speed('fastest')
for i in range(120):
    turtle.pencolor(colors[i % 6])
    turtle.pensize(i/50 + 1)
    turtle.forward(i)
    turtle.left(60)
turtle.exitonclick()

 turtle_rose

#在这上面找了配色https://colorhunt.co/palettes/trendy
#绿松石玫瑰 turquoise rose
import turtle, time
turtle.speed('fastest')
turtle.pensize(3)
turtle.bgcolor("#ffda77")
turtle.title('turquoise rose')
turtle.textinput("Visitor", "Name of first visitor:")    #可以弹出一个对话框
turtle.numinput("Poker", "Your stakes:",
 1000, minval=10, maxval=10000)#弹出一个只能输入数字的对话框
# 设置初始位置------------------------
turtle.penup()  
turtle.left(90)  
#Turn turtle left by angle units. Angle orientation depends on the turtle mode
turtle.fd(200)  
#等于turtle.forward(200)
#Move the turtle forward by the specified distance, 
#in the direction the turtle is headed.
turtle.pendown()  
turtle.right(90)
# 花蕊-----------------------------
turtle.pencolor('#fcdada')
turtle.fillcolor("#ffa5a5")  #just Return or set the fillcolor.
turtle.begin_fill()  
turtle.circle(10,180)  
#turtle.circle(radius, extent=None, steps=None)
#半径,角度,步数(步数越多,越圆滑)
turtle.circle(25,110)  
turtle.left(50)  
turtle.circle(60,45)  
turtle.circle(20,170)  
turtle.right(24)
turtle.fd(30)  
turtle.left(10)  
turtle.circle(30,110)  
turtle.fd(20)  
turtle.left(40)  
turtle.circle(90,70)  
turtle.circle(30,150)  
turtle.right(30)  
turtle.fd(15)  
turtle.circle(80,90)  
turtle.left(15)
turtle.fd(45)
turtle.right(165)  
turtle.fd(20)  
turtle.left(155)  
turtle.circle(150,80)  
turtle.left(50)  
turtle.circle(150,90)  
turtle.end_fill()  
# 花瓣1-------------------------
turtle.left(150)  
turtle.circle(-90,70)  
turtle.left(20)  
turtle.circle(75,105)
turtle.setheading(60)
#Set the orientation of the turtle to to_angle
#这个就是直接指定方向了,不再是旋转角度
#0是东,90北,180西,270南
turtle.circle(80,98)  
turtle.circle(-90,40)  
# 花瓣2   ----------------
turtle.left(180)  
turtle.circle(90,40)  
turtle.circle(-80,98)  
turtle.setheading(-83)  
# 叶子1  -----------------
turtle.fd(30)  
turtle.left(90)  
turtle.fd(25)  
turtle.left(45)  
turtle.fillcolor("#5c969e")  
turtle.begin_fill()  #在一个包围开始之前,要begin_fill()
turtle.circle(-80,90)  
turtle.right(90)  
turtle.circle(-80,90)  
turtle.end_fill()  
turtle.right(135)  
turtle.fd(60)  
turtle.left(180)  
turtle.fd(85)  
turtle.left(90)  
turtle.fd(80)  
# 叶子2  
turtle.right(90)  
turtle.right(45)  
turtle.fillcolor("#3d7ea6")  
turtle.begin_fill()  
turtle.circle(80,90)  
turtle.left(90)  
turtle.circle(80,90)  
turtle.end_fill()  
turtle.left(135)  
turtle.fd(60)  
turtle.left(180)  
turtle.fd(60)  
turtle.right(90)  
turtle.circle(200,60) 
turtle.exitonclick()

 turtle_christmas

import turtle, time
screen = turtle.Screen()
screen.setup(1200,800)
circle = turtle.Turtle()
circle.shape('circle')
#但是找不到默认的半径
'''可以自己添加形状turtle.register_shape(name, shape=None),详情看说明书
    Set turtle shape to shape with given name or, 
    if name is not given, return name of current shape. 
    Shape with name must exist in the TurtleScreen’s shape dictionary. 
    Initially there are the following polygon shapes: “arrow”, 
    “turtle”, “circle”, “square”, “triangle”, “classic”.
'''
circle.color('#fa4252')
'''
    Return the current pencolor and the current fillcolor as a pair of color 
    specification strings or tuples as returned by pencolor() and fillcolor().
'''
circle.speed('fastest')
'''
    turtle.speed(speed=None)
    speed – an integer in the range 0..10 or a speedstring (see below)
    Set the turtle’s speed to an integer value in the range 0..10. If no argument is given, return current speed.
    If input is a number greater than 10 or smaller than 0.5, speed is set to 0. Speedstrings are mapped to speedvalues as follows:
    “fastest”: 0
    “fast”: 10
    “normal”: 6
    “slow”: 3
    “slowest”: 1
    Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.
    Attention: speed = 0 means that no animation takes place. 
    forward/back makes turtle jump and likewise left/right make 
    the turtle turn instantly.
'''
circle.up() #Pull the pen up – no drawing when moving.
square = turtle.Turtle()
square.shape('square')
square.color('#91bd3a')
square.speed('fastest')
square.up()

circle.goto(0,280)
#turtle的起始位置和pygame的不一样,turtle采用的是正常的坐标轴,(0,0)在正中央
#Move turtle to an absolute position. If the pen is down, 
#draw line. Do not change the turtle’s orientation.
circle.stamp()
#Stamp a copy of the turtle shape onto the canvas at the current
# turtle position. Return a stamp_id for that stamp,
# which can be used to delete it by calling clearstamp(stamp_id).
k = 0
for i in range(1,17):
    y = 30 * i
    for j in range(i-k):
        x = 30 * j
        square.goto(x, -y + 280)
        square.stamp()
        square.goto(-x, -y + 280)
        square.stamp()
    if i % 4 == 0:
        x = 30 * (j + 1)
        circle.color('#ffa259')
        circle.goto(-x, -y + 280)
        circle.stamp()
        circle.goto(x, -y + 280)
        circle.stamp()
        k += 2
    if i % 4 == 3:
        x = 30 * (j+1)
        circle.color('#fe6845')
        circle.goto(-x, -y + 280)
        circle.stamp()
        circle.goto(x, -y + 280)
        circle.stamp()
        #time.sleep(1)
square.color('#990000')
for i in range(17,21):
    y = 30 * i
    for j in range(3):
        x = 30 * j
        square.goto(x, -y + 280)
        square.stamp()
        square.goto(-x, -y + 280)
        square.stamp()
        #time.sleep(1)
turtle.write('merry christmas',True,'center',font = ('arial', 40, 'normal'))
'''
turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))
Write text - the string representation of arg - at the current 
turtle position according to align (“left”, “center” or right”) 
and with the given font. If move is true, the pen is moved to
 the bottom-right corner of the text. By default, move is False.
'''
turtle.exitonclick()
#turtle.exitonclick()不是很理解bind eye(),但效果就是点击才会退出
#Bind bye() method to mouse clicks on the Screen.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值