保健励志美文体育育儿作文
投稿投诉
作文动态
热点娱乐
育儿情感
教程科技
体育养生
教案探索
美文旅游
财经日志
励志范文
论文时尚
保健游戏
护肤业界

python实现超级玛丽小游戏(动图演示源码分享)

  下面给大家带来python实现超级玛丽小游戏的源码分享
  效果演示:
  基础源码
  1。基础设置(tools部分)
  2。设置背景音乐以及场景中的文字(setup部分)
  3。设置游戏规则(loadscreen)
  4。设置游戏内菜单等(mainmenu)
  5。main()
  6。调用以上函数实现
  1。基础设置(tools部分)
  这个部分设置马里奥以及游戏中蘑菇等怪的的移动设置。importosimportpygameaspgkeybinding{action:pg。Ks,jump:pg。Ka,left:pg。KLEFT,right:pg。KRIGHT,down:pg。KDOWN}classControl(object):Controlclassforentireproject。Containsthegameloop,andcontainstheeventloopwhichpasseseventstoStatesasneeded。Logicforflippingstatesisalsofoundhere。definit(self,caption):self。screenpg。display。getsurface()self。doneFalseself。clockpg。time。Clock()self。captioncaptionself。fps60self。showfpsFalseself。currenttime0。0self。keyspg。key。getpressed()self。statedict{}self。statenameNoneself。stateNonedefsetupstates(self,statedict,startstate):self。statedictstatedictself。statenamestartstateself。stateself。statedict〔self。statename〕defupdate(self):self。currenttimepg。time。getticks()ifself。state。quit:self。doneTrueelifself。state。done:self。flipstate()self。state。update(self。screen,self。keys,self。currenttime)defflipstate(self):previous,self。statenameself。statename,self。state。nextpersistself。state。cleanup()self。stateself。statedict〔self。statename〕self。state。startup(self。currenttime,persist)self。state。previouspreviousdefeventloop(self):foreventinpg。event。get():ifevent。typepg。QUIT:self。doneTrueelifevent。typepg。KEYDOWN:self。keyspg。key。getpressed()self。toggleshowfps(event。key)elifevent。typepg。KEYUP:self。keyspg。key。getpressed()self。state。getevent(event)deftoggleshowfps(self,key):ifkeypg。KF5:self。showfpsnotself。showfpsifnotself。showfps:pg。display。setcaption(self。caption)defmain(self):Mainloopforentireprogramwhilenotself。done:self。eventloop()self。update()pg。display。update()self。clock。tick(self。fps)ifself。showfps:fpsself。clock。getfps()withfps{}{:。2f}FPS。format(self。caption,fps)pg。display。setcaption(withfps)classState(object):definit(self):self。starttime0。0self。currenttime0。0self。doneFalseself。quitFalseself。nextNoneself。previousNoneself。persist{}defgetevent(self,event):passdefstartup(self,currenttime,persistant):self。persistpersistantself。starttimecurrenttimedefcleanup(self):self。doneFalsereturnself。persistdefupdate(self,surface,keys,currenttime):passdefloadallgfx(directory,colorkey(255,0,255),accept(。png,jpg,bmp)):graphics{}forpicinos。listdir(directory):name,extos。path。splitext(pic)ifext。lower()inaccept:imgpg。image。load(os。path。join(directory,pic))ifimg。getalpha():imgimg。convertalpha()else:imgimg。convert()img。setcolorkey(colorkey)graphics〔name〕imgreturngraphicsdefloadallmusic(directory,accept(。wav,。mp3,。ogg,。mdi)):songs{}forsonginos。listdir(directory):name,extos。path。splitext(song)ifext。lower()inaccept:songs〔name〕os。path。join(directory,song)returnsongsdefloadallfonts(directory,accept(。ttf)):returnloadallmusic(directory,accept)defloadallsfx(directory,accept(。wav,。mpe,。ogg,。mdi)):effects{}forfxinos。listdir(directory):name,extos。path。splitext(fx)ifext。lower()inaccept:effects〔name〕pg。mixer。Sound(os。path。join(directory,fx))returneffects2。设置背景音乐以及场景中的文字(setup部分)
  该部分主要设置场景中的背景音乐,以及字体的显示等设置。importosimportpygameaspgfrom。importtoolsfrom。importconstantsascORIGINALCAPTIONc。ORIGINALCAPTIONos。environ〔SDLVIDEOCENTERED〕1pg。init()pg。event。setallowed(〔pg。KEYDOWN,pg。KEYUP,pg。QUIT〕)pg。display。setcaption(c。ORIGINALCAPTION)SCREENpg。display。setmode(c。SCREENSIZE)SCREENRECTSCREEN。getrect()FONTStools。loadallfonts(os。path。join(resources,fonts))MUSICtools。loadallmusic(os。path。join(resources,music))GFXtools。loadallgfx(os。path。join(resources,graphics))SFXtools。loadallsfx(os。path。join(resources,sound))3。设置游戏规则(loadscreen)from。。importsetup,toolsfrom。。importconstantsascfrom。。importgamesoundfrom。。componentsimportinfoclassLoadScreen(tools。State):definit(self):tools。State。init(self)defstartup(self,currenttime,persist):self。starttimecurrenttimeself。persistpersistself。gameinfoself。persistself。nextself。setnextstate()infostateself。setoverheadinfostate()self。overheadinfoinfo。OverheadInfo(self。gameinfo,infostate)self。soundmanagergamesound。Sound(self。overheadinfo)defsetnextstate(self):Setsthenextstatereturnc。LEVEL1defsetoverheadinfostate(self):setsthestatetosendtotheoverheadinfoobjectreturnc。LOADSCREENdefupdate(self,surface,keys,currenttime):Updatestheloadingscreenif(currenttimeself。starttime)2400:surface。fill(c。BLACK)self。overheadinfo。update(self。gameinfo)self。overheadinfo。draw(surface)elif(currenttimeself。starttime)2600:surface。fill(c。BLACK)elif(currenttimeself。starttime)2635:surface。fill((106,150,252))else:self。doneTrueclassGameOver(LoadScreen):AloadingscreenwithGameOverdefinit(self):super(GameOver,self)。init()defsetnextstate(self):Setsnextstatereturnc。MAINMENUdefsetoverheadinfostate(self):setsthestatetosendtotheoverheadinfoobjectreturnc。GAMEOVERdefupdate(self,surface,keys,currenttime):self。currenttimecurrenttimeself。soundmanager。update(self。persist,None)if(self。currenttimeself。starttime)7000:surface。fill(c。BLACK)self。overheadinfo。update(self。gameinfo)self。overheadinfo。draw(surface)elif(self。currenttimeself。starttime)7200:surface。fill(c。BLACK)elif(self。currenttimeself。starttime)7235:surface。fill((106,150,252))else:self。doneTrueclassTimeOut(LoadScreen):LoadingScreenwithTimeOutdefinit(self):super(TimeOut,self)。init()defsetnextstate(self):Setsnextstateifself。persist〔c。LIVES〕0:returnc。GAMEOVERelse:returnc。LOADSCREENdefsetoverheadinfostate(self):Setsthestatetosendtotheoverheadinfoobjectreturnc。TIMEOUTdefupdate(self,surface,keys,currenttime):self。currenttimecurrenttimeif(self。currenttimeself。starttime)2400:surface。fill(c。BLACK)self。overheadinfo。update(self。gameinfo)self。overheadinfo。draw(surface)else:self。doneTrue4。设置游戏内菜单等(mainmenu)importpygameaspgfrom。。importsetup,toolsfrom。。importconstantsascfrom。。componentsimportinfo,marioclassMenu(tools。State):definit(self):Initializesthestatetools。State。init(self)persist{c。COINTOTAL:0,c。SCORE:0,c。LIVES:3,c。TOPSCORE:0,c。CURRENTTIME:0。0,c。LEVELSTATE:None,c。CAMERASTARTX:0,c。MARIODEAD:False}self。startup(0。0,persist)defstartup(self,currenttime,persist):Calledeverytimethegamesstatebecomesthisone。Initializescertainvaluesself。nextc。LOADSCREENself。persistpersistself。gameinfopersistself。overheadinfoinfo。OverheadInfo(self。gameinfo,c。MAINMENU)self。spritesheetsetup。GFX〔titlescreen〕self。setupbackground()self。setupmario()self。setupcursor()defsetupcursor(self):Createsthemushroomcursortoselect1or2playergameself。cursorpg。sprite。Sprite()dest(220,358)self。cursor。image,self。cursor。rectself。getimage(24,160,8,8,dest,setup。GFX〔itemobjects〕)self。cursor。statec。PLAYER1defsetupmario(self):PlacesMarioatthebeginningofthelevelself。mariomario。Mario()self。mario。rect。x110self。mario。rect。bottomc。GROUNDHEIGHTdefsetupbackground(self):Setupthebackgroundimagetoblitself。backgroundsetup。GFX〔level1〕self。backgroundrectself。background。getrect()self。backgroundpg。transform。scale(self。background,(int(self。backgroundrect。widthc。BACKGROUNDMULTIPLER),int(self。backgroundrect。heightc。BACKGROUNDMULTIPLER)))self。viewportsetup。SCREEN。getrect(bottomsetup。SCREENRECT。bottom)self。imagedict{}self。imagedict〔GAMENAMEBOX〕self。getimage(1,60,176,88,(170,100),setup。GFX〔titlescreen〕)defgetimage(self,x,y,width,height,dest,spritesheet):Returnsimagesandrectstoblitontothescreenimagepg。Surface(〔width,height〕)rectimage。getrect()image。blit(spritesheet,(0,0),(x,y,width,height))ifspritesheetsetup。GFX〔titlescreen〕:image。setcolorkey((255,0,220))imagepg。transform。scale(image,(int(rect。widthc。SIZEMULTIPLIER),int(rect。heightc。SIZEMULTIPLIER)))else:image。setcolorkey(c。BLACK)imagepg。transform。scale(image,(int(rect。width3),int(rect。height3)))rectimage。getrect()rect。xdest〔0〕rect。ydest〔1〕return(image,rect)defupdate(self,surface,keys,currenttime):Updatesthestateeveryrefreshself。currenttimecurrenttimeself。gameinfo〔c。CURRENTTIME〕self。currenttimeself。updatecursor(keys)self。overheadinfo。update(self。gameinfo)surface。blit(self。background,self。viewport,self。viewport)surface。blit(self。imagedict〔GAMENAMEBOX〕〔0〕,self。imagedict〔GAMENAMEBOX〕〔1〕)surface。blit(self。mario。image,self。mario。rect)surface。blit(self。cursor。image,self。cursor。rect)self。overheadinfo。draw(surface)defupdatecursor(self,keys):Updatethepositionofthecursorinputlist〔pg。KRETURN,pg。Ka,pg。Ks〕ifself。cursor。statec。PLAYER1:self。cursor。rect。y358ifkeys〔pg。KDOWN〕:self。cursor。statec。PLAYER2forinputininputlist:ifkeys〔input〕:self。resetgameinfo()self。doneTrueelifself。cursor。statec。PLAYER2:self。cursor。rect。y403ifkeys〔pg。KUP〕:self。cursor。statec。PLAYER1defresetgameinfo(self):ResetsthegameinfoincaseofaGameOverandrestartself。gameinfo〔c。COINTOTAL〕0self。gameinfo〔c。SCORE〕0self。gameinfo〔c。LIVES〕3self。gameinfo〔c。CURRENTTIME〕0。0self。gameinfo〔c。LEVELSTATE〕Noneself。persistself。gameinfo5。main()from。importsetup,toolsfrom。statesimportmainmenu,loadscreen,level1from。importconstantsascdefmain():Addstatestocontrolhere。runittools。Control(setup。ORIGINALCAPTION)statedict{c。MAINMENU:mainmenu。Menu(),c。LOADSCREEN:loadscreen。LoadScreen(),c。TIMEOUT:loadscreen。TimeOut(),c。GAMEOVER:loadscreen。GameOver(),c。LEVEL1:level1。Level1()}runit。setupstates(statedict,c。MAINMENU)runit。main()6。调用以上函数实现importsysimportpygameaspgfrom小ahrefhttps:www。q578。coml60targetblankclassinfotextkey游戏a。超级玛丽。data。mainimportmainimportcProfileifnamemain:main()pg。quit()sys。exit()

每日小记我的一天品学网专稿未经允许不得转载我今年面临着小升初的考验,小升初是人生第一道门槛,这个门槛预示着孩童的年代已经过去。我们开始告别孩童的年代,已经到了青少年时期。从现在开始……小学一年级雾霾的危害英语作文第一篇Thiskindofairpollutantcomefromavarietyofnaturalandmanmadesources。Naturalsourcescan……元旦见闻的作文中国传统节日介绍mdash;mdash;元旦中国的元旦,据传说起于三皇五帝之一的颛顼,距今已有3000多年的历史。ldquo;元旦rdquo;一词最早出现于《晋书》:ld……全国中小学生创新作文多年以后,面对火葬场上空蒸腾着的黑烟,殓妆师沈素将会回想起,她打扮的第一具尸体留在她手心的凉意。那天下午,她第一次走进火葬场。小镇报时的钟声在透彻的阳光里飘荡,一丝腐烂的……华为荣耀手机抗造真人真事,前几日有一天下班回家,骑上了共享单车,飞快地赶往大巴赶,当然戴着耳机听着喜马,手机在衣服的口袋里,口袋有点浅。路过国贸桥下西向东,绿灯后,疯狂往前骑,刚骑过10……忘不了那玫瑰似的笑作文微笑的面对人生,将回迎来一片美丽的色彩,装饰自己那乏味的生活。历经了一件又一件的事,但我怎么也忘不了那玫瑰似的笑。玫瑰那么美丽动人,他不比牡丹高贵,也不比芙蓉娇艳。它很平……投资数百亿?韩国SK集团与比尔盖茨合作建设新一代核电站据韩国《朝鲜日报》网站近日报道,韩国SK集团将向美国微软公司创始人比尔盖茨创立的新一代核能初创企业泰拉能源投资数百亿韩元,收购其10的股份。一直对电动汽车电池、氢气等清洁能源领……五年级上册数学日记怎么写日记是指用来记录其内容的载体,作为一种文体,属于记叙文性质的应用文。五年级上册数学日记怎么写,我们来看看。篇一:五年级上册数学日记怎么写以前,我一直认为学习求最小公倍数这……我们美丽的家园作文有一天半夜里,一只孤单的小鸟在我的睡梦里哭泣,它伤心得对我说:小姐姐,请你救救我吧,不要把我们美丽的家园毁掉。前几天,我的妈妈在一棵高大的树上,刚刚建好了房子,我们一家子高高兴……为了忘却的回忆500字作文人们常说,回忆是美好的、回忆是有味的,一切值得记住的东西在人们的心里是永远抹不掉的,既使夕阳西下了,也会在记忆中珍藏。回忆因年龄有关,要是让小孩子说说有什么值得回忆的事,……巴菲特再度抨击比特币不具生产力不会购买周璐璐张枕河中国证券报中证网中证网讯(记者周璐璐张枕河)在当地时间4月30日举行的2022年巴菲特股东大会上,巴菲特再度抨击比特币。巴菲特举例说,如果要取得假设总资产25……珍惜童年现在的一切,来之不易,而有些人却不懂得珍惜。越是有的越不懂得珍惜,得到失去时才会后悔。ldquo;珍惜rdquo;一个多么美的词汇,可是为什么有些人却就不懂得它的含义呢?……
享受日出五年级作文700字许多人都喜欢日出,它代表着美好一天的开端,奋斗新的一天,好心情的开始而日出跟日落,我恐怕会更赞赏日落一些,它虽说是代表着美好一天的结束,但它一天所留下的残余,会让你静静去……春天来了春天来了,我也重新开始了又是一年春天花开了草绿了小河流动了我也重新开始了mdash;mdash;那是一年烦恼一年忧愁一年伤心一年哭泣一年仇恨都翻过去了我擦干了泪扬起……琴弦振动频率怎么调?张朝阳的物理课求解波动方程和计算引力结合4月10日12时,《张朝阳的物理课》第四十四期开播,搜狐创始人、董事局主席兼CEO张朝阳坐镇搜狐视频直播间,带网友们复习并拓展了上一次线下直播课的内容,求解琴弦波动方程,并根据……小鹏汽车回应辅助驾驶失灵需观察周围环境4月11日,针对车主称小鹏汽车自动辅助驾驶失灵的情况,小鹏汽车方面回应称,经查,3月13日,车主邓先生驾驶小鹏P7于当地高速路段与前方横停的侧翻车辆发生碰撞,事故未导致人伤。初……小学四年级优秀作文观鱼这里的鱼,是一道美丽的风景,它们万紫千红,千姿百态,赢得了游客的一声声赞叹。早晨,阳光送了一件金衣给了大湖,我来到湖边,只见一层早雾飘在湖上方,在阳光的照耀下,慢慢消散,……学会珍藏的记忆作文600字初中导语:也许是上帝在创造了许多聪明的女孩之后疲倦了,就在不知不觉中创造出了傻傻的我。下面是小编整理的一些关于初二的优秀作文,欢迎查阅,谢谢!【篇一】逝去的如水一般的时光,留……五一劳动记叙事作文这个五一节因为爸爸妈妈工作忙,而且家中正在造房,所以,想要出去游玩的梦想支离破碎了,我在家中百般无聊,看着那些远远望去有一层白灰的家具,我的脑袋中立即冒出了一个点子,就是打扫房……高一暑假日记200字左右篇一今天,我去了青岛。那里有小熊猫,有水母,有企鹅,有海狮,有小丑鱼,有鳄鱼,有乌龟,有海马,有海蟹,我还有点害怕那!我们还去了水乐方。水乐方是个好地方,我先玩了水……关于写元旦的作文900字三篇在日常学习、工作和生活中,大家都经常接触到作文吧,借助作文人们可以实现文化交流的目的。那么你有了解过作文吗?下面是小编精心整理的写元旦的作文900字3篇,希望对大家有所帮助。写……关于现代儿童交友对象的调查俗话说:ldquo;近朱者赤,近墨者黑。rdquo;,如果交错了朋友,后果可想而知,轻者让自己成绩下降,重者将会引你误入歧途,毁掉你一生。因此,交往对象的重要性可想而知,那么现……iOS16新功能爆料苹果将改进专注模式彭博社记者MarkGurman本周报道,iOS16将对通知系统进行更新,苹果最新的测试版软件中的代码证实了这一点。正如Gurman所描述的,iOS16将具有相当显著的全面……三个小伙伴小鹿阿姨生病住进了医院。小猪、小熊和小猫这三个小伙伴约好第二天上午9点一同去看她。第二天,小熊早早的就赶到了医院,见小猪、小猫还没有到,他只好站在医院门口等。小猪一……
友情链接:易事利快生活快传网聚热点七猫云快好知快百科中准网快好找文好找中准网快软网