2020年9月12日星期六

95C 《零基礎入門學習Python》筆記 第095講:Pygame:飛機大戰6 下


  1. #enemy.py
  2. import pygame
  3. from random import *
  4. class SmallEnemy(pygame.sprite.Sprite):
  5. def __init__(self, bg_size):
  6. pygame.sprite.Sprite.__init__(self)
  7. self.image = pygame.image.load("images/enemy1.png").convert_alpha()
  8. #添加坠机图片
  9. self.destroy_images = []
  10. self.destroy_images.extend([\
  11. pygame.image.load("images/enemy1_down1.png").convert_alpha(), \
  12. pygame.image.load("images/enemy1_down2.png").convert_alpha(), \
  13. pygame.image.load("images/enemy1_down3.png").convert_alpha(), \
  14. pygame.image.load("images/enemy1_down4.png").convert_alpha() \
  15. ])
  16. self.rect = self.image.get_rect()
  17. self.width, self.height = bg_size[0], bg_size[1]
  18. self.speed = 2
  19. self.active = True
  20. self.rect.left, self.rect.top = \
  21. randint(0, self.width - self.rect.width), \
  22. randint(-5 * self.height, 0) #在页面外部上方5个屏幕高度范围内随机生成一个敌机
  23. self.mask = pygame.mask.from_surface(self.image) #将非透明部分标记为mask
  24. def move(self):
  25. if self.rect.top < self.height:
  26. self.rect.top += self.speed
  27. else:
  28. self.reset()
  29. def reset(self):
  30. self.active = True
  31. self.rect.left, self.rect.top = \
  32. randint(0, self.width - self.rect.width), \
  33. randint(-5 * self.height, 0)
  34. class MidEnemy(pygame.sprite.Sprite):
  35. energy = 8
  36. def __init__(self, bg_size):
  37. pygame.sprite.Sprite.__init__(self)
  38. self.image = pygame.image.load("images/enemy2.png").convert_alpha()
  39. self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()#中弹图片
  40. #添加坠机图片
  41. self.destroy_images = []
  42. self.destroy_images.extend([\
  43. pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
  44. pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
  45. pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
  46. pygame.image.load("images/enemy2_down4.png").convert_alpha() \
  47. ])
  48. self.rect = self.image.get_rect()
  49. self.width, self.height = bg_size[0], bg_size[1]
  50. self.speed = 1
  51. self.active = True
  52. self.rect.left, self.rect.top = \
  53. randint(0, self.width - self.rect.width), \
  54. randint(-10 * self.height, -self.height) #在页面外部上方1个到10个屏幕高度范围内随机生成一个敌机(1-10个,就保证了不会一开始出大一个大的)
  55. self.mask = pygame.mask.from_surface(self.image) #将非透明部分标记为mask
  56. self.energy = MidEnemy.energy
  57. self.hit = False #检测是否被击中的属性
  58. def move(self):
  59. if self.rect.top < self.height:
  60. self.rect.top += self.speed
  61. else:
  62. self.reset()
  63. def reset(self):
  64. self.active = True
  65. self.energy = MidEnemy.energy
  66. self.rect.left, self.rect.top = \
  67. randint(0, self.width - self.rect.width), \
  68. randint(-10 * self.height, -self.height)
  69. class BigEnemy(pygame.sprite.Sprite):
  70. energy = 20
  71. def __init__(self, bg_size):
  72. pygame.sprite.Sprite.__init__(self)
  73. #大型敌机有飞行特效,两张图片切换
  74. self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
  75. self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
  76. self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()#中弹图片
  77. #添加坠机图片
  78. self.destroy_images = []
  79. self.destroy_images.extend([\
  80. pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
  81. pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
  82. pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
  83. pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
  84. pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
  85. pygame.image.load("images/enemy3_down6.png").convert_alpha() \
  86. ])
  87. self.rect = self.image1.get_rect()
  88. self.width, self.height = bg_size[0], bg_size[1]
  89. self.speed = 1
  90. self.active = True
  91. self.rect.left, self.rect.top = \
  92. randint(0, self.width - self.rect.width), \
  93. randint(-15 * self.height, -5 * self.height) #在页面外部上方5-15个屏幕高度范围内随机生成一个敌机
  94. self.mask = pygame.mask.from_surface(self.image1) #将非透明部分标记为mask
  95. self.energy = BigEnemy.energy
  96. self.hit = False #检测是否被击中的属性
  97. def move(self):
  98. if self.rect.top < self.height:
  99. self.rect.top += self.speed
  100. else:
  101. self.reset()
  102. def reset(self):
  103. self.active = True
  104. self.energy = BigEnemy.energy
  105. self.rect.left, self.rect.top = \
  106. randint(0, self.width - self.rect.width), \
  107. randint(-15 * self.height, -5 * self.height)
  108. #bullet.py
  109. import pygame
  110. class Bullet1(pygame.sprite.Sprite):#普通子弹,一次一发
  111. def __init__(self, position):
  112. pygame.sprite.Sprite.__init__(self)
  113. self.image = pygame.image.load("images/bullet1.png").convert_alpha()
  114. self.rect = self.image.get_rect()
  115. self.rect.left, self.rect.top = position #把飞机机头位置给子弹位置
  116. self.speed = 12
  117. self.active = True
  118. self.mask = pygame.mask.from_surface(self.image) #非透明部分
  119. def move(self):
  120. self.rect.top -= self.speed
  121. if self.rect.top < 0: #射出屏幕外
  122. self.active = False
  123. def reset(self, position):
  124. self.rect.left, self.rect.top = position
  125. self.active = True
  126. class Bullet2(pygame.sprite.Sprite):
  127. def __init__(self, position):
  128. pygame.sprite.Sprite.__init__(self)
  129. self.image = pygame.image.load("images/bullet2.png").convert_alpha()
  130. self.rect = self.image.get_rect()
  131. self.rect.left, self.rect.top = position
  132. self.speed = 14
  133. self.active = False
  134. self.mask = pygame.mask.from_surface(self.image)
  135. def move(self):
  136. self.rect.top -= self.speed
  137. if self.rect.top < 0:
  138. self.active = False
  139. def reset(self, position):
  140. self.rect.left, self.rect.top = position
  141. self.active = True
  142. #supply.py
  143. import pygame
  144. from random import *
  145. class Bullet_Supply(pygame.sprite.Sprite):
  146. def __init__(self, bg_size):
  147. pygame.sprite.Sprite.__init__(self)
  148. self.image = pygame.image.load("images/bullet_supply.png").convert_alpha()
  149. self.rect = self.image.get_rect()
  150. self.width, self.height = bg_size[0], bg_size[1]
  151. self.rect.left, self.rect.bottom = \
  152. randint(0, self.width - self.rect.width), -100 #水平方向随机,垂直方向-100
  153. self.speed = 5
  154. self.active = False
  155. self.mask = pygame.mask.from_surface(self.image)
  156. def move(self):
  157. if self.rect.top < self.height:
  158. self.rect.top += self.speed
  159. else:
  160. self.active = False
  161. def reset(self):
  162. self.active = True
  163. self.rect.left, self.rect.bottom = \
  164. randint(0, self.width - self.rect.width), -100
  165. class Bomb_Supply(pygame.sprite.Sprite):
  166. def __init__(self, bg_size):
  167. pygame.sprite.Sprite.__init__(self)
  168. self.image = pygame.image.load("images/bomb_supply.png").convert_alpha()
  169. self.rect = self.image.get_rect()
  170. self.width, self.height = bg_size[0], bg_size[1]
  171. self.rect.left, self.rect.bottom = \
  172. randint(0, self.width - self.rect.width), -100
  173. self.speed = 5
  174. self.active = False
  175. self.mask = pygame.mask.from_surface(self.image)
  176. def move(self):
  177. if self.rect.top < self.height:
  178. self.rect.top += self.speed
  179. else:
  180. self.active = False
  181. def reset(self):
  182. self.active = True
  183. self.rect.left, self.rect.bottom = \
  184. randint(0, self.width - self.rect.width), -100

0 留言:

發佈留言