飛行機ゲーム demo
  1. pygame.init()
  2. bg_size = width, height = 480, 600
  3. screen = pygame.display.set_mode(bg_size)
  4. pygame.display.set_caption("飛行機ゲーム Demo")
  5. background = pygame.image.load("images/yuzhou.png").convert()
  6. BLACK = (0, 0, 0)
  7. WHITE = (255, 255, 255)
  8. GREEN = (0, 255, 0)
  9. RED = (255, 0, 0)
  10. ygame.mixer.music.load("sound/game_music.ogg")
  11. pygame.mixer.music.set_volume(0.2)
  12. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  13. bullet_sound.set_volume(0.2)
  14. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  15. bomb_sound.set_volume(0.2)
  16. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  17. supply_sound.set_volume(0.2)
  18. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  19. get_bomb_sound.set_volume(0.2)
  20. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  21. get_bullet_sound.set_volume(0.2)
  22. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  23. upgrade_sound.set_volume(0.2)
  24. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  25. enemy3_fly_sound.set_volume(0.2)
  26. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  27. enemy1_down_sound.set_volume(0.2)
  28. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  29. enemy2_down_sound.set_volume(0.2)
  30. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  31. enemy3_down_sound.set_volume(0.5)
  32. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  33. me_down_sound.set_volume(0.2)
  34. def add_small_enemies(group1, group2, num):
  35. for i in range(num):
  36. e1 = enemy.SmallEnemy(bg_size)
  37. group1.add(e1)
  38. group2.add(e1)
  39. def add_mid_enemies(group1, group2, num):
  40. for i in range(num):
  41. e2 = enemy.MidEnemy(bg_size)
  42. group1.add(e2)
  43. group2.add(e2)
  44. def add_big_enemies(group1, group2, num):
  45. for i in range(num):
  46. e3 = enemy.BigEnemy(bg_size)
  47. group1.add(e3)
  48. group2.add(e3)
  49. def inc_speed(target, inc):
  50. for each in target:
  51. each.speed += inc
  52. def main():
  53. pygame.mixer.music.play(-1)
  54. me = myplane.MyPlane(bg_size)
  55. enemies = pygame.sprite.Group()
  56. small_enemies = pygame.sprite.Group()
  57. add_small_enemies(small_enemies, enemies, 15)
  58. mid_enemies = pygame.sprite.Group()
  59. add_mid_enemies(mid_enemies, enemies, 4)
  60. big_enemies = pygame.sprite.Group()
  61. add_big_enemies(big_enemies, enemies, 2)
  62. bullet1 = []
  63. bullet1_index = 0
  64. BULLET1_NUM = 4
  65. for i in range(BULLET1_NUM):
  66. bullet1.append(bullet.Bullet1(me.rect.midtop))
  67. bullet2 = []
  68. bullet2_index = 0
  69. BULLET2_NUM = 8
  70. for i in range(BULLET2_NUM // 2):
  71. bullet2.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery)))
  72. bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery)))
  73. clock = pygame.time.Clock()
  74. e1_destroy_index = 0
  75. e2_destroy_index = 0
  76. e3_destroy_index = 0
  77. me_destroy_index = 0
  78. score = 0
  79. score_font = pygame.font.Font("font/font.ttf", 36)
  80. paused = False
  81. pause_nor_image = pygame.image.load("images/pause_nor.png").convert_alpha()
  82. pause_pressed_image = pygame.image.load("images/pause_pressed.png").convert_alpha()
  83. resume_nor_image = pygame.image.load("images/resume_nor.png").convert_alpha()
  84. resume_pressed_image = pygame.image.load("images/resume_pressed.png").convert_alpha()
  85. paused_rect = pause_nor_image.get_rect()
  86. paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
  87. paused_image = pause_nor_image
  88. level = 1
  89. bomb_image = pygame.image.load("images/bomb.png").convert_alpha()
  90. bomb_rect = bomb_image.get_rect()
  91. bomb_font = pygame.font.Font("font/font.ttf", 48)
  92. bomb_num = 3
  93. bullet_supply = supply.Bullet_Supply(bg_size)
  94. bomb_supply = supply.Bomb_Supply(bg_size)
  95. SUPPLY_TIME = USEREVENT
  96. pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
  97. DOUBLE_BULLET_TIME = USEREVENT + 1
  98. is_double_bullet = False
  99. INVINCIBLE_TIME = USEREVENT + 2
  100. life_image = pygame.image.load("images/life.png").convert_alpha()
  101. ife_rect = life_image.get_rect()
  102. life_num = 3
  103. ecorded = False
  104. gameover_font = pygame.font.Font("font/font.TTF", 48)
  105. again_image = pygame.image.load("images/again.png").convert_alpha()
  106. again_rect = again_image.get_rect()
  107. gameover_image = pygame.image.load("images/gameover.png").convert_alpha()
  108. gameover_rect = gameover_image.get_rect()
  109. switch_image = True
  110. elay = 100
  111. unning = True
  112. while running:
  113. for event in pygame.event.get():
  114. if event.type == QUIT:
  115. pygame.quit()
  116. sys.exit()
  117. elif event.type == MOUSEBUTTONDOWN:
  118. if event.button == 1 and paused_rect.collidepoint(event.pos):
  119. paused = not paused
  120. if paused:
  121. pygame.time.set_timer(SUPPLY_TIME, 0)
  122. pygame.mixer.music.pause()
  123. pygame.mixer.pause()
  124. else:
  125. pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
  126. pygame.mixer.music.unpause()
  127. pygame.mixer.unpause()
  128. elif event.type == MOUSEMOTION:
  129. if paused_rect.collidepoint(event.pos):
  130. if paused:
  131. paused_image = resume_pressed_image
  132. else:
  133. paused_image = pause_pressed_image
  134. else:
  135. if paused:
  136. paused_image = resume_nor_image
  137. else:
  138. paused_image = pause_nor_image
  139. elif event.type == KEYDOWN:
  140. if event.key == K_SPACE:
  141. if bomb_num:
  142. bomb_num -= 1
  143. bomb_sound.play()
  144. for each in enemies:
  145. if each.rect.bottom > 0:
  146. each.active = False
  147. elif event.type == SUPPLY_TIME:
  148. supply_sound.play()
  149. if choice([True, False]):
  150. bomb_supply.reset()
  151. else:
  152. bullet_supply.reset()
  153. elif event.type == DOUBLE_BULLET_TIME:
  154. is_double_bullet = False
  155. pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)
  156. elif event.type == INVINCIBLE_TIME:
  157. me.invincible = False
  158. pygame.time.set_timer(INVINCIBLE_TIME, 0)
  159. if level == 1 and score > 50000:
  160. level = 2
  161. upgrade_sound.play()
  162. add_small_enemies(small_enemies, enemies, 3)
  163. add_mid_enemies(mid_enemies, enemies, 2)
  164. add_big_enemies(big_enemies, enemies, 1)
  165. inc_speed(small_enemies, 1)
  166. elif level == 2 and score > 300000:
  167. level = 3
  168. upgrade_sound.play()
  169. add_small_enemies(small_enemies, enemies, 5)
  170. add_mid_enemies(mid_enemies, enemies, 3)
  171. add_big_enemies(big_enemies, enemies, 2)
  172. inc_speed(small_enemies, 1)
  173. inc_speed(mid_enemies, 1)
  174. elif level == 3 and score > 600000:
  175. level = 4
  176. upgrade_sound.play()
  177. add_small_enemies(small_enemies, enemies, 5)
  178. add_mid_enemies(mid_enemies, enemies, 3)
  179. add_big_enemies(big_enemies, enemies, 2)
  180. inc_speed(small_enemies, 1)
  181. inc_speed(mid_enemies, 1)
  182. elif level == 4 and score > 1000000:
  183. upgrade_sound.play()
  184. add_small_enemies(small_enemies, enemies, 5)
  185. add_mid_enemies(mid_enemies, enemies, 3)
  186. add_big_enemies(big_enemies, enemies, 2)
  187. inc_speed(small_enemies, 1)
  188. inc_speed(mid_enemies, 1)
  189. screen.blit(background, (0, 0))
  190. if life_num and not paused:
  191. key_pressed = pygame.key.get_pressed()
  192. if key_pressed[K_w] or key_pressed[K_UP]:
  193. me.moveUp()
  194. if key_pressed[K_s] or key_pressed[K_DOWN]:
  195. me.moveDown()
  196. if key_pressed[K_a] or key_pressed[K_LEFT]:
  197. me.moveLeft()
  198. if key_pressed[K_d] or key_pressed[K_RIGHT]:
  199. me.moveRight()
  200. if bomb_supply.active:
  201. bomb_supply.move()
  202. screen.blit(bomb_supply.image, bomb_supply.rect)
  203. if pygame.sprite.collide_mask(bomb_supply, me):
  204. get_bomb_sound.play()
  205. if bomb_num < 3:
  206. bomb_num += 1
  207. bomb_supply.active = False
  208. if bullet_supply.active:
  209. bullet_supply.move()
  210. screen.blit(bullet_supply.image, bullet_supply.rect)
  211. if pygame.sprite.collide_mask(bullet_supply, me):
  212. get_bullet_sound.play()
  213. is_double_bullet = True
  214. pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
  215. bullet_supply.active = False
  216. if not (delay % 10):
  217. bullet_sound.play()
  218. if is_double_bullet:
  219. bullets = bullet2
  220. bullets[bullet2_index].reset((me.rect.centerx - 33, me.rect.centery))
  221. bullets[bullet2_index + 1].reset((me.rect.centerx + 30, me.rect.centery))
  222. bullet2_index = (bullet2_index + 2) % BULLET2_NUM
  223. else:
  224. bullets = bullet1
  225. bullets[bullet1_index].reset(me.rect.midtop)
  226. bullet1_index = (bullet1_index + 1) % BULLET1_NUM
  227. for b in bullets:
  228. if b.active:
  229. b.move()
  230. screen.blit(b.image, b.rect)
  231. enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
  232. if enemy_hit:
  233. b.active = False
  234. for e in enemy_hit:
  235. if e in mid_enemies or e in big_enemies
  236. :
  237. e.hit = True
  238. e.energy -= 1
  239. if e.energy == 0:
  240. e.active = False
  241. else:
  242. e.active = False
  243. for each in big_enemies:
  244. if each.active:
  245. each.move()
  246. if each.hit:
  247. screen.blit(each.image_hit, each.rect)
  248. each.hit = False
  249. else:
  250. if switch_image:
  251. screen.blit(each.image1, each.rect)
  252. else:
  253. screen.blit(each.image2, each.rect)
  254. pygame.draw.line(screen, BLACK, \
  255. (each.rect.left, each.rect.top - 5), \
  256. (each.rect.right, each.rect.top - 5), \
  257. 2)
  258. energy_remain = each.energy / enemy.BigEnemy.energy
  259. if energy_remain > 0.2:
  260. energy_color = GREEN
  261. else:
  262. energy_color = RED
  263. pygame.draw.line(screen, energy_color, \
  264. (each.rect.left, each.rect.top - 5), \
  265. (each.rect.left + each.rect.width * energy_remain, \
  266. each.rect.top - 5), 2)
  267. if each.rect.bottom == -50:
  268. enemy3_fly_sound.play(-1)
  269. else:
  270. if not (delay % 3):
  271. if e3_destroy_index == 0:
  272. enemy3_down_sound.play()
  273. screen.blit(each.destroy_images[e3_destroy_index], each.rect)
  274. e3_destroy_index = (e3_destroy_index + 1) % 6
  275. if e3_destroy_index == 0:
  276. enemy3_fly_sound.stop()
  277. score += 10000
  278. each.reset()
  279. for each in mid_enemies:
  280. if each.active:
  281. each.move()
  282. if each.hit:
  283. screen.blit(each.image_hit, each.rect)
  284. each.hit = False
  285. else:
  286. screen.blit(each.image, each.rect)
  287. pygame.draw.line(screen, BLACK, \
  288. (each.rect.left, each.rect.top - 5), \
  289. (each.rect.right, each.rect.top - 5), \
  290. 2)
  291. energy_remain = each.energy / enemy.MidEnemy.energy
  292. if energy_remain > 0.2:
  293. energy_color = GREEN
  294. else:
  295. energy_color = RED
  296. pygame.draw.line(screen, energy_color, \
  297. (each.rect.left, each.rect.top - 5), \
  298. (each.rect.left + each.rect.width * energy_remain, \
  299. each.rect.top - 5), 2)
  300. else:
  301. if not (delay % 3):
  302. if e2_destroy_index == 0:
  303. enemy2_down_sound.play()
  304. screen.blit(each.destroy_images[e2_destroy_index], each.rect)
  305. e2_destroy_index = (e2_destroy_index + 1) % 4
  306. if e2_destroy_index == 0:
  307. score += 6000
  308. each.reset()
  309. for each in small_enemies:
  310. if each.active:
  311. each.move()
  312. screen.blit(each.image, each.rect)
  313. else:
  314. if not (delay % 3):
  315. if e1_destroy_index == 0:
  316. enemy1_down_sound.play()
  317. screen.blit(each.destroy_images[e1_destroy_index], each.rect)
  318. e1_destroy_index = (e1_destroy_index + 1) % 4
  319. if e1_destroy_index == 0:
  320. score += 1000
  321. each.reset()
  322. enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
  323. if enemies_down and not me.invincible:
  324. me.active = False
  325. for e in enemies_down:
  326. e.active = False
  327. if me.active:
  328. if switch_image:
  329. screen.blit(me.image1, me.rect)
  330. else:
  331. screen.blit(me.image2, me.rect)
  332. else:
  333. if not (delay % 3):
  334. if me_destroy_index == 0:
  335. me_down_sound.play()
  336. screen.blit(me.destroy_images[me_destroy_index], me.rect)
  337. me_destroy_index = (me_destroy_index + 1) % 4
  338. if me_destroy_index == 0:
  339. life_num -= 1
  340. me.reset()
  341. pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)
  342. bomb_text = bomb_font.render("× %d" % bomb_num, True, WHITE)
  343. text_rect = bomb_text.get_rect()
  344. screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
  345. screen.blit(bomb_text, (20 + bomb_rect.width, height - 5 - text_rect.height))
  346. if life_num:
  347. for i in range(life_num):
  348. screen.blit(life_image, \
  349. (width - 10 - (i + 1) * life_rect.width, \
  350. height - 10 - life_rect.height))
  351. score_text = score_font.render("Score : %s" % str(score), True, WHITE)
  352. screen.blit(score_text, (10, 5))
  353. elif life_num == 0:
  354. pygame.mixer.music.stop()
  355. pygame.mixer.stop()
  356. pygame.time.set_timer(SUPPLY_TIME, 0)
  357. if not recorded:
  358. recorded = True
  359. with open("record.txt", "r") as f:
  360. record_score = int(f.read())
  361. if score > record_score:
  362. with open("record.txt", "w") as f:
  363. f.write(str(score))
  364. record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
  365. screen.blit(record_score_text, (50, 50))
  366. gameover_text1 = gameover_font.render("Your Score", True, (255, 255, 255))
  367. gameover_text1_rect = gameover_text1.get_rect()
  368. gameover_text1_rect.left, gameover_text1_rect.top = \
  369. (width - gameover_text1_rect.width) // 2, height // 3
  370. screen.blit(gameover_text1, gameover_text1_rect)
  371. gameover_text2 = gameover_font.render(str(score), True, (255, 255, 255))
  372. gameover_text2_rect = gameover_text2.get_rect()
  373. gameover_text2_rect.left, gameover_text2_rect.top = \
  374. (width - gameover_text2_rect.width) // 2, \
  375. gameover_text1_rect.bottom + 10
  376. screen.blit(gameover_text2, gameover_text2_rect)
  377. again_rect.left, again_rect.top = \
  378. (width - again_rect.width) // 2, \
  379. gameover_text2_rect.bottom + 50
  380. screen.blit(again_image, again_rect)
  381. gameover_rect.left, gameover_rect.top = \
  382. (width - again_rect.width) // 2, \
  383. again_rect.bottom + 10
  384. screen.blit(gameover_image, gameover_rect)
  385. if pygame.mouse.get_pressed()[0]:
  386. pos = pygame.mouse.get_pos()
  387. if again_rect.left < pos[0] < again_rect.right and \
  388. again_rect.top < pos[1] < again_rect.bottom:
  389. main()
  390. elif gameover_rect.left < pos[0] < gameover_rect.right and \
  391. gameover_rect.top < pos[1] < gameover_rect.bottom:
  392. pygame.quit()
  393. sys.exit()
  394. screen.blit(paused_image, paused_rect)
  395. if not (delay % 5):
  396. switch_image = not switch_image
  397. delay -= 1
  398. if not delay:
  399. delay = 100
  400. pygame.display.flip()
  401. clock.tick(60)
  402. if __name__ == "__main__":
  403. try:
  404. main()
  405. except SystemExit:
  406. pass
  407. except:
  408. traceback.print_exc()
  409. pygame.quit()
  410. input()
試作録画
参照1(ソースコードの資源本体)
参照2