当前位置:   article > 正文

超级玛丽游戏python实现_pygame超级玛丽火球与敌人碰撞

pygame超级玛丽火球与敌人碰撞

超级玛丽游戏python实现

  1. import pygame # 将pygame库导入到python程序中
  2. from pygame.locals import * # 导入pygame中的常量
  3. import sys # 导入系统模块
  4. SCREENWIDTH = 822 # 窗口宽度
  5. SCREENHEIGHT = 199 # 窗口高度
  6. FPS = 30 # 更新画面的时间
  7. # 定义一个移动地图类
  8. class MyMap():
  9. def __init__(self, x, y):
  10. # 加载背景图片
  11. self.bg = pygame.image.load("image/bg.png").convert_alpha()
  12. self.x = x
  13. self.y = y
  14. def map_rolling(self):
  15. if self.x < -790: # 小于-790说明地图已经完全移动完毕
  16. self.x = 800 # 给地图一个新的坐标点
  17. else:
  18. self.x -= 5 # 5个像素向左移动
  19. # 更新地图
  20. def map_update(self):
  21. SCREEN.blit(self.bg, (self.x, self.y))
  22. # 背景音乐按钮
  23. class Music_Button():
  24. is_open = True # 背景乐音的标记
  25. def __init__(self):
  26. self.open_img = pygame.image.load('image/btn_open.png').convert_alpha()
  27. self.close_img = pygame.image.load('image/btn_close.png').convert_alpha()
  28. self.bg_music = pygame.mixer.Sound('audio/bg_music.wav') # 加载背景音乐
  29. # 判断鼠标是否在,按钮的范围内
  30. def is_select(self):
  31. # 获取鼠标,的坐标
  32. point_x, point_y = pygame.mouse.get_pos()
  33. w, h = self.open_img.get_size() # 获取按钮图片的大小
  34. # 判断鼠标是否在按钮范围内
  35. in_x = point_x > 20 and point_x < 20 + w
  36. in_y = point_y > 20 and point_y < 20 + h
  37. return in_x and in_y
  38. from itertools import cycle # 导入迭代工具
  39. # 玛丽类
  40. class Marie():
  41. def __init__(self):
  42. # 初始化小玛丽矩形
  43. self.rect = pygame.Rect(0, 0, 0, 0)
  44. self.jumpState = False # 跳跃的状态
  45. self.jumpHeight = 130 # 跳跃的高度
  46. self.lowest_y = 140 # 最低坐标
  47. self.jumpValue = 0 # 跳跃增变量
  48. # 小玛丽动图索引
  49. self.marieIndex = 0
  50. self.marieIndexGen = cycle([0, 1, 2])
  51. # 加载小玛丽图片
  52. self.adventure_img = (
  53. pygame.image.load("image/adventure1.png").convert_alpha(),
  54. pygame.image.load("image/adventure2.png").convert_alpha(),
  55. pygame.image.load("image/adventure3.png").convert_alpha(),
  56. )
  57. self.jump_audio = pygame.mixer.Sound('audio/jump.wav') # 跳音效
  58. self.rect.size = self.adventure_img[0].get_size()
  59. self.x = 50; # 绘制小玛丽的X坐标
  60. self.y = self.lowest_y; # 绘制小玛丽的Y坐标
  61. self.rect.topleft = (self.x, self.y)
  62. # 跳状态
  63. def jump(self):
  64. self.jumpState = True
  65. # 小玛丽移动
  66. def move(self):
  67. if self.jumpState: # 当起跳的时候
  68. if self.rect.y >= self.lowest_y: # 如果站在地上
  69. self.jumpValue = -5 # 以5个像素值向上移动
  70. if self.rect.y <= self.lowest_y - self.jumpHeight: # 小玛丽到达顶部回落
  71. self.jumpValue = 5 # 以5个像素值向下移动
  72. self.rect.y += self.jumpValue # 通过循环改变玛丽的Y坐标
  73. if self.rect.y >= self.lowest_y: # 如果小玛丽回到地面
  74. self.jumpState = False # 关闭跳跃状态
  75. # 绘制小玛丽
  76. def draw_marie(self):
  77. # 匹配小玛丽动图
  78. marieIndex = next(self.marieIndexGen)
  79. # 绘制小玛丽
  80. SCREEN.blit(self.adventure_img[marieIndex],
  81. (self.x, self.rect.y))
  82. import random # 随机数
  83. # 障碍物类
  84. class Obstacle():
  85. score = 1 # 分数
  86. move = 5 # 移动距离
  87. obstacle_y = 150 # 障碍物y坐标
  88. def __init__(self):
  89. # 初始化障碍物矩形
  90. self.rect = pygame.Rect(0, 0, 0, 0)
  91. # 加载障碍物图片
  92. self.missile = pygame.image.load("image/missile.png").convert_alpha()
  93. self.pipe = pygame.image.load("image/pipe.png").convert_alpha()
  94. # 加载分数图片
  95. self.numbers = (pygame.image.load('image/0.png').convert_alpha(),
  96. pygame.image.load('image/1.png').convert_alpha(),
  97. pygame.image.load('image/2.png').convert_alpha(),
  98. pygame.image.load('image/3.png').convert_alpha(),
  99. pygame.image.load('image/4.png').convert_alpha(),
  100. pygame.image.load('image/5.png').convert_alpha(),
  101. pygame.image.load('image/6.png').convert_alpha(),
  102. pygame.image.load('image/7.png').convert_alpha(),
  103. pygame.image.load('image/8.png').convert_alpha(),
  104. pygame.image.load('image/9.png').convert_alpha())
  105. # 加载加分音效
  106. self.score_audio = pygame.mixer.Sound('audio/score.wav') # 加分
  107. # 0和1随机数
  108. r = random.randint(0, 1)
  109. if r == 0: # 如果随机数为0显示导弹障碍物相反显示管道
  110. self.image = self.missile # 显示导弹障碍
  111. self.move = 15 # 移动速度加快
  112. self.obstacle_y = 100 # 导弹坐标在天上
  113. else:
  114. self.image = self.pipe # 显示管道障碍
  115. # 根据障碍物位图的宽高来设置矩形
  116. self.rect.size = self.image.get_size()
  117. # 获取位图宽高
  118. self.width, self.height = self.rect.size
  119. # 障碍物绘制坐标
  120. self.x = 800
  121. self.y = self.obstacle_y
  122. self.rect.center = (self.x, self.y)
  123. # 障碍物移动
  124. def obstacle_move(self):
  125. self.rect.x -= self.move
  126. # 绘制障碍物
  127. def draw_obstacle(self):
  128. SCREEN.blit(self.image, (self.rect.x, self.rect.y))
  129. # 获取分数
  130. def getScore(self):
  131. self.score
  132. tmp = self.score;
  133. if tmp == 1:
  134. self.score_audio.play() # 播放加分音乐
  135. self.score = 0;
  136. return tmp;
  137. # 显示分数
  138. def showScore(self, score):
  139. # 获取得分数字
  140. self.scoreDigits = [int(x) for x in list(str(score))]
  141. totalWidth = 0 # 要显示的所有数字的总宽度
  142. for digit in self.scoreDigits:
  143. # 获取积分图片的宽度
  144. totalWidth += self.numbers[digit].get_width()
  145. # 分数横向位置
  146. Xoffset = (SCREENWIDTH - (totalWidth+30))
  147. for digit in self.scoreDigits:
  148. # 绘制分数
  149. SCREEN.blit(self.numbers[digit], (Xoffset, SCREENHEIGHT * 0.1))
  150. # 随着数字增加改变位置
  151. Xoffset += self.numbers[digit].get_width()
  152. # 游戏结束的方法
  153. def game_over():
  154. bump_audio = pygame.mixer.Sound('audio/bump.wav') # 撞击
  155. bump_audio.play() # 播放撞击音效
  156. # 获取窗体宽、高
  157. screen_w = pygame.display.Info().current_w
  158. screen_h = pygame.display.Info().current_h
  159. # 加载游戏结束的图片
  160. over_img = pygame.image.load('image/gameover.png').convert_alpha()
  161. # 将游戏结束的图片绘制在窗体的中间位置
  162. SCREEN.blit(over_img, ((screen_w - over_img.get_width()) / 2,
  163. (screen_h - over_img.get_height()) / 2))
  164. def mainGame():
  165. score = 0 # 得分
  166. over = False # 游戏结束标记
  167. global SCREEN, FPSCLOCK
  168. pygame.init() # 经过初始化以后我们就可以尽情地使用pygame了。
  169. # 使用Pygame时钟之前,必须先创建Clock对象的一个实例,
  170. # 控制每个循环多长时间运行一次。
  171. FPSCLOCK = pygame.time.Clock()
  172. SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) # 通常来说我们需要先创建一个窗口,方便我们与程序的交互。
  173. pygame.display.set_caption('玛丽冒险') # 设置窗口标题
  174. # 创建地图对象
  175. bg1 = MyMap(0, 0)
  176. bg2 = MyMap(800, 0)
  177. # 创建小玛丽对象
  178. marie = Marie()
  179. addObstacleTimer = 0 # 添加障碍物的时间
  180. list = [] # 障碍物对象列表
  181. music_button = Music_Button() # 创建背景音乐按钮对象
  182. btn_img = music_button.open_img # 设置背景音乐按钮的默认图片
  183. music_button.bg_music.play(-1) # 循环播放背景音乐
  184. while True:
  185. # 获取单击事件
  186. for event in pygame.event.get():
  187. if event.type == pygame.MOUSEBUTTONUP: # 判断鼠标事件
  188. if music_button.is_select(): # 判断鼠标是否在静音按钮范围
  189. if music_button.is_open: # 判断背景音乐状态
  190. btn_img = music_button.close_img # 单击后显示关闭状态的图片
  191. music_button.is_open = False # 关闭背景音乐状态
  192. music_button.bg_music.stop() # 停止背景音乐的播放
  193. else:
  194. btn_img = music_button.open_img
  195. music_button.is_open = True
  196. music_button.bg_music.play(-1)
  197. # 如果单击了关闭窗口就将窗口关闭
  198. if event.type == QUIT:
  199. pygame.quit() # 退出窗口
  200. sys.exit() # 关闭窗口
  201. # 单击键盘空格键,开启跳的状态
  202. if event.type == KEYDOWN and event.key == K_SPACE:
  203. if marie.rect.y >= marie.lowest_y: # 如果小玛丽在地面上
  204. marie.jump_audio.play() # 播放小玛丽跳跃音效
  205. marie.jump() # 开启小玛丽跳的状态
  206. if over == True: # 判断游戏结束的开关是否开启
  207. mainGame() # 如果开启将调用mainGame方法重新启动游戏
  208. if over == False:
  209. # 绘制地图起到更新地图的作用
  210. bg1.map_update()
  211. # 地图移动
  212. bg1.map_rolling()
  213. bg2.map_update()
  214. bg2.map_rolling()
  215. # 小玛丽移动
  216. marie.move()
  217. # 绘制小玛丽
  218. marie.draw_marie()
  219. # 计算障碍物间隔时间
  220. if addObstacleTimer >= 1300:
  221. r = random.randint(0, 100)
  222. if r > 40:
  223. # 创建障碍物对象
  224. obstacle = Obstacle()
  225. # 将障碍物对象添加到列表中
  226. list.append(obstacle)
  227. # 重置添加障碍物时间
  228. addObstacleTimer = 0
  229. # 循环遍历障碍物
  230. for i in range(len(list)):
  231. # 障碍物移动
  232. list[i].obstacle_move()
  233. # 绘制障碍物
  234. list[i].draw_obstacle()
  235. # 判断小玛丽与障碍物是否碰撞
  236. if pygame.sprite.collide_rect(marie, list[i]):
  237. over = True # 碰撞后开启结束开关
  238. game_over() # 调用游戏结束的方法
  239. music_button.bg_music.stop()
  240. else:
  241. # 判断小玛丽是否跃过了障碍物
  242. if (list[i].rect.x + list[i].rect.width) < marie.rect.x:
  243. # 加分
  244. score += list[i].getScore()
  245. # 显示分数
  246. list[i].showScore(score)
  247. addObstacleTimer += 20 # 增加障碍物时间
  248. SCREEN.blit(btn_img, (20, 20)) # 绘制背景音乐按钮
  249. pygame.display.update() # 更新整个窗口
  250. FPSCLOCK.tick(FPS) # 循环应该多长时间运行一次
  251. if __name__ == '__main__':
  252. mainGame()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/215753
推荐阅读
相关标签
  

闽ICP备14008679号