python第8周项目

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#欢天喜地接元宝游戏importsimpleguitkassimpleguiimportmathimportrandomimporttime#定义全局变量WIDTH=800#画布宽度HEIGHT=600#画布高度score=0#游戏得分time=120#游戏限定时间(剩余时间)lives=3#游戏机会次数(财神的命)started=False#游戏是否开始over=False#游戏状态(是否结束)wealthgod=None#财神对象falling_group=set([])#所有天空坠物对象的集合#定义图片信息类classImageInfo:def__init__(self,center,size,radius=0,id=0,lifespan=None,animated=False):self.center=centerself.size=sizeself.radius=radiusself.id=idiflifespan:self.lifespan=lifespanelse:self.lifespan=float('inf')self.animated=animateddefget_center(self):returnself.centerdefget_size(self):returnself.sizedefget_radius(self):returnself.radiusdefget_id(self):returnself.iddefget_lifespan(self):returnself.lifespandefget_animated(self):returnself.animated#定义财神类classWealthgod:def__init__(self,pos,image,info):self.pos=[pos[0],pos[1]]self.image=imageself.image_center=info.get_center()self.image_size=info.get_size()self.radius=info.get_radius()defget_position(self):returnself.posdefget_radius(self):returnself.radius#向左平移defmoveLeft(self):ifself.pos[0]-self.image_center[0]=0:self.pos[0]=self.image_center[0]else:self.pos[0]-=10#向右平移defmoveRight(self):ifself.pos[0]+self.image_center[0]=WIDTH:self.pos[0]=WIDTH-self.image_center[0]else:self.pos[0]+=10defdraw(self,canvas):canvas.draw_image(self.image,self.image_center,self.image_size,self.pos,self.image_size,0)#定义坠落物类classFallings:def__init__(self,pos,image,info,sound=None):self.pos=[pos[0],pos[1]]self.image=imageself.image_center=info.get_center()self.image_size=info.get_size()self.radius=info.get_radius()self.id=info.get_id()self.lifespan=info.get_lifespan()self.animated=info.get_animated()self.age=0ifsound:sound.rewind()sound.play()defget_position(self):returnself.posdefget_radius(self):returnself.radiusdefget_id(self):returnself.id#坠物碰撞到财神defcollide(self,other_object):ifdist(self.get_position(),other_object.get_position())=self.radius+other_object.get_radius():returnTrueelse:returnFalsedefdraw(self,canvas):canvas.draw_image(self.image,self.image_center,self.image_size,self.pos,self.image_size,0)defupdate(self):self.pos[1]=(self.pos[1]+1)%HEIGHTself.age+=1ifself.age=self.lifespan:returnTrueelse:returnFalse#图片、声音资源#开始背景start,游戏背景background,财神wealthgod,钻石diamond,元宝ingot,铜钱copper,恶狗dog,炸弹bomb,开始游戏按钮startbutton#背景音乐sound1start_info=ImageInfo([400,300],[800,600])start_image=simplegui.load_image()#开始背景background_info=ImageInfo([195,110],[391,220])background_image=simplegui.load_image()#游戏背景wealthgod_info=ImageInfo([56.66,72.5],[113.33,145],20,0,True,3)wealthgod_image=simplegui.load_image()#财神,图像文件中包含3副图片用来实现动画diamond_info=ImageInfo([25,25],[50,50],20,1)diamond_image=simplegui.load_image()#钻石ingot_info=ImageInfo([25,25],[50,50],20,2)ingot_image=simplegui.load_image()#元宝copper_info=ImageInfo([25,25],[50,50],20,3)copper_image=simplegui.load_image()#铜钱dog_info=ImageInfo([25,25],[50,50],20,4)dog_image=simplegui.load_image()#恶狗bomb_info=ImageInfo([25,25],[50,50],20,5)bomb_image=simplegui.load_image()#炸弹startButton_info=ImageInfo([114,50],[228,99])startButton_image=simplegui.load_image()#游戏开始按钮end_info=ImageInfo([400,300],[800,600])end_image=simplegui.load_image()#游戏结束按钮collision_info=ImageInfo([55,43],[110,86])collision_image=simplegui.load_image()#炸弹爆炸效果blood_info=ImageInfo([85.5,85.5],[171,171])blood_image=simplegui.load_image()#被恶狗咬后的溅血效果#加载音效资源back_sound=simplegui.load_sound()#背景音效explode_sound=simplegui.load_sound()#爆炸音效money_sound=simplegui.load_sound()#捡到元宝或钱的音效dog_sound=simplegui.load_sound()#定义函数defdist(p,q):returnmath.sqrt((p[0]-q[0])**2+(p[1]-q[1])**2)#游戏初始化辅助函数(游戏运行时出现的第一个界面,需要单击“开始游戏”才可以开始游戏,同时也是“再来一局”按钮处理函数)definit():#设置游戏状态、得分、游戏限定时间、游戏机会次数,禁用背景音乐globalstarted,over,score,lives,timestarted=Falseover=Falsescore=0lives=3time=120back_sound.pause()#游戏开始函数defgamestart():#设置游戏状态、得分、游戏机会次数、播放背景音乐,并在屏幕底部中间位置出现财神globalstarted,over,score,lives,wealthgod,falling_groupstarted=Trueover=Falsetimer.start()back_sound.rewind()back_sound.play()wealthgod=Wealthgod([WIDTH/2,HEIGHT-wealthgod_info.get_size()[0]/2],wealthgod_image,wealthgod_info)#游戏结束函数defgameover():#设置游戏状态、得分、游戏机会次数globalstarted,over,timestarted=Falseover=Truetime=0#天空坠物随机出现函数,用来产生天空坠物的时间处理函数deffalling_sp

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功