下面是我畫海龜的過程:
(1)打開python:
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、效果如下: