下面是部分代码预览:
'''Python模拟海底世界章鱼哥_向面对象 .py 本程序采用面向对象方法编程,新建了Bubble类,Octopus类等,栩栩如生地展现了可爱的章鱼吐泡泡和左右摇摆的形态. 本用也有面向过程的同样效果的程序,还有一个用arcade模块制作的同样效果的程序. 这个版本是有鱼和螃蟹的. ''' import math,time from turtle import Turtle,Screen # 导入Turtle类和Screen命令 from random import randint def follow_mouse(event): """本函数让小海龟面朝鼠标指针移动 由于海龟画图的坐标和tkinter画布原生坐标不一样,所以要进行坐标转换。 """ x = event.x - width//2 # 转换成海龟坐标系中的x坐标 y = height//2 - event.y # 转换成海龟坐标系中的y坐标 class Bubble(Turtle): def __init__(self,images,master,delaytime): Turtle.__init__(self,visible=False) self.speed(0) # 动作速度为最快 self.penup() self.images = images # 造型列表 self.amounts = len(images) # 造型数量 self.master = master # 它的主人是章鱼 self.delaytime = delaytime # 延迟总时间 self.delaycounter = 0 # 延时出现计时器 self.screen_height = self.screen.window_height() self.wait_until_time_up() # 异步等待,时间到了出现泡泡 调用self.rise def wait_until_time_up(self): """等到时间到了就显示""" if self.delaycounter < self.delaytime: self.delaycounter += 1 self.screen.ontimer(self.wait_until_time_up,1000) # 1秒后再次运行 else: # 超时就显示出来 self.init_costume() # 初始化造型等 self.rise() # 泡泡上升 def init_costume(self): """初始化造型""" self.ht() # 隐藏 def rise(self): """泡泡上升""" def alt_costume(self): """切换造型,让泡泡变大,为了不让它一下子变大,加了interval""" def animate_screen(): """动态背景,由于要改变bg_index的值,所以申明为全局""" global bg_index screen.bgpic(waveimages[bg_index]) # 切换背景 bg_index = bg_index + 1 # 背景图像编号加1 class Octopus(Turtle): def __init__(self,images): Turtle.__init__(self,visible=False) self.images = images self.penup() self.speed(0) self.costume_counter = 0 # 造型计数器,相当于自变量 self.costume_index = 0 # 造型索引号 def animate(self): """切换造型,此处用到了 80 - abs(self.costume_counter%160 - 80) 算术表达式 它的costume_counter相当于自变量,能让造型编号从0增加到80,然后又下降直到0。 值变化为0,1,2,3,4,...80,79,78,...0,1,2,3....周期性为160。 """ class Fish(Turtle): def __init__(self,images_right,images_left,enemy): Turtle.__init__(self,visible=False) self.images_right = images_right # 右造型列表 self.images_left = images_left # 左造型列表 self.images_list = [images_right,images_left] # 造型列表 def wait_until_time_up(self): """每条鱼的出现时间不一样""" if self.delaycounter < self.delaytime: self.delaycounter += 1 self.screen.ontimer(self.wait_until_time_up,1000) # 1秒后再次运行 else: # 超时就显示出来 self.move() # 移动 self.animate() # 换造型 self.st() # 显示 def set_enemy_dead_rect(self): """章鱼脚大概的矩形范围""" self.enemy_left = self.enemy.xcor() - 65 self.enemy_right = self.enemy.xcor() + 65 self.enemy_top = self.enemy.ycor() self.enemy_bottom = self.enemy.ycor() -125 def move(self): if not self.dead: self.fd(10) self.bounce_on_edge() # 碰到边缘就反弹 self.bump_enemy_check() # 碰敌人检测 self.screen.ontimer(self.move,100) def bump_enemy_check(self): """碰敌人检测""" self.set_enemy_dead_rect() def bounce_on_edge(self): if abs(self.xcor()) >= self.screen_width//2: self.right(180) self.set_costume_list() def set_costume_list(self): """根据方向设置造型列表,有0和180两个方向""" self.images_index = int(self.heading() // 180) # 根据方向选择用左或右系列造型 self.images = self.images_list[self.images_index] # 当前的造型列表 def animate(self): if not self.dead: image = self.images[self.costume_index] # 取图像 class Crab(Turtle): def __init__(self,images): Turtle.__init__(self,visible=False) self.images = images # 造型列表 self.setheading(randint(0,1)*180) # 初始方向,向左或向右 self.penup() self.speed(0) self.costume_index = 0 # 造型索引号 self.costume_amounts = len(images) # 造型数量 self.screen_width = self.screen.window_width() # 获取屏幕宽 self.screen_height = self.screen.window_height() # 获取屏幕高度 def wait_until_time_up(self): """每个crab的出现时间不一样""" def move(self): """移动""" def random_turn_back(self): """设置一定的概率让它有时候反向""" def bounce_on_edge(self): """碰到左右边缘就反向""" def animate(self): """不定时切换造型""" if __name__ == "__main__": bg_index = 0 width,height = 500,685 pop_images = ["bubbles6/" + str(i) + ".gif" for i in range(20)] octopus_images = ["章鱼/" + str(i) + ".gif" for i in range(1,82)] fish_right_images = ["fishes/0.gif","fishes/1.gif","fishes/2.gif"] fish_left_images = ["fishes/0-left.gif","fishes/1-left.gif","fishes/2-left.gif"] crab_images = ["crabs/crab-a.gif","crabs/crab-b.gif"] waveimages = ["bg2/" + (4-len(str(i))) * "0" + str(i) + ".png" for i in range(1,16)] bg_amounts = len(waveimages) screen = Screen() screen.colormode(255) screen.title('章鱼哥') #写上窗口标题 screen.setup(width,height) #设定窗口大小 screen.delay(0) [screen.addshape(image) for image in pop_images] # 添加泡泡从小到大造型 [screen.addshape(image) for image in octopus_images] # 添加章鱼的所有造型 [screen.addshape(image) for image in fish_right_images]# 添加鱼的右造型表 [screen.addshape(image) for image in fish_left_images] # 添加鱼的左造型表 [screen.addshape(image) for image in crab_images] # 添加螃蟹的造型表 animate_screen() #Crab(crab_images) #Crab(crab_images) octopus = Octopus(octopus_images) [ Bubble(pop_images,octopus,delaytime) for delaytime in range(1,6) ] #[ Fish(fish_right_images,fish_left_images,octopus) for i in range(10) ] screen.cv.bind("<Motion>",follow_mouse) #画布绑定鼠标移动事件 screen.mainloop()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)