python使用turtle步骤_Python+turtle交互式绘图:可以用鼠标拖动的小海龟

下面是我画海龟的过程:

(1)打开python:

1621981-20190313172634236-1396286027.png

2、调用turtle库,先设置一些基础设置,与一个画龟的函数

from turtle import Screen,Turtle,mainloop

class ColorTurtle(Turtle):

def __init__(self,x,y):

Turtle.__init__(self)

self.shape("turtle")

self.resizemode("usre")

self.shapesize(3,3,0)

self.pensize(10)

self._color=[0,0,0]

self.x=x

self._color[x]=y

self.color(self._color)

self.speed(0)

self.left(90)

self.up()

self.goto(x,0)

self.down()

self.sety(1)

self.up()

self.sety(y)

self.pencolor("black")

self.ondrag(self.shift)

3、设计shift函数

defshift(self,x,y):

self.sety(max(o,min(y,1)))

self._color[self.x]=self.ycor()

self.fillcolor(self._color)

setbgcolor()

4、背景颜色函数

defsetbgcolor():

screen.bgcolor(red.ycor(),green.ycor(),blue.ycor())

5、主函数

defmain():globalscreen,red,green,blue

screen=Screen()

screen.delay(0)

screen.setworldcoordinates(-1,-0.3,3,1.3)

red=ColorTurtle(0,.7)

green=ColorTurtle(1,.3)

blue=ColorTurtle(2,.6)

setbgcolor()

writer=Turtle()

writer.hideturtle()

writer.up()

writer.goto(1,1.15)

write.write("DRAG",align="center",font=("Arial",30,("bold","italic")))

if __name__=="__main__":

main()

mainloop()

6、效果如下:

1621981-20190313182008245-1677709968.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值