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

server/2024/9/23 0:26:10/

小时候最喜欢玩的小游戏就是超级玛丽了,有刺激有又技巧,通关真的很难,救下小公主还被抓走了,唉,心累,最后还是硬着头皮继续闯,终于要通关了,之后再玩还是没有那么容易,哈哈,不知道现在能不能通关,今天就来简单实现一下。

超级玛丽

运行起来是这样的,可以简单作为试玩。

绘制金币:

python">
class Coin(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"def update(self, cam):if self.alive:self.animation.update()self.screen.blit(self.animation.image, (self.rect.x + cam.x, self.rect.y))

绘制玛丽:

python">
spriteCollection = Sprites().spriteCollection
smallAnimation = 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,
)class Mario(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)def update(self):if self.invincibilityFrames > 0:self.invincibilityFrames -= 1self.updateTraits()self.moveMario()self.camera.move()self.applyGravity()self.checkEntityCollision()self.input.checkForInput()def moveMario(self):self.rect.y += self.vel.yself.collision.checkY()self.rect.x += self.vel.xself.collision.checkX()def checkEntityCollision(self):for ent in self.levelObj.entityList:collisionState = self.EntityCollider.check(ent)if collisionState.isColliding:if ent.type == "Item":self._onCollisionWithItem(ent)elif ent.type == "Block":self._onCollisionWithBlock(ent)elif ent.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):if not block.triggered:self.dashboard.coins += 1self.sound.play_sfx(self.sound.bump)block.triggered = Truedef _onCollisionWithMob(self, mob, collisionState):if isinstance(mob, RedMushroom) and mob.alive:self.powerup(1)self.killEntity(mob)self.sound.play_sfx(self.sound.powerup)elif collisionState.isTop and (mob.alive or mob.bouncing):self.sound.play_sfx(self.sound.stomp)self.rect.bottom = mob.rect.topself.bounce()self.killEntity(mob)elif collisionState.isTop and mob.alive and not mob.active:self.sound.play_sfx(self.sound.stomp)self.rect.bottom = mob.rect.topmob.timer = 0self.bounce()mob.alive = Falseelif collisionState.isColliding and mob.alive and not mob.active and not mob.bouncing:mob.bouncing = Trueif mob.rect.x < self.rect.x:mob.leftrightTrait.direction = -1mob.rect.x += -5self.sound.play_sfx(self.sound.kick)else:mob.rect.x += 5mob.leftrightTrait.direction = 1self.sound.play_sfx(self.sound.kick)elif collisionState.isColliding and mob.alive and not self.invincibilityFrames:if self.powerUpState == 0:self.gameOver()elif self.powerUpState == 1:self.powerUpState = 0self.traits['goTrait'].updateAnimation(smallAnimation)x, y = self.rect.x, self.rect.yself.rect = pygame.Rect(x, y + 32, 32, 32)self.invincibilityFrames = 60self.sound.play_sfx(self.sound.pipe)def bounce(self):self.traits["bounceTrait"].jump = Truedef killEntity(self, ent):if ent.__class__.__name__ != "Koopa":ent.alive = Falseelse:ent.timer = 0ent.leftrightTrait.speed = 1ent.alive = Trueent.active = Falseent.bouncing = Falseself.dashboard.points += 100def gameOver(self):srf = pygame.Surface((640, 480))srf.set_colorkey((255, 255, 255), pygame.RLEACCEL)srf.set_alpha(128)self.sound.music_channel.stop()self.sound.music_channel.play(self.sound.death)for i in range(500, 20, -2):srf.fill((0, 0, 0))pygame.draw.circle(srf,(255, 255, 255),(int(self.camera.x + self.rect.x) + 16, self.rect.y + 16),i,)self.screen.blit(srf, (0, 0))pygame.display.update()self.input.checkForInput()while self.sound.music_channel.get_busy():pygame.display.update()self.input.checkForInput()self.restart = Truedef getPos(self):return self.camera.x + self.rect.x, self.rect.ydef setPos(self, x, y):self.rect.x = xself.rect.y = ydef powerup(self, powerupID):if self.powerUpState == 0:if powerupID == 1:self.powerUpState = 1self.traits['goTrait'].updateAnimation(bigAnimation)self.rect = pygame.Rect(self.rect.x, self.rect.y-32, 32, 64)self.invincibilityFrames = 20

绘制土坯,金币盒子,动物,乌龟等的文件:

python">
class CoinBox(EntityBase):def __init__(self, screen, spriteCollection, x, y, sound, dashboard, gravity=0):super(CoinBox, self).__init__(x, y, gravity)self.screen = screenself.spriteCollection = spriteCollectionself.animation = copy(self.spriteCollection.get("CoinBox").animation)self.type = "Block"self.triggered = Falseself.time = 0self.maxTime = 10self.sound = soundself.dashboard = dashboardself.vel = 1self.item = Item(spriteCollection, screen, self.rect.x, self.rect.y)def update(self, cam):if self.alive and not self.triggered:self.animation.update()else:self.animation.image = self.spriteCollection.get("empty").imageself.item.spawnCoin(cam, self.sound, self.dashboard)if self.time < self.maxTime:self.time += 1self.rect.y -= self.velelse:if self.time < self.maxTime * 2:self.time += 1self.rect.y += self.velself.screen.blit(self.spriteCollection.get("sky").image,(self.rect.x + cam.x, self.rect.y + 2),)self.screen.blit(self.animation.image, (self.rect.x + cam.x, self.rect.y - 1))

直接运行main.py文件就可以进入游戏了:

python">
windowSize = 640, 480def main():pygame.mixer.pre_init(44100, -16, 2, 4096)pygame.init()screen = pygame.display.set_mode(windowSize)max_frame_rate = 60dashboard = Dashboard("./img/font.png", 8, screen)sound = Sound()level = Level(screen, sound, dashboard)menu = Menu(screen, dashboard, level, sound)while not menu.start:menu.update()mario = Mario(0, 0, level, screen, dashboard, sound)clock = pygame.time.Clock()while not mario.restart:pygame.display.set_caption("Super Mario running with {:d} FPS".format(int(clock.get_fps())))if mario.pause:mario.pauseObj.update()else:level.drawLevel(mario.camera)dashboard.update()mario.update()pygame.display.update()clock.tick(max_frame_rate)return 'restart'if __name__ == "__main__":exitmessage = 'restart'while exitmessage == 'restart':exitmessage = main()

需要游戏素材,和完整代码,可在下方图片获取,备注:超级玛丽 即可获取,长期有效。

在这里插入图片描述


http://www.ppmy.cn/server/101205.html

相关文章

基于springboot物流管理系统

TOC springboot208基于springboot物流管理系统 第1章 绪论 1.1 研究背景 互联网时代不仅仅是通过各种各样的电脑进行网络连接的时代&#xff0c;也包含了移动终端连接互联网进行复杂处理的一些事情。传统的互联网时代一般泛指就是PC端&#xff0c;也就是电脑互联网时代&…

这是啥设计模式-适配模式

有一个广告召回系统&#xff0c;输入用户id就可以给用户推荐相应的广告&#xff0c;一开始我们只有布尔检索和向量检索两种方式。 1. 面向接口编程&#xff0c;而非实现 第一点就是定义接口&#xff0c;客户端关注的是接口&#xff0c;对客户端来说&#xff0c;他只关心检索引…

GPS叉车安全管理系统,远程监控管理车辆,保障叉车资产安全!

叉车的管理和监管一直是一个挑战&#xff0c;九盾叉车监管系统旨在实现对叉车资产的全面监管和管理&#xff0c;结合了GPS车辆定位技术&#xff0c;为您提供了实时、精确的叉车位置信息&#xff0c;从而帮助您更好地管理您的叉车资产。 一、IC卡指纹认证&#xff1a; 确保叉车…

docker做Llm开发时可能会遇到的问题

如果没有开启GPU&#xff0c;会报错 docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]]. 原因可能是 &#xff1a;没有安装 GPU Docker 运行时 则按照如下参照安装&#xff0c; 基于 Docker 的深度学习环境&…

smallpdf: 免费高效的PDF水印添加工具

引言 在数字文档管理和分享的过程中&#xff0c;保护版权和确保文档的原创性变得尤为重要。PDF文件作为一种广泛使用的格式&#xff0c;经常需要添加水印来表明所有权或提醒查看者注意文档的敏感性。本文将介绍一款名为smallpdf的免费工具&#xff0c;它能够轻松地为PDF文件添…

Python——扩展数据类型

Python 的扩展数据类型是对内置数据类型的增强&#xff0c;旨在解决特定需求&#xff0c;提供更高级的功能。我们来看一些常见的扩展数据类型及其原理、用途&#xff0c;并通过示例逐步讲解。 1. collections.namedtuple namedtuple 是增强的元组&#xff0c;允许用名称访问元…

产品经理-如何判断一个产品的好与坏(36)

当面试官问到,如何判断一个产品的好与坏时,该怎么回答,这个问题非常综合地考查了你对产品的理解&#xff0c;但是题目本身非常大且难有标准答案 即使是面试官也不敢说能答好这道题。求职者在遇到这种很开放的题目时&#xff0c;如果不假思索就开答&#xff0c;往往是很危险的。…

互联网摸鱼日报(2024-08-13)

互联网摸鱼日报(2024-08-13) 36氪新闻 氪星晚报 | 周鸿祎&#xff1a;大模型能帮助360从广告模式转向付费订阅模式&#xff1b;腾讯申请多枚脱口秀和TA的朋友们系列商标 2B多模态新SOTA&#xff0c;华科、华南理工发布Mini-Monkey&#xff0c;专治“切分增大分辨率”后遗症 …