搜索
查看: 8286|回复: 31

Python小游戏:飞机大战{更新1.9}

  [复制链接]
发表于 2022-10-18 19:52:25 | 显示全部楼层 |阅读模式
本帖最后由 电脑时间 于 2023-2-8 09:25 编辑

更新:
1 使用exe
2 修复 飞机过大
3 有彩蛋
链接:
https://pan.huang1111.cn/s/mzEoS1
提取码:DNSJ

飞机大战就是玩家飞机通过发射子弹来击落敌机
已知问题:
2 敌机有可能出生在角落卡子弹
3 可以漏过敌机
源代码(PyCharm上的色)


  1. # -*- coding: utf-8 -*-
  2. import pygame      
  3. from pygame.locals import *
  4. from sys import exit
  5. import random
  6. import codecs

  7. SCREEN_WIDTH = 480
  8. SCREEN_HEIGHT = 800

  9. # 子弹类
  10. class Bullet(pygame.sprite.Sprite):
  11.     def __init__(self, bullet_img, init_pos):

  12.         pygame.sprite.Sprite.__init__(self)
  13.         self.image = bullet_img
  14.         self.rect = self.image.get_rect()
  15.         self.rect.midbottom = init_pos
  16.         self.speed = 10

  17.     def move(self):
  18.         self.rect.top -= self.speed

  19. # 玩家飞机类
  20. class Player(pygame.sprite.Sprite):
  21.     def __init__(self, player_rect, init_pos):
  22.         pygame.sprite.Sprite.__init__(self)
  23.         self.image = []
  24.         for i in range(len(player_rect)):
  25.             self.image.append(player_rect.convert_alpha())

  26.         self.rect = player_rect[0].get_rect()
  27.         self.rect.topleft = init_pos
  28.         self.speed = 8
  29.         self.bullets = pygame.sprite.Group()
  30.         self.img_index = 0
  31.         self.is_hit = False

  32.     def shoot(self, bullet_img):
  33.         bullet = Bullet(bullet_img, self.rect.midtop)
  34.         self.bullets.add(bullet)

  35.     def moveUp(self):
  36.         if self.rect.top <= 0:
  37.             self.rect.top = 0
  38.         else:
  39.             self.rect.top -= self.speed

  40.     def moveDown(self):
  41.         if self.rect.top >= SCREEN_HEIGHT - self.rect.height:
  42.             self.rect.top = SCREEN_HEIGHT - self.rect.height
  43.         else:
  44.             self.rect.top += self.speed

  45.     def moveLeft(self):
  46.         if self.rect.left <= 0:
  47.             self.rect.left = 0
  48.         else:
  49.             self.rect.left -= self.speed

  50.     def moveRight(self):
  51.         if self.rect.left >= SCREEN_WIDTH - self.rect.width:
  52.             self.rect.left = SCREEN_WIDTH - self.rect.width
  53.         else:
  54.             self.rect.left += self.speed

  55. class Enemy(pygame.sprite.Sprite):
  56.     def __init__(self, enemy_img, enemy_down_imgs, init_pos):
  57.         pygame.sprite.Sprite.__init__(self)
  58.         self.image = enemy_img
  59.         self.rect = self.image.get_rect()
  60.         self.rect.topleft = init_pos
  61.         self.down_imgs = enemy_down_imgs
  62.         self.speed = 2
  63.         self.down_index = 0

  64.     # 敌机移动,边界判断及删除在游戏主循环里处理
  65.     def move(self):
  66.         self.rect.top += self.speed

  67. def write_txt(content, strim, path):
  68.     f = codecs.open(path, strim, 'utf8')
  69.     f.write(str(content))
  70.     f.close()

  71. def read_txt(path):
  72.     with open(path, 'r', encoding='utf8') as f:
  73.         lines = f.readlines()
  74.     return lines


  75. pygame.init()
  76. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  77. ic_launcher = pygame.image.load('resources/image/ic_launcher.png').convert_alpha()
  78. pygame.display.set_caption('彩图版飞机大战 By 电脑时间')
  79. pygame.display.set_icon(ic_launcher)
  80. background = pygame.image.load('resources/image/background.png').convert_alpha()
  81. game_over = pygame.image.load('resources/image/gameover.png')
  82. plane_bullet = pygame.image.load('resources/image/bullet.png')
  83. player_img1= pygame.image.load('resources/image/player1.png')
  84. player_img2= pygame.image.load('resources/image/player2.png')
  85. player_img3= pygame.image.load('resources/image/player_off1.png')
  86. player_img4= pygame.image.load('resources/image/player_off2.png')
  87. player_img5= pygame.image.load('resources/image/player_off3.png')
  88. enemy_img1= pygame.image.load('resources/image/enemy1.png')
  89. enemy_img2= pygame.image.load('resources/image/enemy2.png')
  90. enemy_img3= pygame.image.load('resources/image/enemy3.png')
  91. enemy_img4= pygame.image.load('resources/image/enemy4.png')
  92. def startGame():
  93.     player_rect = []
  94.     player_rect.append(player_img1)
  95.     player_rect.append(player_img2)
  96.     player_rect.append(player_img2)
  97.     player_rect.append(player_img3)
  98.     player_rect.append(player_img4)
  99.     player_rect.append(player_img5)
  100.     player_pos = [200, 600]
  101.     player = Player(player_rect, player_pos)
  102.     bullet_img = plane_bullet
  103.     enemy1_img = enemy_img1
  104.     enemy1_rect=enemy1_img.get_rect()
  105.     enemy1_down_imgs = []
  106.     enemy1_down_imgs.append(enemy_img1)
  107.     enemy1_down_imgs.append(enemy_img2)
  108.     enemy1_down_imgs.append(enemy_img3)
  109.     enemy1_down_imgs.append(enemy_img4)
  110.     enemies1 = pygame.sprite.Group()
  111.     enemies_down = pygame.sprite.Group()
  112.     shoot_frequency = 0
  113.     enemy_frequency = 0
  114.     player_down_index = 16
  115.     score = 0
  116.     clock = pygame.time.Clock()
  117.     running = True
  118.     while running:
  119.         screen.fill(0)
  120.         screen.blit(background, (0, 0))
  121.         clock.tick(60)
  122.         if not player.is_hit:
  123.             if shoot_frequency % 15 == 0:
  124.                 player.shoot(bullet_img)
  125.             shoot_frequency += 1
  126.             if shoot_frequency >= 15:
  127.                 shoot_frequency = 0
  128.         for bullet in player.bullets:
  129.             bullet.move()
  130.             if bullet.rect.bottom < 0:
  131.                 player.bullets.remove(bullet)
  132.         player.bullets.draw(screen)
  133.         if enemy_frequency % 50 == 0:
  134.             enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0]
  135.             enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos)
  136.             enemies1.add(enemy1)
  137.         enemy_frequency += 1
  138.         if enemy_frequency >= 100:
  139.             enemy_frequency = 0
  140.         for enemy in enemies1:
  141.             enemy.move()
  142.             if pygame.sprite.collide_circle(enemy, player):
  143.                 enemies_down.add(enemy)
  144.                 enemies1.remove(enemy)
  145.                 player.is_hit = True
  146.                 break
  147.             if enemy.rect.top < 0:
  148.                 enemies1.remove(enemy)
  149.         enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1)
  150.         for enemy_down in enemies1_down:
  151.             enemies_down.add(enemy_down)
  152.         if not player.is_hit:
  153.             screen.blit(player.image[player.img_index], player.rect)
  154.             player.img_index = shoot_frequency // 8
  155.         else:
  156.             player.img_index = player_down_index // 8
  157.             screen.blit(player.image[player.img_index], player.rect)
  158.             player_down_index += 1
  159.             if player_down_index > 47:
  160.                 running = False
  161.         for enemy_down in enemies_down:
  162.             if enemy_down.down_index == 0:
  163.                 pass
  164.             if enemy_down.down_index > 7:
  165.                 enemies_down.remove(enemy_down)
  166.                 score += 100
  167.                 continue
  168.             screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect)
  169.             enemy_down.down_index += 1
  170.         enemies1.draw(screen)
  171.         score_font = pygame.font.Font(None, 36)
  172.         score_text = score_font.render(str(score), True, (255, 255, 255))
  173.         text_rect = score_text.get_rect()
  174.         text_rect.topleft = [10, 10]
  175.         screen.blit(score_text, text_rect)
  176.         pygame.display.update()
  177.         for event in pygame.event.get():
  178.             if event.type == pygame.QUIT:
  179.                 pygame.quit()
  180.                 exit()
  181.         key_pressed = pygame.key.get_pressed()
  182.         if key_pressed[K_w] or key_pressed[K_UP]:
  183.             player.moveUp()
  184.         if key_pressed[K_s] or key_pressed[K_DOWN]:
  185.             player.moveDown()
  186.         if key_pressed[K_a] or key_pressed[K_LEFT]:
  187.             player.moveLeft()
  188.         if key_pressed[K_d] or key_pressed[K_RIGHT]:
  189.             player.moveRight()

  190.     screen.blit(game_over, (0, 0))
  191.     font = pygame.font.Font(None, 48)
  192.     text = font.render('Score: ' + str(score), True, (255, 255, 255))
  193.     text_rect = text.get_rect()
  194.     text_rect.centerx = screen.get_rect().centerx
  195.     text_rect.centery = screen.get_rect().centery + 24
  196.     screen.blit(text, text_rect)
  197.     xtfont = pygame.font.SysFont('SimHei', 30)
  198.     textstart = xtfont.render('重新开始 ', True, (255, 255, 255))
  199.     text_rect = textstart.get_rect()
  200.     text_rect.centerx = screen.get_rect().centerx
  201.     text_rect.centery = screen.get_rect().centery + 120
  202.     screen.blit(textstart, text_rect)
  203.     textstart = xtfont.render('排行榜 ', True, (255, 255, 255))
  204.     text_rect = textstart.get_rect()
  205.     text_rect.centerx = screen.get_rect().centerx
  206.     text_rect.centery = screen.get_rect().centery + 180
  207.     screen.blit(textstart, text_rect)

  208.     j = 0
  209.     arrayscore = read_txt(r'score.txt')[0].split('mr')
  210.     for i in range(0, len(arrayscore)):
  211.         if score > int(arrayscore):
  212.             j = arrayscore
  213.             arrayscore = str(score)
  214.             score = 0
  215.         if int(j) > int(arrayscore):
  216.             k = arrayscore
  217.             arrayscore = str(j)
  218.             j = k
  219.     for i in range(0, len(arrayscore)):
  220.         if i == 0:
  221.             write_txt(arrayscore + 'mr', 'w', r'score.txt')
  222.         else:
  223.             if (i == 9):
  224.                 write_txt(arrayscore, 'a', r'score.txt')
  225.             else:
  226.                 write_txt(arrayscore + 'mr', 'a', r'score.txt')

  227. def gameRanking():
  228.     screen2 = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  229.     screen2.fill(0)
  230.     screen2.blit(background, (0, 0))
  231.     xtfont = pygame.font.SysFont('SimHei', 30)
  232.     textstart = xtfont.render('排行榜 ', True, (255, 0, 0))
  233.     text_rect = textstart.get_rect()
  234.     text_rect.centerx = screen.get_rect().centerx
  235.     text_rect.centery = 50
  236.     screen.blit(textstart, text_rect)
  237.     textstart = xtfont.render('重新开始 ', True, (255, 0, 0))
  238.     text_rect = textstart.get_rect()
  239.     text_rect.centerx = screen.get_rect().centerx
  240.     text_rect.centery = screen.get_rect().centery + 120
  241.     screen2.blit(textstart, text_rect)
  242.     arrayscore = read_txt(r'score.txt')[0].split('mr')
  243.     for i in range(0, len(arrayscore)):
  244.         font = pygame.font.Font(None, 48)
  245.         k=i+1
  246.         text = font.render(str(k) +"  " +arrayscore, True, (255, 0, 0))
  247.         text_rect = text.get_rect()
  248.         text_rect.centerx = screen2.get_rect().centerx
  249.         text_rect.centery = 80 + 30*k
  250.         screen2.blit(text, text_rect)

  251. startGame()

  252. while True:
  253.     for event in pygame.event.get():
  254.         if event.type == pygame.QUIT:
  255.             pygame.quit()
  256.             exit()
  257.         elif event.type == pygame.MOUSEBUTTONDOWN:
  258.             if screen.get_rect().centerx - 70 <= event.pos[0] \
  259.                     and event.pos[0] <= screen.get_rect().centerx + 50 \
  260.                     and screen.get_rect().centery + 100 <= event.pos[1] \
  261.                     and screen.get_rect().centery + 140 >= event.pos[1]:
  262.                 startGame()
  263.             if screen.get_rect().centerx - 70 <= event.pos[0] \
  264.                     and event.pos[0] <= screen.get_rect().centerx + 50 \
  265.                     and screen.get_rect().centery + 160 <= event.pos[1] \
  266.                     and screen.get_rect().centery + 200 >= event.pos[1]:
  267.                 gameRanking()
  268.     pygame.display.update()
复制代码


飞机大战.zip

724.75 KB, 下载次数: 461

评分

1

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2022-11-3 07:54:16 | 显示全部楼层
要装Pygame
要不然只会有命令行窗口一闪而过
回复

使用道具 举报

 楼主| 发表于 2022-10-18 19:52:49 | 显示全部楼层
放不了图片
回复

使用道具 举报

发表于 2022-10-18 20:04:35 | 显示全部楼层
emmm。。。我电脑打不开。
回复

使用道具 举报

发表于 2022-10-18 21:13:07 | 显示全部楼层
我发现咱们论坛好多坛友都会python啊,厉害!
回复

使用道具 举报

发表于 2022-10-18 23:13:27 | 显示全部楼层
不放点图么?
我比较好奇,飞机是啥样的。
回复

使用道具 举报

发表于 2022-10-18 23:32:19 | 显示全部楼层
OOO 发表于 2022-10-18 23:13
不放点图么?
我比较好奇,飞机是啥样的。

压缩包里有图片。
回复

使用道具 举报

发表于 2022-10-19 08:33:10 | 显示全部楼层
看起来不错 下回来玩玩
回复

使用道具 举报

发表于 2022-10-19 08:59:55 | 显示全部楼层
其实可以截个图放上来!
小朋友佳作不断呀。
回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-12-22 00:10 , Processed in 0.097553 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表