找回密码
 会员注册
查看: 23|回复: 0

Python版超级玛丽+源码-Python制作超级玛丽游戏

[复制链接]

5

主题

0

回帖

16

积分

新手上路

积分
16
发表于 2024-9-9 09:30:17 | 显示全部楼层 |阅读模式
小时候最喜欢玩的小游戏就是超级玛丽了,有刺激有又技巧,通关真的很难,救下小公主还被抓走了,唉,心累,最后还是硬着头皮继续闯,终于要通关了,之后再玩还是没有那么容易,哈哈,不知道现在能不能通关,今天就来简单实现一下。超级玛丽运行起来是这样的,可以简单作为试玩。绘制金币:classCoin(EntityBase):def__init__(self,screen,spriteCollection,x,y,gravity=0):super(Coin,self).__init__(x,y,gravity)self.screen=screenself.spriteCollection=spriteCollectionself.animation=copy(self.spriteCollection.get("coin").animation)self.type="Item"defupdate(self,cam):ifself.alive:self.animation.update()self.screen.blit(self.animation.image,(self.rect.x+cam.x,self.rect.y))12345678910111213绘制玛丽:spriteCollection=Sprites().spriteCollectionsmallAnimation=Animation([spriteCollection["mario_run1"].image,spriteCollection["mario_run2"].image,spriteCollection["mario_run3"].image,],spriteCollection["mario_idle"].image,spriteCollection["mario_jump"].image,)bigAnimation=Animation([spriteCollection["mario_big_run1"].image,spriteCollection["mario_big_run2"].image,spriteCollection["mario_big_run3"].image,],spriteCollection["mario_big_idle"].image,spriteCollection["mario_big_jump"].image,)classMario(EntityBase):def__init__(self,x,y,level,screen,dashboard,sound,gravity=0.8):super(Mario,self).__init__(x,y,gravity)self.camera=Camera(self.rect,self)self.sound=soundself.input=Input(self)self.inAir=Falseself.inJump=Falseself.powerUpState=0self.invincibilityFrames=0self.traits={"jumpTrait":JumpTrait(self),"goTrait":GoTrait(smallAnimation,screen,self.camera,self),"bounceTrait":bounceTrait(self),}self.levelObj=levelself.collision=Collider(self,level)self.screen=screenself.EntityCollider=EntityCollider(self)self.dashboard=dashboardself.restart=Falseself.pause=Falseself.pauseObj=Pause(screen,self,dashboard)defupdate(self):ifself.invincibilityFrames>0:self.invincibilityFrames-=1self.updateTraits()self.moveMario()self.camera.move()self.applyGravity()self.checkEntityCollision()self.input.checkForInput()defmoveMario(self):self.rect.y+=self.vel.yself.collision.checkY()self.rect.x+=self.vel.xself.collision.checkX()defcheckEntityCollision(self):forentinself.levelObj.entityList:collisionState=self.EntityCollider.check(ent)ifcollisionState.isColliding:ifent.type=="Item":self._onCollisionWithItem(ent)elifent.type=="Block":self._onCollisionWithBlock(ent)elifent.type=="Mob":self._onCollisionWithMob(ent,collisionState)def_onCollisionWithItem(self,item):self.levelObj.entityList.remove(item)self.dashboard.points+=100self.dashboard.coins+=1self.sound.play_sfx(self.sound.coin)def_onCollisionWithBlock(self,block):ifnotblock.triggered:self.dashboard.coins+=1self.sound.play_sfx(self.sound.bump)block.triggered=Truedef_onCollisionWithMob(self,mob,collisionState):ifisinstance(mob,RedMushroom)andmob.alive:self.powerup(1)self.killEntity(mob)self.sound.play_sfx(self.sound.powerup)elifcollisionState.isTopand(mob.aliveormob.bouncing):self.sound.play_sfx(self.sound.stomp)self.rect.bottom=mob.rect.topself.bounce()self.killEntity(mob)elifcollisionState.isTopandmob.aliveandnotmob.active:self.sound.play_sfx(self.sound.stomp)self.rect.bottom=mob.rect.topmob.timer=0self.bounce()mob.alive=FalseelifcollisionState.isCollidingandmob.aliveandnotmob.activeandnotmob.bouncing:mob.bouncing=Trueifmob.rect.x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-10 14:21 , Processed in 0.473353 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表