• python快速实现简易超级玛丽小游戏


    《超级玛丽》是一款超级马里奥全明星的同人作品,也是任天堂公司出品的著名横版游戏。

    《超级马里奥》是一款经典的像素冒险过关游戏。最早在红白机上推出,有多款后续作品,迄今多个版本合共销量已突破4000万套。其中的主角马里奥路易碧琪公主奇诺比奥等等已成为任天堂的招牌人物。主角马里奥日文原名マリオ,英文译作Mario,在译成中文时因时代不同,华语圈地区不同而译作"马力欧""玛丽"等情况也确有存在。根据任天堂公布的官方中文译名和牛津词典,一般称为"马里奥"。 

    完整代码如下:

    1. import numpy as np
    2. import random, sys
    3. import pgzrun
    4. class Brick(Actor):
    5. def react(self):
    6. if np.abs(mario.center[1]+mario.size[1]/2-self.center[1]+self.size[1]/2)<15: # z gory
    7. mario.vy = 0
    8. mario.bottom = self.top
    9. elif np.abs(mario.center[1]-mario.size[1]/2-self.center[1]-self.size[1]/2)<15: # z dolu
    10. mario.vy = 0
    11. mario.top = self.bottom
    12. elif np.abs(mario.center[0]+mario.size[0]/2-self.center[0]+self.size[0]/2)<15: # z prawej
    13. moveall(6)
    14. elif np.abs(mario.center[0]-mario.size[0]/2-self.center[0]-self.size[0]/2)<15: # z lewej
    15. moveall(-6)
    16. def move(self):
    17. pass
    18. class Coin(Actor):
    19. def react(self):
    20. if mario.colliderect(self):
    21. #sounds.coin.play()
    22. objs.remove(self)
    23. mario.points=mario.points+1
    24. def move(self):
    25. pass
    26. class Block(Actor):
    27. def react(self):
    28. if np.abs(mario.center[1]+mario.size[1]/2-self.center[1]+self.size[1]/2)<15: # z gory
    29. mario.vy = 0
    30. mario.bottom = self.top
    31. elif np.abs(mario.center[1]-mario.size[1]/2-self.center[1]-self.size[1]/2)<15: # z dolu
    32. mario.vy = 0
    33. mario.top = self.bottom
    34. animate(self, pos=(self.center[0], -10000))
    35. elif np.abs(mario.center[0]+mario.size[0]/2-self.center[0]+self.size[0]/2)<15: # z prawej
    36. moveall(6)
    37. elif np.abs(mario.center[0]-mario.size[0]/2-self.center[0]-self.size[0]/2)<15: # z lewej
    38. moveall(-6)
    39. def move(self):
    40. pass
    41. class Mushroom(Actor):
    42. def react(self):
    43. if self.colliderect(mario):
    44. mario.small=False
    45. objs.remove(self)
    46. def move(self):
    47. for obj in objs:
    48. if obj!=self and self.colliderect(obj) and not obj.image in ["bush.png","brick.png","hill.png"]:
    49. self.dir=-self.dir
    50. self.x=self.x+self.dir
    51. uy=self.vy
    52. self.vy=self.vy+2000.0*0.015
    53. self.y=self.y+(uy+self.vy)*0.5*0.015
    54. for obj in objs:
    55. if self.colliderect(obj) and np.abs(self.center[1]+self.size[1]/2-obj.center[1]+obj.size[1]/2)<15:
    56. self.vy = 0
    57. self.bottom = obj.top
    58. class Question(Actor):
    59. def react(self):
    60. if np.abs(mario.center[1]+mario.size[1]/2-self.center[1]+self.size[1]/2)<15: # z gory
    61. mario.vy = 0
    62. mario.bottom = self.top
    63. elif np.abs(mario.center[1]-mario.size[1]/2-self.center[1]-self.size[1]/2)<15: # z dolu
    64. mario.vy = 0
    65. mario.top = self.bottom
    66. if self.image=="question.png":
    67. self.image = "question2.png"
    68. objs.append(Mushroom("mushroom.png",(self.center[0],self.center[1]-50)))
    69. objs[-1].dir=1
    70. objs[-1].vy=0
    71. animate(objs[-1],pos=(self.center[0],self.center[1]-objs[-1].size[1]+2))
    72. elif np.abs(mario.center[0]+mario.size[0]/2-self.center[0]+self.size[0]/2)<15: # z prawej
    73. moveall(6)
    74. elif np.abs(mario.center[0]-mario.size[0]/2-self.center[0]-self.size[0]/2)<15: # z lewej
    75. moveall(-6)
    76. def move(self):
    77. pass
    78. class Cloud(Actor):
    79. def react(self):
    80. pass
    81. def move(self):
    82. self.x=(self.x-1)%7000
    83. class Monster(Actor):
    84. def react(self):
    85. if np.abs(mario.center[1]+mario.size[1]/2-self.center[1]+self.size[1]/2)<15: # z gory
    86. mario.vy = 0
    87. mario.bottom = self.top
    88. animate(self, pos=(self.right+50, HEIGHT+50))
    89. elif np.abs(mario.center[1]-mario.size[1]/2-self.center[1]-self.size[1]/2)<15: # z dolu
    90. mario.vy = 0
    91. mario.top = self.bottom
    92. elif np.abs(mario.center[0]+mario.size[0]/2-self.center[0]+self.size[0]/2)<15: # z prawej
    93. if mario.small:
    94. mario.dead = True
    95. newgame()
    96. else:
    97. animate(self, pos=(self.right+50, HEIGHT+50))
    98. mario.small=True
    99. elif np.abs(mario.center[0]-mario.size[0]/2-self.center[0]-self.size[0]/2)<15: # z lewej
    100. if mario.small:
    101. mario.dead = True
    102. newgame()
    103. else:
    104. animate(self, pos=(self.right+50, HEIGHT+50))
    105. mario.small=True
    106. def move(self):
    107. for obj in objs:
    108. if obj!=self and self.colliderect(obj) and not obj.image in ["bush.png","brick.png","hill.png"]:
    109. self.dir=-self.dir
    110. if self.dir==1 and self.image in ["turtle.png","turtleleft.png"]:
    111. self.image = "turtle.png"
    112. elif self.dir==-1 and self.image in ["turtle.png","turtleleft.png"]:
    113. self.image = "turtleleft.png"
    114. self.x=self.x+self.dir
    115. class Bush(Actor):
    116. def react(self):
    117. pass
    118. def move(self):
    119. pass
    120. def newgame():
    121. mario.pos=(200,HEIGHT-120)
    122. mario.vy=0
    123. mario.time=0
    124. mario.dir="right"
    125. mario.dead=False
    126. mario.small=True
    127. mario.s="s"
    128. mario.points=0
    129. mario.win=False
    130. for i in range(len(objs)):
    131. objs.remove(objs[0])
    132. file = open(sys.path[0] + "\\level1.dat")
    133. i = 0
    134. for line in file:
    135. for j in range(len(line)):
    136. if line[j]=="O":
    137. objs.append(Brick("brick.png",(j*32,32*i)))
    138. elif line[j]=="B":
    139. objs.append(Brick("brick2.png",(j*32,32*i)))
    140. elif line[j]=="D":
    141. objs.append(Block("block.png",(j*32,32*i)))
    142. elif line[j]=="Q":
    143. objs.append(Question("question.png",(j*32,32*i)))
    144. elif line[j]=="c":
    145. objs.append(Cloud("cloud.png",(j*32,32*i)))
    146. elif line[j]=="h":
    147. objs.append(Bush("hill.png",(j*32,32*i-22)))
    148. elif line[j]=="b":
    149. objs.append(Bush("bush.png",(j*32,32*i-12)))
    150. elif line[j]=="E":
    151. objs.append(Monster("enemy1.png",(j*32,32*i)))
    152. objs[-1].dir = 1
    153. elif line[j]=="T":
    154. objs.append(Monster("turtle.png",(j*32,32*i)))
    155. objs[-1].dir = 1
    156. elif line[j]=="p":
    157. objs.append(Coin("coin.png",(j*32,32*i)))
    158. i = i + 1
    159. music.play("theme.mp3")
    160. def draw():
    161. screen.fill((148, 146, 255))
    162. for obj in objs:
    163. obj.draw()
    164. mario.draw()
    165. screen.draw.text(str(mario.points),color="black",midtop=(WIDTH/8*7,10),fontsize=70,shadow=(0,0))
    166. if mario.win:
    167. screen.draw.text("You win!",color="black",midtop=(WIDTH/2,10),fontsize=170,shadow=(0,0))
    168. def moveall(x):
    169. if x>0:
    170. if 0<=mario.x:
    171. mario.x=mario.x-x
    172. elif mario.x<0:
    173. mario.x=0
    174. else:
    175. if 0<=mario.x<WIDTH/2:
    176. mario.x=mario.x-x
    177. elif mario.x>WIDTH/2:
    178. mario.x=WIDTH/2
    179. elif mario.x>=WIDTH/2:
    180. for obj in objs:
    181. obj.x=obj.x+x
    182. def move(dt):
    183. if mario.dir=="right":
    184. mario.image= mario.s + "mario.png"
    185. else:
    186. mario.image= mario.s + "marioleft.png"
    187. uy=mario.vy
    188. mario.vy=mario.vy+2000.0*dt
    189. mario.y=mario.y+(uy+mario.vy)*0.5*dt
    190. if keyboard.right:
    191. if mario.small:
    192. moveall(-2)
    193. else:
    194. moveall(-3)
    195. mario.dir="right"
    196. if mario.time<8:
    197. mario.image= mario.s + "mariomove.png"
    198. else:
    199. mario.image= mario.s + "mariomove2.png"
    200. if keyboard.left:
    201. if mario.small:
    202. moveall(2)
    203. else:
    204. moveall(3)
    205. mario.dir="left"
    206. if mario.time<8:
    207. mario.image= mario.s + "mariomoveleft.png"
    208. else:
    209. mario.image= mario.s + "mariomoveleft2.png"
    210. for obj in objs:
    211. if mario.colliderect(obj):
    212. obj.react()
    213. if mario.vy !=0 and mario.dir=="right":
    214. mario.image= mario.s + "mariojump.png"
    215. elif mario.vy !=0 and mario.dir=="left":
    216. mario.image= mario.s + "mariojumpleft.png"
    217. if mario.bottom>HEIGHT:
    218. mario.dead = True
    219. def update(dt):
    220. if mario.small:
    221. mario.s="s"
    222. else:
    223. mario.s=""
    224. mario.time=(mario.time+1)%16
    225. if not mario.win:
    226. move(dt)
    227. for obj in objs:
    228. obj.move()
    229. if obj.image=="castle.png":
    230. if np.abs(obj.center[0]-mario.center[0])<20:
    231. mario.win=True
    232. if mario.dead:
    233. #music.pause()
    234. #sounds.gameover.play()
    235. #from pygame import time
    236. #mario.dead = False
    237. #time.wait(3000)
    238. newgame()
    239. def on_key_down(key):
    240. if key==keys.SPACE and mario.vy==0:
    241. mario.vy=-800
    242. HEIGHT=640
    243. WIDTH=1024
    244. TITLE="Mario"
    245. mario=Actor("smario.png",(200,HEIGHT-120))
    246. mario.vy=0
    247. mario.time=0
    248. mario.dir="right"
    249. mario.dead=False
    250. mario.small=True
    251. mario.s="s"
    252. mario.points=0
    253. mario.win=False
    254. objs = []
    255. newgame()
    256. pgzrun.go()

    运行效果如下:

     关于项目运行的说明:

    操作方式:键盘方向键左右移动,空格键跳跃

    一、完整项目(游戏素材+代码)获取方式如下:

    阿里云盘分享

    其中music文件夹和sounds文件夹为空,music文件夹下为mp3格式的游戏背景音乐,可根据需要自行应用,放置在music文件夹下重命名为 theme 即可

    如,直接在qq音乐中搜索下载游戏的经典背景音乐

    sounds文件夹应该是吃金币的音效文件,暂未补充,可以将22行的代码注释以此消除吃金币时产生的闪退问题

     二、pyzrun导入失败问题

    需要安装pyzero包,pip install pyzero即可

    三、地图问题

    地图根据项目资源中的level1.dat构建

    c代表云朵,p代表金币,D代表可撞碎的方块,Q代表包含变大蘑菇的问号方块,B代表地图中帮助跳跃的不可撞碎的方块,h为小山背景,b为灌木丛背景,E为蘑菇怪敌人,T为乌龟敌人,O为地面

     对比图:

    自行在level1.dat中最后部分增加的地图:

     

     

    主要参考来源

    Python编写超级玛丽竟如此简单?不信你试试_qianer的博客-CSDN博客

  • 相关阅读:
    类和对象(前)
    Windows电脑中护眼(夜间)模式的开启异常
    什么是等保测评?
    蓝桥杯算法双周赛心得——迷宫逃脱(记忆化搜索)
    论文阅读 MAML (Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks)
    【ASE入门学习】ASE入门系列十六——色相与自动变色荧光棒
    学内核之十六:linux内存管理结构大蓝图
    SHC加密sh脚本
    Vue3介绍和安装
    XPS表征(工作原理与特点)-科学指南针
  • 原文地址:https://blog.csdn.net/qq_38563206/article/details/127804007