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

news/2024/9/22 18:37:50/

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

超级玛丽

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

绘制金币:

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/news/1512284.html

相关文章

React 学习——useImperativeHandle,暴漏子组件中的方法

暴露子组件的方法&#xff1a; import { forwardRef,useRef,useImperativeHandle } from react;const Input forwardRef((props,propRef)>{const sonRef useRef(null);const focusHandle () > {console.log(父组件调用子组件的方法);sonRef.current.focus();}// 暴露…

成为创作者的第1024天:成长与技术积累的旅程

前言 &#x1f4eb; 大家好&#xff0c;我是南木元元&#xff0c;热爱技术和分享&#xff0c;欢迎大家交流&#xff0c;一起学习进步&#xff01; &#x1f345; 个人主页&#xff1a;南木元元 今天是我成为创作者的第1024天。回顾这段时间&#xff0c;虽然日常的忙碌充斥着生活…

一款功能强大的本地数据全文搜索引擎Anytxt Searcher

Anytxt Searcher是一款功能强大的本地数据全文搜索引擎&#xff0c;它类似于本地磁盘的Google搜索引擎&#xff0c;是理想的桌面内容搜索工具。以下是关于Anytxt Searcher的详细介绍及使用方法&#xff1a; Anytxt Searcher是什么&#xff1f; Anytxt Searcher内置了一个功能…

8月21日星期三今日早报简报微语报早读

8月21日星期三&#xff0c;农历七月十八&#xff0c;早报微语早读。 1、《黑神话&#xff1a;悟空》Steam在线玩家突破200万&#xff1b; 2、中国篮协&#xff1a;进一步加强篮球赛事监管和赛风赛纪工作&#xff1b; 3、厦门房产新规&#xff1a;10月1日起满足条件的购房人可…

如何将 Bamboo agent 能力迁移到极狐GitLab tag 上?

极狐GitLab 是 GitLab 在中国的发行版&#xff0c;专门面向中国程序员和企业提供企业级一体化 DevOps 平台&#xff0c;用来帮助用户实现需求管理、源代码托管、CI/CD、安全合规&#xff0c;而且所有的操作都是在一个平台上进行&#xff0c;省事省心省钱。可以一键安装极狐GitL…

重构版:链动3+1创新裂变模式解析

链动31模式&#xff0c;作为一种创新的市场扩张策略&#xff0c;专注于通过产品的独特魅力驱动用户自主传播与裂变。与传统的链动21模式相比&#xff0c;它在结构上进行了重大革新&#xff0c;不再局限于传统的太阳线裂变方式&#xff0c;而是引入了四四复制的架构&#xff0c;…

Spring 中StaticListableBeanFactory

StaticListableBeanFactory 是 Spring Framework 中的一个类&#xff0c;位于 org.springframework.beans.factory.support 包中。它是一个简单的 ListableBeanFactory 实现&#xff0c;主要用于提供对静态 bean 的访问。这个类通常用于测量、监控或测试目的&#xff0c;允许被…

《计算机操作系统》(第4版)第2章 进程的描述与控制 复习笔记

第2章 进程的描述与控制 一、前趋图和程序执行 1. 前趋图 (1)定义 前趋图是指一个有向无循环图&#xff0c;可记为DAG, 它用于描述进程之间执行的先后顺序。 (2)图形表示 前趋图如图2-1所示。 图2-1 前趋图 2. 程序的执行 (1)程序顺序执行时的特征 ①顺序性。 ②封闭性。 ③ 可…