本人以前写的一个类,是一只小猫边走路边切换造型。它并没有继承自海龟类,小猫其实是一只海龟,只是造型换成猫的了。代码如下所示:
"""小猫边走边换造型的类"""
from turtle import Turtle,Screen
class Cat:
def __init__(self,images,x,y):
self.images = images
self.index = 0
self.x = x
self.y = y
self.body = Turtle(shape = images[0])
self.body.penup()
def move(self,distance):
self.body.fd(distance)
self.next_costume()
def next_costume(self):
self.index = 1 - self.index
self.body.shape(self.images[self.index])
screen = Screen()
images = "cat1.gif","cat2.gif"
[screen.addshape(image) for image in images]
c1 = Cat(images,100,100)
for i in range(100):
c1.move(1)
相关图片:
本人撰写了大量原创的Python创意程代码,主要是基于turtle和pygame。CSDN这里只是传了极少一部分,打算上传到本人的独立域名博客。敬请关注! 需要订制作python创意动画游戏编程作品也可联系本人。