当前位置:   article > 正文

python-小游戏(打飞机)v1.1

python-小游戏(打飞机)v1.1

---------------------------------备注-----------------------------------

运行环境 :

python --v3.2.5

pygame -- pygame-1.9.2a0.win32-py3.2

-------------------------------------------------------------------------

  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import random
  4. from pygame.locals import *
  5. from sys import exit
  6. pygame.init()
  7. class FeiJi:
  8. def __init__(self,x,y):
  9. self.x = x
  10. self.y = y
  11. self.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\feiji.png')
  12. def restart(self):
  13. self.x = 200
  14. self.y = 500
  15. #加载 飞机 图像
  16. screen.blit(feiji.image, (feiji.x,feiji.y))
  17. def move(self):
  18. #-----------------------------------
  19. #判断 按键 状态
  20. if event.type==pygame.KEYDOWN:
  21. if event.key==K_w:
  22. keys[0]=True
  23. elif event.key==K_a:
  24. keys[1]=True
  25. elif event.key==K_s:
  26. keys[2]=True
  27. elif event.key==K_d:
  28. keys[3]=True
  29. if event.type==pygame.KEYUP:
  30. if event.key==K_w:
  31. keys[0]=False
  32. elif event.key==K_a:
  33. keys[1]=False
  34. elif event.key==K_s:
  35. keys[2]=False
  36. elif event.key==K_d:
  37. keys[3]=False
  38. #-----------------------------------
  39. # 按键 的操作
  40. if keys[0]==True:
  41. if self.y > 0:
  42. self.y-=0.1
  43. if keys[1]==True:
  44. if self.x > 0:
  45. self.x-=0.1
  46. if keys[2]==True:
  47. if self.y < 600 - feiji.image.get_height():
  48. self.y+=0.1
  49. if keys[3]==True:
  50. if self.x < 300 - feiji.image.get_width():
  51. self.x+=0.1
  52. #-----------------------------------
  53. #---------------END-----------------
  54. class ShiTou:
  55. def __init__(self):
  56. self.x = random.randint(0, 250)
  57. self.y = random.randint(-150, 0)
  58. self.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\shitou.png')
  59. self.active = False
  60. #设置石头下落速度
  61. def speed(self):
  62. self.y+=0.1
  63. #重置石头
  64. def restart(self):
  65. self.x=random.randint(0, 250)
  66. self.y=random.randint(-200, 0)
  67. self.active = True
  68. #石头的下落
  69. def down(self):
  70. if self.active:
  71. if self.y < 600:
  72. self.speed()
  73. if self.y > 600:
  74. self.active = False
  75. else:
  76. self.restart()
  77. #-----------------------------------
  78. #---------------END-----------------
  79. class ZiDan:
  80. def __init__(self):
  81. self.x = feiji.x + 7
  82. self.y = feiji.y
  83. self.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\zidan.png')
  84. self.active = False
  85. #设置子弹发射速度
  86. def speed(self):
  87. self.y-=0.8
  88. #重置子弹
  89. def restart(self):
  90. self.active = True
  91. self.y=feiji.y
  92. self.x=feiji.x + 7
  93. #子弹的发射
  94. def she(self):
  95. if self.active:
  96. self.speed()
  97. if self.y < 0:
  98. self.active = False
  99. else:
  100. self.restart()
  101. #-----------------------------------
  102. #---------------END-----------------
  103. #创建石头击落函数
  104. #判断子弹是否击中石头,只需要判断子弹是否穿过石头。
  105. #方法--判断子弹的x坐标是否大于石头图像左边的x坐标或者是否小于石头图像右边的x坐标
  106. def CheckHit(zidan, shitou):
  107. if(shitou.x < zidan.x and zidan.x < shitou.x + shitou.image.get_width()) and (shitou.y < zidan.y and zidan.y < shitou.y + shitou.image.get_height()):
  108. shitou.active = False
  109. zidan.active = False
  110. return True
  111. return False
  112. #创建飞机坠毁函数
  113. #方法参考“CheckHit”
  114. def CheckTouch(shitou, feiji):
  115. if (shitou.x + shitou.image.get_width() > feiji.x and feiji.x + feiji.image.get_width() > shitou.x) and (shitou.y + shitou.image.get_height() > feiji.y and feiji.y + feiji.image.get_height() > shitou.y):
  116. return True
  117. return False
  118. #游戏状态
  119. gameover = False
  120. font = pygame.font.Font(None, 32)
  121. font_over = pygame.font.Font(None, 128)
  122. #飞机实例
  123. feiji = FeiJi(200,500)
  124. #石头实例组
  125. shitou = []
  126. for i in range(5):
  127. shitou.append(ShiTou())
  128. #子弹实例组
  129. zidan = []
  130. for i in range(5):
  131. zidan.append(ZiDan())
  132. #子弹发射间隔
  133. zidan_jiange = 1200
  134. #引导子弹组
  135. zidan_index = 0
  136. #初始化积分
  137. jifen = 0
  138. #按键初始化(w,a,s,d,r)
  139. keys = [False, False, False, False]
  140. #设置游戏窗口大小
  141. width,height = 300,600
  142. #新建一个游戏窗口,窗口大小为(width,height)
  143. screen=pygame.display.set_mode((width,height))
  144. #游戏窗口标题
  145. pygame.display.set_caption("test-game")
  146. bg = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\black.png').convert()
  147. #游戏主循环
  148. while 1:
  149. for event in pygame.event.get():
  150. if event.type==pygame.QUIT:
  151. pygame.quit()
  152. exit(0)
  153. #将没有图像的空间填满(黑色)
  154. #screen.fill(0)
  155. #载入背景图
  156. screen.blit(bg, (0,0))
  157. #加载飞机图像
  158. screen.blit(feiji.image, (feiji.x,feiji.y))
  159. #判断游戏状态
  160. if not gameover:
  161. #文本-积分
  162. text_jifen = font.render("Score: %d"%jifen, 0, (0, 0, 0))
  163. screen.blit(text_jifen, (0, 0))
  164. #调用 飞机 移动的方法
  165. feiji.move()
  166. #加载 石头组 图像并且调用石头下落的方法
  167. for s in shitou:
  168. if CheckTouch(s, feiji):
  169. gameover = True
  170. s.down()
  171. screen.blit(s.image, (s.x,s.y))
  172. #设置子弹组循环发射--------------------------------
  173. zidan_jiange -= 1
  174. if zidan_jiange < 0:
  175. zidan_jiange = 1200 #子弹发射间隔
  176. zidan[zidan_index].restart() #子弹组发射
  177. zidan_index = (zidan_index + 1) % 5 #设置子弹组循环发射
  178. #加载 子弹组 图像并且调用子弹射击的方法---------------
  179. for z in zidan:
  180. z.she()
  181. screen.blit(z.image, (z.x,z.y))
  182. for s in shitou:
  183. if (CheckHit(z, s)): #调用 检查击中 函数,判断子弹是否击中 石头
  184. jifen+=100 #当子弹 击中 石头,积分加 100
  185. else:
  186. #文本-游戏restart
  187. text_restart = font.render("按 R 键重新游戏", 1, (0, 0, 0))
  188. screen.blit(text_restart, (100,350))
  189. #文本-游戏状态
  190. text_over = font.render("Game Over", 1, (0, 0, 0))
  191. screen.blit(text_over, (100,250))
  192. #文本-积分
  193. text_jifen = font.render("Score: %d" %jifen, 1, (0, 0, 0))
  194. screen.blit(text_jifen, (100, 300))
  195. if event.type == pygame.KEYDOWN:
  196. if event.key==K_r:
  197. jifen = 0
  198. for z in zidan:
  199. z.restart()
  200. for s in shitou:
  201. s.restart()
  202. #初始化飞机
  203. feiji.restart()
  204. #按键初始化(w,a,s,d,r)
  205. keys = [False, False, False, False]
  206. gameover = False
  207. #刷新屏幕
  208. pygame.display.flip()

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

闽ICP备14008679号