2020年9月12日星期六

96C 《零基礎入門學習Python》筆記 第096講:Pygame:飛機大戰7 下


  1. #myplane.py
  2. import pygame
  3. class MyPlane(pygame.sprite.Sprite):
  4. def __init__(self, bg_size):
  5. pygame.sprite.Sprite.__init__(self)
  6. self.image1 = pygame.image.load("images/me1.png").convert_alpha()
  7. self.image2 = pygame.image.load("images/me2.png").convert_alpha()
  8. self.destroy_images = []
  9. self.destroy_images.extend([\
  10. pygame.image.load("images/me_destroy_1.png").convert_alpha(), \
  11. pygame.image.load("images/me_destroy_2.png").convert_alpha(), \
  12. pygame.image.load("images/me_destroy_3.png").convert_alpha(), \
  13. pygame.image.load("images/me_destroy_4.png").convert_alpha() \
  14. ])
  15. self.rect = self.image1.get_rect()
  16. self.width, self.height = bg_size[0], bg_size[1]
  17. self.rect.left, self.rect.top = \
  18. (self.width - self.rect.width) // 2, \
  19. self.height - self.rect.height - 60
  20. self.speed = 10
  21. self.active = True
  22. self.invincible = False
  23. self.mask = pygame.mask.from_surface(self.image1)
  24. def moveUp(self):
  25. if self.rect.top > 0:
  26. self.rect.top -= self.speed
  27. else:
  28. self.rect.top = 0
  29. def moveDown(self):
  30. if self.rect.bottom < self.height - 60:
  31. self.rect.top += self.speed
  32. else:
  33. self.rect.bottom = self.height - 60
  34. def moveLeft(self):
  35. if self.rect.left > 0:
  36. self.rect.left -= self.speed
  37. else:
  38. self.rect.left = 0
  39. def moveRight(self):
  40. if self.rect.right < self.width:
  41. self.rect.left += self.speed
  42. else:
  43. self.rect.right = self.width
  44. def reset(self):
  45. self.rect.left, self.rect.top = \
  46. (self.width - self.rect.width) // 2, \
  47. self.height - self.rect.height - 60
  48. self.active = True
  49. self.invincible = True
  50. #enemy.py
  51. import pygame
  52. from random import *
  53. class SmallEnemy(pygame.sprite.Sprite):
  54. def __init__(self, bg_size):
  55. pygame.sprite.Sprite.__init__(self)
  56. self.image = pygame.image.load("images/enemy1.png").convert_alpha()
  57. self.destroy_images = []
  58. self.destroy_images.extend([\
  59. pygame.image.load("images/enemy1_down1.png").convert_alpha(), \
  60. pygame.image.load("images/enemy1_down2.png").convert_alpha(), \
  61. pygame.image.load("images/enemy1_down3.png").convert_alpha(), \
  62. pygame.image.load("images/enemy1_down4.png").convert_alpha() \
  63. ])
  64. self.rect = self.image.get_rect()
  65. self.width, self.height = bg_size[0], bg_size[1]
  66. self.speed = 2
  67. self.active = True
  68. self.rect.left, self.rect.top = \
  69. randint(0, self.width - self.rect.width), \
  70. randint(-5 * self.height, 0)
  71. self.mask = pygame.mask.from_surface(self.image)
  72. def move(self):
  73. if self.rect.top < self.height:
  74. self.rect.top += self.speed
  75. else:
  76. self.reset()
  77. def reset(self):
  78. self.active = True
  79. self.rect.left, self.rect.top = \
  80. randint(0, self.width - self.rect.width), \
  81. randint(-5 * self.height, 0)
  82. class MidEnemy(pygame.sprite.Sprite):
  83. energy = 8
  84. def __init__(self, bg_size):
  85. pygame.sprite.Sprite.__init__(self)
  86. self.image = pygame.image.load("images/enemy2.png").convert_alpha()
  87. self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()
  88. self.destroy_images = []
  89. self.destroy_images.extend([\
  90. pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
  91. pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
  92. pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
  93. pygame.image.load("images/enemy2_down4.png").convert_alpha() \
  94. ])
  95. self.rect = self.image.get_rect()
  96. self.width, self.height = bg_size[0], bg_size[1]
  97. self.speed = 1
  98. self.active = True
  99. self.rect.left, self.rect.top = \
  100. randint(0, self.width - self.rect.width), \
  101. randint(-10 * self.height, -self.height)
  102. self.mask = pygame.mask.from_surface(self.image)
  103. self.energy = MidEnemy.energy
  104. self.hit = False
  105. def move(self):
  106. if self.rect.top < self.height:
  107. self.rect.top += self.speed
  108. else:
  109. self.reset()
  110. def reset(self):
  111. self.active = True
  112. self.energy = MidEnemy.energy
  113. self.rect.left, self.rect.top = \
  114. randint(0, self.width - self.rect.width), \
  115. randint(-10 * self.height, -self.height)
  116. class BigEnemy(pygame.sprite.Sprite):
  117. energy = 20
  118. def __init__(self, bg_size):
  119. pygame.sprite.Sprite.__init__(self)
  120. self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
  121. self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
  122. self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()
  123. self.destroy_images = []
  124. self.destroy_images.extend([\
  125. pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
  126. pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
  127. pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
  128. pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
  129. pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
  130. pygame.image.load("images/enemy3_down6.png").convert_alpha() \
  131. ])
  132. self.rect = self.image1.get_rect()
  133. self.width, self.height = bg_size[0], bg_size[1]
  134. self.speed = 1
  135. self.active = True
  136. self.rect.left, self.rect.top = \
  137. randint(0, self.width - self.rect.width), \
  138. randint(-15 * self.height, -5 * self.height)
  139. self.mask = pygame.mask.from_surface(self.image1)
  140. self.energy = BigEnemy.energy
  141. self.hit = False
  142. def move(self):
  143. if self.rect.top < self.height:
  144. self.rect.top += self.speed
  145. else:
  146. self.reset()
  147. def reset(self):
  148. self.active = True
  149. self.energy = BigEnemy.energy
  150. self.rect.left, self.rect.top = \
  151. randint(0, self.width - self.rect.width), \
  152. randint(-15 * self.height, -5 * self.height)
  153. #bullet.py
  154. import pygame
  155. class Bullet1(pygame.sprite.Sprite):
  156. def __init__(self, position):
  157. pygame.sprite.Sprite.__init__(self)
  158. self.image = pygame.image.load("images/bullet1.png").convert_alpha()
  159. self.rect = self.image.get_rect()
  160. self.rect.left, self.rect.top = position
  161. self.speed = 12
  162. self.active = False
  163. self.mask = pygame.mask.from_surface(self.image)
  164. def move(self):
  165. self.rect.top -= self.speed
  166. if self.rect.top < 0:
  167. self.active = False
  168. def reset(self, position):
  169. self.rect.left, self.rect.top = position
  170. self.active = True
  171. class Bullet2(pygame.sprite.Sprite):
  172. def __init__(self, position):
  173. pygame.sprite.Sprite.__init__(self)
  174. self.image = pygame.image.load("images/bullet2.png").convert_alpha()
  175. self.rect = self.image.get_rect()
  176. self.rect.left, self.rect.top = position
  177. self.speed = 14
  178. self.active = False
  179. self.mask = pygame.mask.from_surface(self.image)
  180. def move(self):
  181. self.rect.top -= self.speed
  182. if self.rect.top < 0:
  183. self.active = False
  184. def reset(self, position):
  185. self.rect.left, self.rect.top = position
  186. self.active = True
  187. #supply.py
  188. import pygame
  189. from random import *
  190. class Bullet_Supply(pygame.sprite.Sprite):
  191. def __init__(self, bg_size):
  192. pygame.sprite.Sprite.__init__(self)
  193. self.image = pygame.image.load("images/bullet_supply.png").convert_alpha()
  194. self.rect = self.image.get_rect()
  195. self.width, self.height = bg_size[0], bg_size[1]
  196. self.rect.left, self.rect.bottom = \
  197. randint(0, self.width - self.rect.width), -100
  198. self.speed = 5
  199. self.active = False
  200. self.mask = pygame.mask.from_surface(self.image)
  201. def move(self):
  202. if self.rect.top < self.height:
  203. self.rect.top += self.speed
  204. else:
  205. self.active = False
  206. def reset(self):
  207. self.active = True
  208. self.rect.left, self.rect.bottom = \
  209. randint(0, self.width - self.rect.width), -100
  210. class Bomb_Supply(pygame.sprite.Sprite):
  211. def __init__(self, bg_size):
  212. pygame.sprite.Sprite.__init__(self)
  213. self.image = pygame.image.load("images/bomb_supply.png").convert_alpha()
  214. self.rect = self.image.get_rect()
  215. self.width, self.height = bg_size[0], bg_size[1]
  216. self.rect.left, self.rect.bottom = \
  217. randint(0, self.width - self.rect.width), -100
  218. self.speed = 5
  219. self.active = False
  220. self.mask = pygame.mask.from_surface(self.image)
  221. def move(self):
  222. if self.rect.top < self.height:
  223. self.rect.top += self.speed
  224. else:
  225. self.active = False
  226. def reset(self):
  227. self.active = True
  228. self.rect.left, self.rect.bottom = \
  229. randint(0, self.width - self.rect.width), -100
至此,Pygame 的內容就講解完畢了。
至此,《零基礎入門學習Python》的課程講解也完畢了。
接下來有新的更高級的課程哦。
相關代碼與素材:https://dxyxln.github.io/Plane-Wars/

僅供學習交流,請勿商用。

0 留言:

發佈留言