学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import * class SpriteSheet: def __init__(self, arr, config): self.isPlaying = True self.isFlag = False self.currentFrame = 0 self.totalFrames = len(arr) self.stamps = [] self.config = config for i in range(len(arr)): sp = stamp(arr[i]) self.stamps.append(sp) sp.hide() def play(self): self.isPlaying = True def stop(self): self.isPlaying = False def gotoAndStop(self, n): self.currentFrame = n; self.stop() def gotoAndPlay(self, n): self.currentFrame = n; self.play() def nextFrame(self): self.currentFrame = self.currentFrame + 1 if self.isFlag: if self.currentFrame > self.flagEnd: self.currentFrame = self.flagStart elif self.currentFrame >= self.totalFrames: self.currentFrame = 0 def playFlag(self, flag): if flag in self.config: a = self.config[flag] self.flagStart = a[0] self.flagEnd = a[1] self.isFlag = True self.gotoAndPlay(self.flagStart); def clearPlayFlag(self): self.isFlag = False def update(self): if self.isPlaying: self.nextFrame() for i in range(len(self.stamps)): self.stamps[i].hide() if self.totalFrames > 0: self.stamps[self.currentFrame].show() hero = SpriteSheet( [ 'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782884_441334.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782789_777162.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782868_528853.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782879_421421.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782894_144746.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782864_319894.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782867_785734.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782864_084225.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782894_974575.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782845_061008.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782804_792339.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782871_063850.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782827_010127.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782783_055114.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782783_165885.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782845_347436.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782845_099353.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782866_974514.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782789_491249.png' ,'https://code-file.xdf.cn/files/dingdangweb/20230606/1686017782864_583559.png' ], { 'stand': [0, 4], 'run': [5, 10], 'jump': [11, 13], 'jump2': [14, 19] } ) def updateN(n): global hero, actions, tf2 hero.playFlag(actions[n]) tf2.change(actions[n]) def tap(): global n n = n + 1 if n == len(actions): n = 0 updateN(n) def loop(): hero.update() text("点击切换动画", 100, 100, 50, 'red') tf2 = text("", 100, 200, 50, 'red') n = 0 actions = ['stand', 'run', 'jump', 'jump2'] updateN(0)
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。