保卫公主行动之Princess类

"""
   princess.py
   本模块是作品《保卫公主行动》的公主类。公主实例化后会按路径移动。
   路径是随机的,由另一个模块产生。
"""

import time
from turtle import *

class Princess(Turtle):
    counter = 0
    def __init__(self,image,cors):
        """image:造型,cors:路径上所有坐标点"""
        Turtle.__init__(self,visible=False)
        self.shape(image)
        self.penup()
        self.index = 0           # 坐标点索引号
        self.cors = cors
        self.amounts = len(cors) # 坐标数量
        self.goto(cors[0])       # 定位
        self.st()                # 显示
        self.move()

    def move(self):
        """沿着路径移动"""
        # 如果超时则移动  
        if self.index < self.amounts:
            self.goto(cors[self.index])                
            self.index = self.index + 1
            self.start_time = time.time()
            self.screen.ontimer(self.move,100)
        else:
            self.ht()
            Princess.counter += 1
                
            
if __name__ == "__main__":

    screen = Screen()
    screen.bgcolor("black")
    screen.title("公主类测试程序")
    cors = [(-133, 40), (-130, 42), (-127, 43), (-125, 44), (-122, 45),
            (-119, 46), (-116, 47), (-114, 49), (-111, 50), (-108, 51),
            (-105, 52), (-103, 53), (-100, 54), (-97, 56), (-94, 57),
            (-92, 58), (-89, 59), (-86, 59), (-83, 59), (-80, 60), (-77, 60),
            (-74, 60), (-71, 60), (-68, 61), (-65, 61), (-62, 61), (-59, 61),
            (-56, 62), (-54, 61), (-52, 59), (-49, 57), (-47, 56), (-45, 54),
            (-42, 52), (-40, 50), (-38, 48), (-35, 46), (-33, 45), (-30, 43),
            (-28, 41), (-26, 39), (-23, 37), (-21, 35), (-19, 33), (-17, 32),
            (-14, 31), (-12, 30), (-9, 29), (-6, 27), (-3, 26), (-1, 25),
            (1, 24), (4, 22), (7, 21), (9, 20), (12, 18), (15, 17), (17, 16),
            (21, 15), (24, 14), (27, 14), (30, 13), (33, 13), (36, 13), (39, 12),
            (42, 12), (45, 12), (48, 11), (51, 11), (54, 11), (57, 10), (60, 10),
            (63, 9), (66, 9), (69, 9), (72, 8), (75, 8), (78, 7), (81, 7), (84, 7),
            (87, 6), (90, 6), (93, 5), (95, 5), (98, 4), (101, 4), (104, 4), (107, 3),
            (110, 3), (113, 3), (116, 3), (119, 2), (122, 2), (125, 2), (128, 2),
            (131, 2), (134, 2), (137, 1)]
    image = "princess.gif"
    screen.addshape(image)

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