下面是我画海龟的过程:

(1)打开python:

Python+turtle交互式绘图:可以用鼠标拖动的小海龟-LMLPHP

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函数

def shift(self,x,y):
self.sety(max(o,min(y,1)))
self._color[self.x]=self.ycor()
self.fillcolor(self._color)
setbgcolor()

4、背景颜色函数

def setbgcolor():
screen.bgcolor(red.ycor(),green.ycor(),blue.ycor())

5、主函数

def main():
global screen,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、效果如下:

Python+turtle交互式绘图:可以用鼠标拖动的小海龟-LMLPHP

05-02 12:24