当前位置:   article > 正文

Python利用pygame实现飞机大战游戏_python 飞机大战游戏

python 飞机大战游戏

文章目录:

一:运行效果

1.演示

2.思路和功能

二:代码

文件架构

Demo


必备知识:python图形化编程pygame游戏模块

一:运行效果

1.演示

效果图◕‿◕✌✌✌

Python利用pygame实现飞机大战游戏运行演示


参考:【Python游戏】1小时开发飞机大战游戏-Pygame版本(1小时40分钟)

博主提取资源: 提取

2.思路和功能

  1. 通过方向键移动飞机,按空格键射出子弹,击毁迎面而来的敌机获取积分
  2. 在主循环内调用封装的函数对象

二:代码

文件架构

Demo

  1. import pygame #pip install pygame
  2. import random #随机
  3. import math #引入数学模块
  4. #------------------------------------------------------------------#初始化界面
  5. pygame.init()
  6. screen = pygame.display.set_mode((800, 600)) #屏幕宽和高
  7. pygame.display.set_caption('飞机大战') #设置标题
  8. # 引入UFO游戏左上角的游戏图标图片
  9. icon = pygame.image.load('ufo.png') #外部加载图像
  10. pygame.display.set_icon(icon) #设置左上角的游戏图标,图标尺寸大小为32*32
  11. # 背景图片
  12. bgImg = pygame.image.load('bg.png')
  13. #------------------------------------------------------------------#初始化界面
  14. #------------------------------------------------------------------#添加音乐音效
  15. #背景音乐
  16. pygame.mixer.music.load('bg.wav')
  17. pygame.mixer.music.play(-1) #单曲循环
  18. #射中音效
  19. bao_sound = pygame.mixer.Sound('exp.wav')
  20. #------------------------------------------------------------------#添加音乐音效
  21. #------------------------------------------------------------------#引入飞机图片
  22. playerImg = pygame.image.load('player.png')
  23. playerX = 400 #玩家的X坐标
  24. playerY = 500 #玩家的Y坐标
  25. playerStep = 0 #玩家移动的速度
  26. #------------------------------------------------------------------#引入飞机图片
  27. #------------------------------------------------------------------#分数
  28. # 初始化分数
  29. score = 0
  30. # 字体 自带的
  31. font = pygame.font.Font('freesansbold.ttf', 32)
  32. #font = pygame.font.SysFont('simsunnsimsun',32) #宋体
  33. # 显示分数
  34. def show_score():
  35. text = f'Score: {score}'
  36. # 渲染文本的 Surface 对象
  37. score_render = font.render(text, True, (255,0,0))
  38. # 将一个图像(Surface 对象)绘制到另一个图像上
  39. screen.blit(score_render, (10,10))
  40. #------------------------------------------------------------------#分数
  41. #------------------------------------------------------------------#游戏结束
  42. # 默认是开启游戏的
  43. is_over = False
  44. # 字体
  45. over_font = pygame.font.Font('freesansbold.ttf', 64)
  46. # 判断游戏是否结束
  47. def check_is_over():
  48. if is_over:
  49. text = "Game Over"
  50. # 渲染文本的 Surface 对象
  51. render = over_font.render(text, True, (255,0,0))
  52. # 将一个图像(Surface 对象)绘制到另一个图像上
  53. screen.blit(render, (200,250))
  54. #------------------------------------------------------------------#游戏结束
  55. #------------------------------------------------------------------#两个点之间的距离
  56. def distance(bx, by, ex, ey):
  57. a = bx - ex
  58. b = by - ey
  59. return math.sqrt(a*a + b*b) #开根号
  60. #------------------------------------------------------------------#两个点之间的距
  61. #-----------------------------------------------------------------------------------------------------------#敌人
  62. #初始化敌人的数量
  63. number_of_enemies = 6
  64. #------------------------------------------------------------------#敌人类
  65. class Enemy():
  66. def __init__(self):
  67. # 添加敌人
  68. self.img = pygame.image.load('enemy.png')
  69. self.x = random.randint(200, 600)
  70. self.y = random.randint(50, 250)
  71. # 敌人移动的速度
  72. self.step = random.randint(2, 6)
  73. #重置位置:当被射中时,恢复位置
  74. def reset(self):
  75. self.x = random.randint(200, 600)
  76. self.y = random.randint(50, 200)
  77. #------------------------------------------------------------------#敌人类
  78. #保存所有的敌人
  79. enemies = []
  80. for i in range(number_of_enemies):
  81. enemies.append(Enemy()) #调用敌人类Enemy()
  82. #------------------------------------------------------------------#显示敌人
  83. def show_enemy():
  84. global is_over
  85. for e in enemies:
  86. # 画出敌人
  87. screen.blit(e.img,(e.x, e.y))
  88. e.x += e.step
  89. # 如何敌人碰到左右边界
  90. if(e.x > 736 or e.x < 0):
  91. # 改变运行方向
  92. e.step *= -1
  93. # 开始向下沉
  94. e.y += 40
  95. # 判断游戏是否结束
  96. if e.y > 450:
  97. # 显示结束状态 print("游戏结束啦")
  98. is_over = True
  99. enemies.clear()
  100. # ------------------------------------------------------------------#显示敌人
  101. #-----------------------------------------------------------------------------------------------------------#敌人
  102. #-----------------------------------------------------------------------------------------------------------#子弹
  103. #------------------------------------------------------------------#子弹类
  104. class Bullet():
  105. def __init__(self):
  106. # 画出子弹
  107. self.img = pygame.image.load('bullet.png')
  108. self.x = playerX + 16 #(64-32)/2
  109. self.y = playerY + 10 # 子弹出现在玩家的上方一点点
  110. # 子弹移动的速度
  111. self.step = 10
  112. #判断是否击中敌人
  113. def hit(self):
  114. global score
  115. for e in enemies:
  116. if(distance(self.x, self.y, e.x, e.y) < 30): #子弹和敌人位置较近
  117. #射中啦
  118. bao_sound.play() # 射中音效
  119. bullets.remove(self) # 移除该子弹
  120. e.reset() # 调用重置位置函数reset()
  121. #添加分数
  122. score += 1
  123. #保存现有的子弹
  124. bullets = []
  125. #------------------------------------------------------------------#子弹类
  126. #------------------------------------------------------------------#显示并移动子弹
  127. def show_bullets():
  128. for b in bullets:
  129. # 显示图片到什么地方
  130. screen.blit(b.img, (b.x, b.y))
  131. # 看看是否击中了敌人:调用hit()函数
  132. b.hit()
  133. # 移动子弹:向上
  134. b.y -= b.step
  135. #判断子弹是否出了界面,如果出了就移除掉
  136. if b.y < 0:
  137. bullets.remove(b)
  138. #------------------------------------------------------------------#显示并移动子弹
  139. #-----------------------------------------------------------------------------------------------------------#子弹
  140. #------------------------------------------------------------------#移动飞机防止飞机出界
  141. def move_player():
  142. global playerX
  143. playerX += playerStep
  144. #防止飞机出界
  145. if playerX > 736: #右边
  146. playerX = 736
  147. if playerX < 0: #左边
  148. playerX = 0
  149. #------------------------------------------------------------------#移动飞机防止飞机出界
  150. #-------------------------------游戏主循环-----------------------------------#
  151. running = True
  152. while running:
  153. # 画出背景渲染到屏幕
  154. screen.blit(bgImg,(0,0)) # 导入背景图片
  155. # 显示分数:调用
  156. show_score()
  157. # 返回当前的所有事件
  158. for event in pygame.event.get():
  159. if event.type == pygame.QUIT: # 退出
  160. running = False
  161. #通过键盘事件控制飞机的移动
  162. if event.type == pygame.KEYDOWN: # 按下就移动
  163. if event.key == pygame.K_RIGHT: # 右键
  164. playerStep = 5
  165. elif event.key == pygame.K_LEFT: # 左键
  166. playerStep = -5
  167. elif event.key == pygame.K_SPACE: # 空格
  168. #创建一颗子弹
  169. bullets.append(Bullet()) # 调用子弹Bullet()
  170. if event.type == pygame.KEYUP: # 按键之后抬起来就不动
  171. playerStep = 0
  172. # 画出飞机
  173. screen.blit(playerImg, (playerX, playerY))
  174. # 每帧循环:依次显示
  175. move_player() #移动飞机
  176. show_enemy() #显示敌人
  177. show_bullets() #显示子弹
  178. check_is_over() #显示游戏结束字段
  179. pygame.display.update() #界面更新
  180. #-------------------------------游戏主循环-----------------------------------#
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/737936
推荐阅读
相关标签
  

闽ICP备14008679号