Python海龟画图旋转的彩色方块之类的继承教学源代码

Python海龟画图旋转的彩色方块之类的继承教学源代码作者李兴球

"""Block类,继承自Turtle类,实例化后会自动旋转。

请将以下的每一句写上注释,和文档字符串。本程序可用于类的继承之教学。


"""

from turtle import *
from random import randint

class Block(Turtle):
    
    def __init__(self,color):
        
        Turtle.__init__(self,shape='square')
        self.penup()
        self.sw  = self.screen.window_width()
        self.sh = self.screen.window_height()
        x = randint(-self.sw/2,self.sw/2)
        y = randint(-self.sh/2,self.sh/2)
        self.goto(x,y)
        self.color(color)        
        self.ondrag(self.goto)
        self.rotate()
        
    def rotate(self):
        
        self.right(10)
        self.screen.ontimer(self.rotate,10)

You must log in to read the rest of this article. Please log in or register as a user.