turtle的ondrag与类的继承练习Durtle类

"""本程序新建Durtle类,生成的拖动角色会换一个造型,松开它会隐藏"""


from turtle import *

class Durtle(Turtle):
    def __init__(self,images):
        Turtle.__init__(self,visible=False)
        self.begin_drag = 0                  # 拖的标志
        self.penup()
        self.images = images                 # 造型列表
        self.index = 0                       # 初始造型索引
        self.amounts = len(images)           # 造型数量

        "设定初始造型"
        self.image = self.images[self.index] # 当前造型
        self.shape(self.image)               # 设定造型
        self.st()                            # 显示            
         
        self.onrelease(self.act2)
        
    def alt_costume(self):
        "切换到下一个造型"
        if self.begin_drag == 0:
            self.index = self.index + 1
            self.index = self.index % self.amounts
            self.image = self.images[self.index]
            self.shape(self.image)
        self.begin_drag += 1        
                
    def act1(self,x,y):
        """切换到下一个造型,定位到鼠标指针坐标"""
        self.alt_costume()
        self.goto(x,y)  
        

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