鱼C论坛

 找回密码
 立即注册
查看: 309|回复: 1

pygame 简陋版飞机大战

[复制链接]
发表于 2020-3-31 16:15:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看完90讲后借用小甲鱼老师的素材先试着写了一下,待完善……
  1. import pygame as py
  2. import random
  3. from threading import Thread

  4. #小飞机类
  5. class Small_plane(py.sprite.Sprite):
  6.     def __init__(self,name,down1,down2,down3,down4,speed):
  7.         super().__init__()
  8.         self._image = py.image.load(name).convert_alpha()
  9.         self._down1 = py.image.load(down1).convert_alpha()
  10.         self._down2 = py.image.load(down2).convert_alpha()
  11.         self._down3 = py.image.load(down3).convert_alpha()
  12.         self._down4 = py.image.load(down4).convert_alpha()
  13.         self._speed = speed
  14.         self.rect = self._image.get_rect()
  15.         self.rect.left,self.rect.top = random.randint(0,423),-43
  16.         self._collide = False
  17.         
  18.     def move(self):
  19.         self.rect = self.rect.move(self._speed)
  20. #中飞机类
  21. class Middle_plane(py.sprite.Sprite):
  22.     def __init__(self,name,name1,down1,down2,down3,down4,speed):
  23.         super().__init__()
  24.         self._image = py.image.load(name).convert_alpha()
  25.         self._image1 = py.image.load(name1).convert_alpha()
  26.         self._down1 = py.image.load(down1).convert_alpha()
  27.         self._down2 = py.image.load(down2).convert_alpha()
  28.         self._down3 = py.image.load(down3).convert_alpha()
  29.         self._down4 = py.image.load(down4).convert_alpha()
  30.         self._speed = speed
  31.         self.rect = self._image.get_rect()
  32.         self.rect.left,self.rect.bottom = random.randint(0,411),0
  33.         self._collide = False
  34.         self._hit = 0
  35.     def move(self):
  36.         self.rect = self.rect.move(self._speed)   
  37.     def blood(self,screen):
  38.         width = 11.5 * (6 - self._hit)
  39.         if self._hit == 0:
  40.             py.draw.rect(screen,(0,255,0),(self.rect.left,self.rect.top - 5,width,5),0)
  41.         elif self._hit == 6:
  42.             py.draw.rect(screen,(255,0,0),(self.rect.left,self.rect.top - 5,width,5),0)
  43.         else:
  44.             py.draw.rect(screen,(0,255,0),(self.rect.left,self.rect.top - 5,width,5),0)
  45.             py.draw.rect(screen,(255,0,0),(self.rect.left + width,self.rect.top - 5,69 - width,5),0)
  46.     pass
  47. #大飞机类
  48. class Large_plane(py.sprite.Sprite):
  49.     def __init__(self,name,name1,name2,down1,down2,down3,down4,down5,down6,speed):
  50.         super().__init__()
  51.         self._image = py.image.load(name).convert_alpha()
  52.         self._image1 = py.image.load(name1).convert_alpha()
  53.         self._image2 = py.image.load(name2).convert_alpha()
  54.         self._down1 = py.image.load(down1).convert_alpha()
  55.         self._down2 = py.image.load(down2).convert_alpha()
  56.         self._down3 = py.image.load(down3).convert_alpha()
  57.         self._down4 = py.image.load(down4).convert_alpha()
  58.         self._down5 = py.image.load(down5).convert_alpha()
  59.         self._down6 = py.image.load(down6).convert_alpha()   
  60.         self._speed = speed
  61.         self.rect = self._image.get_rect()
  62.         self.rect.left,self.rect.bottom = random.randint(0,315),0
  63.         self._collide = False
  64.         self._hit = 0
  65.     def move(self):
  66.         self.rect = self.rect.move(self._speed)   
  67.     def blood(self,screen):
  68.         width = 16.5 * (10 - self._hit)
  69.         if self._hit == 0:
  70.             py.draw.rect(screen,(0,255,0),(self.rect.left,self.rect.top - 5,width,5),0)
  71.         elif self._hit == 10:
  72.             py.draw.rect(screen,(255,0,0),(self.rect.left,self.rect.top - 5,width,5),0)
  73.         else:
  74.             py.draw.rect(screen,(0,255,0),(self.rect.left,self.rect.top - 5,width,5),0)
  75.             py.draw.rect(screen,(255,0,0),(self.rect.left + width,self.rect.top - 5,165 - width,5),0)   
  76.     pass
  77. #自己飞机类
  78. class My_plane(py.sprite.Sprite):
  79.     def __init__(self,name,name1,destroy1,destroy2,destroy3,destroy4,size):
  80.         super().__init__()
  81.         self._image = py.image.load(name).convert_alpha()
  82.         self._image1 = py.image.load(name1).convert_alpha()
  83.         self._destroy1 = py.image.load(destroy1).convert_alpha()
  84.         self._destroy2 = py.image.load(destroy2).convert_alpha()
  85.         self._destroy3 = py.image.load(destroy3).convert_alpha()
  86.         self._destroy4 = py.image.load(destroy4).convert_alpha()        
  87.         self.rect = self._image.get_rect()
  88.         self._size = size
  89.         self.rect.left,self.rect.bottom = (self._size[0] - self.rect.width) / 2,self._size[1] - 57
  90.         self._collide = False
  91.         self._live = True
  92.         self._protect = True
  93.         self._pause = True
  94.         
  95.     def move(self,speed):
  96.         self.rect = self.rect.move(speed)
  97.         #边界处理
  98.         if self.rect.left <= 0:
  99.             self.rect.left = 0
  100.         if self.rect.right >= self._size[0]:
  101.             self.rect.right = self._size[0]
  102.         if self.rect.top <= 0:
  103.             self.rect.top = 0
  104.         if self.rect.bottom >= self._size[1] - 57:
  105.             self.rect.bottom = self._size[1] - 57
  106.         
  107.     pass
  108. #子弹类
  109. class Bullet(py.sprite.Sprite):
  110.     def __init__(self,name,other):
  111.         super().__init__()
  112.         self._image = py.image.load(name).convert_alpha()
  113.         self.rect = self._image.get_rect()
  114.         self.rect.left,self.rect.top = other.rect.left + (other.rect.width - self.rect.width) / 2 + 1,\
  115.                                         other.rect.top - self.rect.height + 50
  116.                                        
  117.     def move(self):
  118.         self.rect = self.rect.move([0,-50])
  119.         
  120.     def existence(self):
  121.         if self.rect.top > 0:
  122.             return True
  123.         else:
  124.             return False

  125. class Supply(py.sprite.Sprite):
  126.     def __init__(self,name):
  127.         super().__init__()
  128.         self._image = py.image.load(name).convert_alpha()
  129.         self.rect = self._image.get_rect()
  130.         self.rect.left,self.rect.bottom = random.randint(0,(480 - self.rect.width)),0
  131.    
  132.     def move(self):
  133.         self.rect = self.rect.move([0,2])
  134.    
  135.     def out(self):
  136.         if self.rect.top >= 700:
  137.             return False
  138.         else:
  139.             return True

  140. class Bullet_dubble1(py.sprite.Sprite):
  141.     def __init__(self,name,other):
  142.         super().__init__()
  143.         self._image = py.image.load(name).convert_alpha()
  144.         self.rect = self._image.get_rect()
  145.         self.rect.left,self.rect.top = other.rect.left + 20 ,\
  146.                                         other.rect.top - self.rect.height + 50
  147.                                        
  148.     def move(self):
  149.         self.rect = self.rect.move([0,-50])
  150.         
  151.     def existence(self):
  152.         if self.rect.top > 0:
  153.             return True
  154.         else:
  155.             return False   

  156. class Bullet_dubble2(py.sprite.Sprite):
  157.     def __init__(self,name,other):
  158.         super().__init__()
  159.         self._image = py.image.load(name).convert_alpha()
  160.         self.rect = self._image.get_rect()
  161.         self.rect.left,self.rect.top = other.rect.left + 77 ,\
  162.                                         other.rect.top - self.rect.height + 50
  163.                                        
  164.     def move(self):
  165.         self.rect = self.rect.move([0,-50])
  166.         
  167.     def existence(self):
  168.         if self.rect.top > 0:
  169.             return True
  170.         else:
  171.             return False  
复制代码

  1. import pygame as py
  2. import sys
  3. import random
  4. import traceback
  5. from class_def import Small_plane,Middle_plane,Large_plane,My_plane,Bullet,Supply,Bullet_dubble1,Bullet_dubble2

  6. def plane_list_init(*args):
  7.     list1 = list(args)
  8.     list1[0].empty()
  9.     list1[1].empty()
  10.     list1[2].empty()
  11.     list1[3].empty()
  12.     list1[4].clear()
  13.     list1[5].clear()
  14.     list1[6].clear()
  15.     list1[7].clear()
  16.     list1[8].clear()

  17. #初始化,窗口大小
  18. def main():
  19.     py.init()
  20.     py.mixer.init()
  21.     #游戏音频导入
  22.     bg_music_name = 'sound\\game_music.ogg'
  23.     bullet_name = 'sound\\bullet.wav'
  24.     supply_name = 'sound\\supply.wav'
  25.     supply_bomb = 'sound\\get_bomb.wav'
  26.     supply_bullet = 'sound\\get_bullet.wav'
  27.     myplane_collide_name = 'sound\\me_down.wav'
  28.     bomb_name = 'sound\\use_bomb.wav'
  29.     middle_down_name = 'sound\\enemy2_down.wav'
  30.     large_down_name = 'sound\\enemy3_down.wav'
  31.     large_move_name = 'sound\\enemy3_flying.wav'
  32.    
  33.    
  34.     py.mixer.music.load(bg_music_name)
  35.     py.mixer.music.set_volume(0.1)
  36.     py.mixer.music.play()
  37.     bullet_sound = py.mixer.Sound(bullet_name)
  38.     bullet_sound.set_volume(0.3)
  39.     supply_sound = py.mixer.Sound(supply_name)
  40.     supply_sound.set_volume(0.2)
  41.     supply_bomb_sound = py.mixer.Sound(supply_bomb)
  42.     supply_bomb_sound.set_volume(0.2)
  43.     supply_bullet_sound = py.mixer.Sound(supply_bullet)
  44.     supply_bullet_sound.set_volume(0.2)
  45.     myplane_collide_sound = py.mixer.Sound(myplane_collide_name)
  46.     myplane_collide_sound.set_volume(0.2)
  47.     bomb_sound = py.mixer.Sound(bomb_name)
  48.     bomb_sound.set_volume(0.2)
  49.     middle_down_sound = py.mixer.Sound(middle_down_name)
  50.     middle_down_sound.set_volume(0.2)
  51.     large_down_sound = py.mixer.Sound(large_down_name)
  52.     large_down_sound.set_volume(0.2)
  53.     large_move_sound = py.mixer.Sound(large_move_name)
  54.     large_move_sound.set_volume(0.1)   
  55.    
  56.     #游戏所需图片导入
  57.     bg_image_name = 'images\\background.png'
  58.     bomb_name = 'images\\bomb.png'
  59.     life_name = 'images\\life.png'
  60.    
  61.     small_plane_name_1 = 'images\\enemy1.png'
  62.     small_plane_name_2 = 'images\\enemy1_down1.png'
  63.     small_plane_name_3 = 'images\\enemy1_down2.png'
  64.     small_plane_name_4 = 'images\\enemy1_down3.png'
  65.     small_plane_name_5 = 'images\\enemy1_down4.png'

  66.     middle_plane_name_1 = 'images\\enemy2.png'
  67.     middle_plane_name_2 = 'images\\enemy2_hit.png'
  68.     middle_plane_name_3 = 'images\\enemy2_down1.png'
  69.     middle_plane_name_4 = 'images\\enemy2_down2.png'
  70.     middle_plane_name_5 = 'images\\enemy2_down3.png'
  71.     middle_plane_name_6 = 'images\\enemy2_down4.png'
  72.    
  73.     large_plane_name_1 = 'images\\enemy3_n1.png'
  74.     large_plane_name_2 = 'images\\enemy3_n2.png'
  75.     large_plane_name_3 = 'images\\enemy3_hit.png'
  76.     large_plane_name_4 = 'images\\enemy3_down1.png'
  77.     large_plane_name_5 = 'images\\enemy3_down2.png'
  78.     large_plane_name_6 = 'images\\enemy3_down3.png'
  79.     large_plane_name_7 = 'images\\enemy3_down4.png'
  80.     large_plane_name_8 = 'images\\enemy3_down5.png'
  81.     large_plane_name_9 = 'images\\enemy3_down6.png'
  82.    
  83.     my_plane_name_1 = 'images\\me1.png'
  84.     my_plane_name_2 = 'images\\me2.png'
  85.     my_plane_name_3 = 'images\\me_destroy_1.png'
  86.     my_plane_name_4 = 'images\\me_destroy_2.png'
  87.     my_plane_name_5 = 'images\\me_destroy_3.png'
  88.     my_plane_name_6 = 'images\\me_destroy_4.png'


  89.     bullet_name_1 = 'images\\bullet1.png'
  90.     bullet_name_2 = 'images\\bullet2.png'
  91.     supply_name_1 = 'images\\bomb_supply.png'
  92.     supply_name_2 = 'images\\bullet_supply.png'
  93.     again_name = 'images\\again.png'
  94.     gameover_name = 'images\\gameover.png'
  95.     pause_name_1 = 'images\\pause_nor.png'
  96.     pause_name_2 = 'images\\pause_pressed.png'
  97.     resume_name_1 = 'images\\resume_nor.png'
  98.     resume_name_2 = 'images\\resume_pressed.png'

  99.     size = width,height = 480,700
  100.    
  101.     screen = py.display.set_mode(size)
  102.    
  103.     py.display.set_caption('Aircraft Wars')
  104.     bg_image = py.image.load(bg_image_name).convert_alpha()
  105.     bomb_image = py.image.load(bomb_name).convert_alpha()
  106.     life_image = py.image.load(life_name).convert_alpha()
  107.     again_image = py.image.load(again_name).convert_alpha()
  108.     again_image_rect = again_image.get_rect()
  109.     again_image_rect.left,again_image_rect.top = 90,300
  110.     gameover_image = py.image.load(gameover_name).convert_alpha()
  111.     gameover_image_rect = gameover_image.get_rect()
  112.     gameover_image_rect.left,gameover_image_rect.top = 90,400
  113.    
  114.     #文字设置
  115.     font1 = py.font.SysFont('行书',40)
  116.     font1.set_bold(True)
  117.     font2 = py.font.SysFont('行书',60)
  118.     scorestext1 = 0000
  119.     num1text1 = 1
  120.     #实例化飞机
  121.     myplane = My_plane(my_plane_name_1,my_plane_name_2,my_plane_name_3,my_plane_name_4,my_plane_name_5,my_plane_name_6,size)
  122.     #存储敌方飞机
  123.     allplanes_group = py.sprite.Group()
  124.    
  125.     smallplane_list = []
  126.     smallplane_destroylist = []
  127.     smallplane_group = py.sprite.Group()
  128.    
  129.     middleplane_list = []
  130.     middleplane_destroylist = []
  131.     middleplane_hitlist = []
  132.     middleplane_group = py.sprite.Group()
  133.    
  134.     largeplane_list = []
  135.     largeplane_destroylist = []
  136.     largeplane_hitlist = []
  137.     largeplane_group = py.sprite.Group()
  138.     #存储己方飞机发射的子弹
  139.     bullet_list  = []
  140.     bullet_group = py.sprite.Group()
  141.     #存储补给
  142.     supply_group = py.sprite.Group()
  143.     #设置按键不断响应
  144.     #py.key.set_repeat(1,10)
  145.     Music_bg = py.USEREVENT
  146.     GameSupply = py.USEREVENT + 1

  147.     py.mixer.music.set_endevent(Music_bg)

  148.    
  149.     flag_left,flag_right,flag_up,flag_down,flag_space = 0,0,0,0,0
  150.     #子弹的移动生成标志,己方飞机的显示标志,敌方飞机被击落标志
  151.     flag_bullet = 0
  152.     #敌方飞机的生成和移动标志
  153.     flag_enemyplane = 0
  154.     #己方飞机被击落标志
  155.     flag_destroyplane = 1
  156.     #毁灭动画显示控制标志
  157.     i,j,m,n = 0,0,0,0
  158.     #敌方中大飞机出现标志
  159.     start,start1,start2 =0,0,0
  160.     #补给标志
  161.     supply_bomb,supply_bullet,supply_get,supply_bullet_time,supply_time= 0,0,0,0,0
  162.     #生命条
  163.     life_num = 3
  164.     life_list = [(434,643) ,(388,643) ,(342,643)]
  165.     speed_list = [(0,1),(0,1.5),(0,2),(0,2.5),(0,3)]
  166.     smallcreate_list = [70,60,50,40,30,20]
  167.     middlecreate_list = [random.randint(2500,3000),random.randint(2000,2500),random.randint(1500,2000),random.randint(1000,1500),1000]
  168.     largecreate_list = [random.randint(4500,5000),random.randint(4000,4500),random.randint(3000,4000),random.randint(2500,3000),2000]
  169.     speed_index = 0
  170.     protect_time = 0 #保护时间
  171.     flag_pause,flag_resume= 1,0 #暂停切换标志
  172.     clock = py.time.Clock()
  173.     while True:
  174.         for event in py.event.get():
  175.             if event.type == py.QUIT:
  176.                 py.quit()
  177.                 sys.exit()

  178.                
  179.             if event.type == py.MOUSEBUTTONDOWN:
  180.                 if event.button == 1:
  181.                     pos = event.pos
  182.                     #重新开始,设置重启
  183.                     if 90<= pos[0] <= 390 and 300 <= pos[1] <= 341:
  184.                         myplane._live = True
  185.                         myplane._collide = False
  186.                         myplane.rect.left,myplane.rect.bottom = (myplane._size[0] - myplane.rect.width) / 2,myplane._size[1] - 57
  187.                         life_num = 3
  188.                         start,start1,start2 =0,0,0
  189.                         flag_enemyplane,flag_bullet= 0,0
  190.                         flag_destroyplane = 1
  191.                         num1text1 = 1
  192.                         scorestext1 = 0000  
  193.                         
  194.                     if 90<= pos[0] <= 390 and 400 <= pos[1] <= 440:
  195.                         py.quit()
  196.                         sys.exit()
  197.                     #暂停   
  198.                     if 420<= pos[0] <= 480 and 0 <= pos[1] <= 45 and flag_pause:
  199.                         py.mixer.music.pause()
  200.                         flag_resume = 1
  201.                         myplane._pause = False
  202.                     if 420<= pos[0] <= 480 and 0 <= pos[1] <= 45 and not flag_pause:
  203.                         flag_resume = 0
  204.                         py.mixer.music.unpause()
  205.                         flag_pause = 1
  206.                         myplane._pause = True               
  207.          
  208.             #己方飞机操作设置   
  209.             if event.type == py.KEYDOWN:
  210.                 if event.key == py.K_a or event.key == py.K_LEFT:
  211.                     flag_left = 1
  212.                 if event.key == py.K_d or event.key == py.K_RIGHT:
  213.                     flag_right = 1
  214.                 if event.key == py.K_w or event.key == py.K_UP:
  215.                     flag_up = 1
  216.                 if event.key == py.K_s or event.key == py.K_DOWN:
  217.                     flag_down = 1
  218.                 if event.key == py.K_SPACE:
  219.                     flag_space = 1
  220.                     
  221.             if event.type == py.KEYUP:
  222.                 if event.key == py.K_a or event.key == py.K_LEFT:
  223.                     flag_left = 0
  224.                 if event.key == py.K_d or event.key == py.K_RIGHT:
  225.                     flag_right = 0
  226.                 if event.key == py.K_w or event.key == py.K_UP:
  227.                     flag_up = 0
  228.                 if event.key == py.K_s or event.key == py.K_DOWN:
  229.                     flag_down = 0   
  230.                     
  231.             if event.type == Music_bg:
  232.                 py.mixer.music.play()
  233.                     
  234.         if myplane._live and myplane._pause:
  235.             #敌方小飞机生成与移动   
  236.             #随着分数增加难度上升
  237.             if scorestext1 < 40000:
  238.                 speed_index = 0
  239.             elif scorestext1 < 100000:
  240.                 speed_index = 1
  241.             elif scorestext1 < 250000:
  242.                 speed_index = 2                    
  243.             elif scorestext1 < 450000:
  244.                 speed_index = 3                    
  245.             else:
  246.                 speed_index = 4
  247.                
  248.             flag_enemyplane += 1
  249.             if smallplane_group:
  250.                 for each in smallplane_group:
  251.                     each.move()
  252.             if middleplane_group:
  253.                 for each in middleplane_group:
  254.                     each.move()   
  255.             if largeplane_group:
  256.                 for each in largeplane_group:
  257.                     large_move_sound.play()
  258.                     each.move()                        
  259.             if flag_enemyplane == smallcreate_list[speed_index]:
  260.                 flag_enemyplane = 0
  261.                 small_plane = Small_plane(small_plane_name_1,small_plane_name_2,small_plane_name_3,small_plane_name_4,small_plane_name_5,speed_list[speed_index])
  262.                 while py.sprite.spritecollide(small_plane,allplanes_group,False):
  263.                     small_plane.rect.left,small_plane.rect.bottom =random.randint(0,423),0                  
  264.                 smallplane_list.append(small_plane)
  265.                 smallplane_group.add(small_plane)
  266.                 allplanes_group.add(small_plane)
  267.                
  268.             start += 1
  269.             if start == 300:
  270.                 start1 = 1
  271.             if start > 300:
  272.                 if start == create_time1:
  273.                     start1 = 1
  274.             if start == 1200:
  275.                 start2 = 1
  276.             if start > 1200:
  277.                 if start == create_time2:
  278.                     start2 = 1
  279.                     
  280.             if start1:
  281.                 middle_plane = Middle_plane(middle_plane_name_1,middle_plane_name_2,middle_plane_name_3,middle_plane_name_4,middle_plane_name_5,middle_plane_name_6,speed_list[speed_index])
  282.                 while py.sprite.spritecollide(middle_plane,allplanes_group,False):
  283.                     middle_plane.rect.left,middle_plane.rect.bottom = random.randint(0,411),0
  284.                 middleplane_list.append(middle_plane)
  285.                 middleplane_group.add(middle_plane)
  286.                 allplanes_group.add(middle_plane)
  287.                 middleplane_hitlist.append(0)
  288.                 start1 = 0
  289.                 create_time1 = start + middlecreate_list[4]
  290.                
  291.             if start2:
  292.                 large_plane = Large_plane(large_plane_name_1,large_plane_name_2,large_plane_name_3,large_plane_name_4,large_plane_name_5,large_plane_name_6,large_plane_name_7,large_plane_name_8,large_plane_name_9,speed_list[speed_index])
  293.                 while py.sprite.spritecollide(large_plane,allplanes_group,False):
  294.                     large_plane.rect.left,large_plane.rect.bottom = random.randint(0,315),0
  295.                 largeplane_list.append(large_plane)
  296.                 largeplane_group.add(large_plane)
  297.                 allplanes_group.add(large_plane)
  298.                 largeplane_hitlist.append(0)
  299.                 start2 = 0
  300.                 create_time2 = start + largecreate_list[4]


  301.             #myplane 方向控制
  302.             if flag_left:
  303.                 myplane.move([-10,0])
  304.             if flag_right:
  305.                 myplane.move([10,0])
  306.             if flag_up:
  307.                 myplane.move([0,-10])
  308.             if flag_down:
  309.                 myplane.move([0,10])
  310.             #投放炸弹清屏
  311.             if flag_space and num1text1:  
  312.                 bomb_sound.play()
  313.                 flag_space = 0
  314.                 num1text1 -= 1
  315.                 scorestext1 += (len(smallplane_group) * 1000 + len(middleplane_group) * 5000 + len(largeplane_group) * 10000)
  316.                 smallplane_destroylist = smallplane_list.copy()
  317.                 middleplane_destroylist = middleplane_list.copy()
  318.                 largeplane_destroylist = largeplane_list.copy()
  319.                 plane_list_init(allplanes_group,smallplane_group,middleplane_group,largeplane_group,\
  320.                               smallplane_list,middleplane_list,largeplane_list,middleplane_hitlist,largeplane_hitlist )
  321.             
  322.             #分数/炸弹数显示
  323.             scorestext2 = 'SCORES:%04d'%scorestext1            
  324.             scores = font1.render(scorestext2,False,(255,255,255))
  325.             num1text2 = 'X%d'%num1text1
  326.             num1 = font2.render(num1text2,False,(255,255,255))
  327.             num1_rect = num1.get_rect()
  328.             num1_rect.left,num1_rect.top = 70,653
  329.             #显示
  330.             screen.blit(bg_image,(0,0))
  331.             screen.blit(bomb_image,(0,643))
  332.             for vol in range(life_num):            
  333.                 screen.blit(life_image,life_list[vol])
  334.             screen.blit(scores,(0,0))
  335.             screen.blit(num1,num1_rect)
  336.             #补给
  337.             if not flag_destroyplane % 50:
  338.                 supply_time += 1
  339.                 if supply_time == 20:
  340.                     supply_time = 0
  341.                     supply_type = random.randint(0,1)
  342.                     if supply_type:
  343.                         supply = Supply(supply_name_1)
  344.                         supply_bomb = 1
  345.                     else:
  346.                         supply = Supply(supply_name_2)
  347.                         supply_bullet = 1
  348.                     supply_sound.play()
  349.                     supply_group.add(supply)        
  350.             
  351.             if supply_group:              
  352.                 for each in supply_group:               
  353.                     if each.out():
  354.                         each.move()
  355.                     else:
  356.                         supply_group.empty()
  357.                     screen.blit(each._image,each.rect)
  358.             #己方飞机显示        
  359.             if not flag_bullet % 5 and not myplane._collide:            
  360.                 screen.blit(myplane._image,myplane.rect)
  361.             if flag_bullet % 5 and not myplane._collide:
  362.                 screen.blit(myplane._image1,myplane.rect)
  363.             #己方飞机子弹的生成与移动   
  364.             if supply_bullet and supply_get:
  365.                 if not flag_bullet % 12:
  366.                     supply_bullet_time += 1
  367.                     bullet1 = Bullet_dubble1(bullet_name_2,myplane)
  368.                     bullet2 = Bullet_dubble2(bullet_name_2,myplane)
  369.                     bullet_sound.play()
  370.                     if flag_up:
  371.                         bullet1.rect.top -= 50
  372.                         bullet2.rect.top -= 50
  373.                     bullet_list.append(bullet1)
  374.                     bullet_list.append(bullet2)
  375.                     bullet_group.add(bullet1)  
  376.                     bullet_group.add(bullet2)  
  377.                     if supply_bullet_time == 60:
  378.                         supply_bullet_time = 0
  379.                         supply_bullet = 0
  380.                         supply_get = 0
  381.                     
  382.                 if not flag_bullet % 3 and bullet_group:
  383.                     for each in bullet_group:
  384.                         if each.existence():
  385.                             each.move()
  386.                         else:
  387.                             bullet_group.remove(each)
  388.                             bullet_list.remove(each)         
  389.             else:
  390.                 if not flag_bullet % 12:
  391.                     bullet = Bullet(bullet_name_1,myplane)
  392.                     bullet_sound.play()
  393.                     if flag_up:
  394.                         bullet.rect.top -= 50
  395.                     bullet_list.append(bullet)
  396.                     bullet_group.add(bullet)
  397.                
  398.                 if not flag_bullet % 3 and bullet_group:
  399.                     for each in bullet_group:
  400.                         if each.existence():
  401.                             each.move()
  402.                         else:
  403.                             bullet_group.remove(each)
  404.                             bullet_list.remove(each)            
  405.             #检测己方飞机与敌方小飞机是否发生碰撞
  406.             if life_num == 0:
  407.                 myplane._live = False
  408.             if myplane._protect:
  409.                 if py.sprite.spritecollide(myplane,allplanes_group,False):
  410.                     myplane_collide_sound.play()
  411.                     myplane._collide = True
  412.                     myplane._protect = False
  413.                     life_num -= 1
  414.             #重生保护
  415.             else:
  416.                 protect_time += 1
  417.                 if protect_time == 300:
  418.                     protect_time = 0
  419.                     myplane._protect = True
  420.                
  421.             if py.sprite.spritecollide(myplane,supply_group,True):
  422.                 supply_get = 1
  423.                 if supply_bullet:
  424.                     supply_bullet_sound.play()
  425.                 else:
  426.                     supply_bomb_sound.play()
  427.             for each in smallplane_group:
  428.                 if py.sprite.collide_rect(each,myplane):
  429.                     each._collide = True
  430.             for each in middleplane_group:
  431.                 if py.sprite.collide_rect(each,myplane):
  432.                     each._collide = True
  433.             for each in largeplane_group:
  434.                 if py.sprite.collide_rect(each,myplane):
  435.                     each._collide = True  
  436.             #轰炸次数      
  437.             if supply_get and supply_bomb:
  438.                 supply_bomb = 0
  439.                 supply_get = 0
  440.                 if num1text1 < 3:
  441.                     num1text1 += 1
  442.             
  443.             #己方飞机坠毁
  444.             if myplane._collide:
  445.                 down = [myplane._destroy1,myplane._destroy2,myplane._destroy3,myplane._destroy4]
  446.                 if not flag_destroyplane % 6:
  447.                     screen.blit(down[j],myplane.rect)
  448.                     j += 1
  449.                     if not j % 4:
  450.                         j = 0
  451.                         myplane.rect.left,myplane.rect.bottom = (myplane._size[0] - myplane.rect.width) / 2,myplane._size[1] - 57
  452.                         myplane._collide = False
  453.                
  454.             #检测飞机是否被击落
  455.             for each in smallplane_group:
  456.                 if py.sprite.spritecollide(each,bullet_group,True):
  457.                     each._collide = True
  458.                     scorestext1 += 1000
  459.                     
  460.             for each in middleplane_group:
  461.                 if py.sprite.spritecollide(each,bullet_group,True):
  462.                     middleplane_hitlist[middleplane_list.index(each)] += 1
  463.                     each._hit += 1
  464.                     if middleplane_hitlist[middleplane_list.index(each)] == 6:
  465.                         each._collide = True
  466.                         scorestext1 += 5000               
  467.    
  468.             for each in largeplane_group:
  469.                 if py.sprite.spritecollide(each,bullet_group,True):
  470.                     largeplane_hitlist[largeplane_list.index(each)] += 1
  471.                     each._hit += 1
  472.                     if largeplane_hitlist[largeplane_list.index(each)] == 10:
  473.                         each._collide = True
  474.                         scorestext1 += 10000   
  475.             
  476.             #显示子弹轨迹
  477.             for each in bullet_group:
  478.                 screen.blit(each._image,each.rect)
  479.             #被击落飞机生成动画
  480.             for each in smallplane_group:
  481.                 if each._collide:
  482.                     smallplane_group.remove(each)
  483.                     allplanes_group.remove(each)
  484.                     smallplane_destroylist.append(smallplane_list.pop(smallplane_list.index(each)))
  485.                 screen.blit(each._image,each.rect)
  486.       
  487.             if smallplane_destroylist:
  488.                 vol = smallplane_destroylist[0]
  489.                 if not flag_bullet % 3:
  490.                     down = [vol._down1,vol._down2,vol._down3,vol._down4]
  491.                     screen.blit(down[i],vol.rect)
  492.                     i += 1
  493.                     if not i % 4:
  494.                         i = 0
  495.                         smallplane_destroylist.pop(0)
  496.                         
  497.             for each in middleplane_group:
  498.                 if each._collide:
  499.                     middle_down_sound.play()
  500.                     middleplane_group.remove(each)
  501.                     allplanes_group.remove(each)
  502.                     index = middleplane_list.index(each)
  503.                     middleplane_destroylist.append(middleplane_list.pop(index))
  504.                     middleplane_hitlist.pop(index)
  505.                 screen.blit(each._image,each.rect)
  506.                 each.blood(screen)
  507.                
  508.             if middleplane_destroylist:
  509.                 vol = middleplane_destroylist[0]
  510.                 if not flag_bullet % 3:
  511.                     down = [vol._down1,vol._down2,vol._down3,vol._down4]
  512.                     screen.blit(down[i],vol.rect)
  513.                     m += 1
  514.                     if not m % 4:
  515.                         m = 0
  516.                         middleplane_destroylist.pop(0)                        
  517.    
  518.             for each in largeplane_group:
  519.                 if each._collide:
  520.                     large_down_sound.play()
  521.                     largeplane_group.remove(each)
  522.                     allplanes_group.remove(each)
  523.                     index1 = largeplane_list.index(each)
  524.                     largeplane_destroylist.append(largeplane_list.pop(index1))
  525.                     largeplane_hitlist.pop(index1)
  526.                 screen.blit(each._image,each.rect)
  527.                 each.blood(screen)
  528.                
  529.             if largeplane_destroylist:
  530.                 vol = largeplane_destroylist[0]
  531.                 if not flag_bullet % 3:
  532.                     down = [vol._down1,vol._down2,vol._down3,vol._down4,vol._down5,vol._down6]
  533.                     screen.blit(down[i],vol.rect)
  534.                     n += 1
  535.                     if not n % 6:
  536.                         n = 0
  537.                         largeplane_destroylist.pop(0)

  538.         if not myplane._live:
  539.             screen.blit(bg_image,(0,0))
  540.             screen.blit(bomb_image,(0,643))
  541.             screen.blit(scores,(0,0))
  542.             screen.blit(num1,num1_rect)
  543.             screen.blit(again_image,again_image_rect)
  544.             screen.blit(gameover_image,gameover_image_rect)
  545.             plane_list_init(allplanes_group,smallplane_group,middleplane_group,largeplane_group,\
  546.                         smallplane_list,middleplane_list,largeplane_list,middleplane_hitlist,largeplane_hitlist )
  547.             bullet_group.empty()
  548.             bullet_list.clear()
  549.             pos = py.mouse.get_pos()
  550.             smallplane_destroylist.clear()
  551.             middleplane_destroylist.clear()
  552.             largeplane_destroylist.clear()
  553.             supply_bomb,supply_bullet,supply_get,supply_bullet_time,supply_time= 0,0,0,0,0
  554.             
  555.         #暂停
  556.         pause1 = py.image.load(pause_name_1).convert_alpha()
  557.         pause2 = py.image.load(pause_name_2).convert_alpha()
  558.         pause1_rect = pause1.get_rect()
  559.         pause1_rect.right,pause1_rect.top = 480,0
  560.         pause_pos = py.mouse.get_pos()
  561.         resume1 = py.image.load(resume_name_1).convert_alpha()
  562.         resume2 = py.image.load(resume_name_2).convert_alpha()
  563.         
  564.         if flag_pause:
  565.             if pause1_rect.left <= pause_pos[0] <= 480 and \
  566.                0 <= pause_pos[1] <= pause1_rect.height:
  567.                screen.blit(pause2,pause1_rect)
  568.             else:
  569.                screen.blit(pause1,pause1_rect)
  570.         if flag_resume:
  571.             flag_pause = 0
  572.             screen.blit(bg_image,(0,0))
  573.             if pause1_rect.left <= pause_pos[0] <= 480 and \
  574.                0 <= pause_pos[1] <= pause1_rect.height:
  575.                screen.blit(resume2,pause1_rect)   
  576.             else:
  577.                screen.blit(resume1,pause1_rect)            
  578.         
  579.         
  580.         flag_bullet += 1
  581.         flag_destroyplane += 1
  582.         if flag_bullet == 90:
  583.             flag_bullet = 0
  584.         if flag_destroyplane == 90:
  585.             flag_destroyplane = 0
  586.       
  587.         py.display.flip()
  588.         clock.tick(60)
  589. if __name__ == '__main__':
  590.     try:
  591.         main()
  592.     except SystemExit:
  593.         pass
  594.     except:
  595.         traceback.print_exc()
  596.         py.quit()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-31 21:53:56 | 显示全部楼层
  1. import pygame
  2. import sys
  3. import traceback
  4. import myplane
  5. import enemy
  6. import bullet
  7. import supply

  8. from pygame.locals import *
  9. from random import *

  10. pygame.init()
  11. pygame.mixer.init()

  12. bg_size = width, height = 480, 700
  13. screen = pygame.display.set_mode(bg_size)
  14. pygame.display.set_caption("飞机大战")

  15. background = pygame.image.load("images/background.png").convert()

  16. BLACK = (0, 0, 0)
  17. WHITE = (255, 255, 255)
  18. GREEN = (0, 255, 0)
  19. RED = (255, 0, 0)

  20. # 载入游戏音乐
  21. pygame.mixer.music.load("sound/game_music.ogg")
  22. pygame.mixer.music.set_volume(0.2)
  23. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  24. bullet_sound.set_volume(0.2)
  25. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  26. bomb_sound.set_volume(0.2)
  27. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  28. supply_sound.set_volume(0.2)
  29. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  30. get_bomb_sound.set_volume(0.2)
  31. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  32. get_bullet_sound.set_volume(0.2)
  33. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  34. upgrade_sound.set_volume(0.2)
  35. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  36. enemy3_fly_sound.set_volume(0.2)
  37. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  38. enemy1_down_sound.set_volume(0.2)
  39. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  40. enemy2_down_sound.set_volume(0.2)
  41. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  42. enemy3_down_sound.set_volume(0.5)
  43. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  44. me_down_sound.set_volume(0.2)


  45. def add_small_enemies(group1, group2, num):
  46.     for i in range(num):
  47.         e1 = enemy.SmallEnemy(bg_size)
  48.         group1.add(e1)
  49.         group2.add(e1)

  50. def add_mid_enemies(group1, group2, num):
  51.     for i in range(num):
  52.         e2 = enemy.MidEnemy(bg_size)
  53.         group1.add(e2)
  54.         group2.add(e2)

  55. def add_big_enemies(group1, group2, num):
  56.     for i in range(num):
  57.         e3 = enemy.BigEnemy(bg_size)
  58.         group1.add(e3)
  59.         group2.add(e3)

  60. def inc_speed(target, inc):
  61.     for each in target:
  62.         each.speed += inc

  63. def main():
  64.     pygame.mixer.music.play(-1)

  65.     # 生成我方飞机
  66.     me = myplane.MyPlane(bg_size)

  67.     enemies = pygame.sprite.Group()

  68.     # 生成敌方小型飞机
  69.     small_enemies = pygame.sprite.Group()
  70.     add_small_enemies(small_enemies, enemies, 15)

  71.     # 生成敌方中型飞机
  72.     mid_enemies = pygame.sprite.Group()
  73.     add_mid_enemies(mid_enemies, enemies, 4)

  74.     # 生成敌方大型飞机
  75.     big_enemies = pygame.sprite.Group()
  76.     add_big_enemies(big_enemies, enemies, 2)

  77.     # 生成普通子弹
  78.     bullet1 = []
  79.     bullet1_index = 0
  80.     BULLET1_NUM = 4
  81.     for i in range(BULLET1_NUM):
  82.         bullet1.append(bullet.Bullet1(me.rect.midtop))

  83.     # 生成超级子弹
  84.     bullet2 = []
  85.     bullet2_index = 0
  86.     BULLET2_NUM = 8
  87.     for i in range(BULLET2_NUM//2):
  88.         bullet2.append(bullet.Bullet2((me.rect.centerx-33, me.rect.centery)))
  89.         bullet2.append(bullet.Bullet2((me.rect.centerx+30, me.rect.centery)))

  90.     clock = pygame.time.Clock()

  91.     # 中弹图片索引
  92.     e1_destroy_index = 0
  93.     e2_destroy_index = 0
  94.     e3_destroy_index = 0
  95.     me_destroy_index = 0

  96.     # 统计得分
  97.     score = 0
  98.     score_font = pygame.font.Font("font/font.ttf", 36)

  99.     # 标志是否暂停游戏
  100.     paused = False
  101.     pause_nor_image = pygame.image.load("images/pause_nor.png").convert_alpha()
  102.     pause_pressed_image = pygame.image.load("images/pause_pressed.png").convert_alpha()
  103.     resume_nor_image = pygame.image.load("images/resume_nor.png").convert_alpha()
  104.     resume_pressed_image = pygame.image.load("images/resume_pressed.png").convert_alpha()
  105.     paused_rect = pause_nor_image.get_rect()
  106.     paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
  107.     paused_image = pause_nor_image

  108.     # 设置难度级别
  109.     level = 1

  110.     # 全屏炸弹
  111.     bomb_image = pygame.image.load("images/bomb.png").convert_alpha()
  112.     bomb_rect = bomb_image.get_rect()
  113.     bomb_font = pygame.font.Font("font/font.ttf", 48)
  114.     bomb_num = 3

  115.     # 每20秒发放一个补给包
  116.     bullet_supply = supply.Bullet_Supply(bg_size)
  117.     bomb_supply = supply.Bomb_Supply(bg_size)
  118.     SUPPLY_TIME = USEREVENT
  119.     pygame.time.set_timer(SUPPLY_TIME, 20 * 1000)

  120.     # 超级子弹定时器
  121.     DOUBLE_BULLET_TIME = USEREVENT + 1

  122.     # 标志是否使用超级子弹
  123.     is_double_bullet = False

  124.     # 解除我方无敌状态定时器
  125.     INVINCIBLE_TIME = USEREVENT + 2

  126.     # 生命数量
  127.     life_image = pygame.image.load("images/life.png").convert_alpha()
  128.     life_rect = life_image.get_rect()
  129.     life_num = 3

  130.     # 用于阻止重复打开记录文件
  131.     recorded = False

  132.     # 游戏结束画面
  133.     gameover_font = pygame.font.Font("font/font.TTF", 48)
  134.     again_image = pygame.image.load("images/again.png").convert_alpha()
  135.     again_rect = again_image.get_rect()
  136.     gameover_image = pygame.image.load("images/gameover.png").convert_alpha()
  137.     gameover_rect = gameover_image.get_rect()

  138.     # 用于切换图片
  139.     switch_image = True

  140.     # 用于延迟
  141.     delay = 100

  142.     running = True

  143.     while running:
  144.         for event in pygame.event.get():
  145.             if event.type == QUIT:
  146.                 pygame.quit()
  147.                 sys.exit()

  148.             elif event.type == MOUSEBUTTONDOWN:
  149.                 if event.button == 1 and paused_rect.collidepoint(event.pos):
  150.                     paused = not paused
  151.                     if paused:
  152.                         pygame.time.set_timer(SUPPLY_TIME, 0)
  153.                         pygame.mixer.music.pause()
  154.                         pygame.mixer.pause()
  155.                     else:
  156.                         pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
  157.                         pygame.mixer.music.unpause()
  158.                         pygame.mixer.unpause()

  159.             elif event.type == MOUSEMOTION:
  160.                 if paused_rect.collidepoint(event.pos):
  161.                     if paused:
  162.                         paused_image = resume_pressed_image
  163.                     else:
  164.                         paused_image = pause_pressed_image
  165.                 else:
  166.                     if paused:
  167.                         paused_image = resume_nor_image
  168.                     else:
  169.                        paused_image = pause_nor_image

  170.             elif event.type == KEYDOWN:
  171.                 if event.key == K_SPACE:
  172.                     if bomb_num:
  173.                         bomb_num -= 1
  174.                         bomb_sound.play()
  175.                         for each in enemies:
  176.                             if each.rect.bottom > 0:
  177.                                 each.active = False

  178.             elif event.type == SUPPLY_TIME:
  179.                 supply_sound.play()
  180.                 if choice([True, False]):
  181.                     bomb_supply.reset()
  182.                 else:
  183.                     bullet_supply.reset()

  184.             elif event.type == DOUBLE_BULLET_TIME:
  185.                 is_double_bullet = False
  186.                 pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)

  187.             elif event.type == INVINCIBLE_TIME:
  188.                 me.invincible = False
  189.                 pygame.time.set_timer(INVINCIBLE_TIME, 0)
  190.                         

  191.         # 根据用户的得分增加难度
  192.         if level == 1 and score > 50000:
  193.             level = 2
  194.             upgrade_sound.play()
  195.             # 增加3架小型敌机、2架中型敌机和1架大型敌机
  196.             add_small_enemies(small_enemies, enemies, 3)
  197.             add_mid_enemies(mid_enemies, enemies, 2)
  198.             add_big_enemies(big_enemies, enemies, 1)
  199.             # 提升小型敌机的速度
  200.             inc_speed(small_enemies, 1)
  201.         elif level == 2 and score > 300000:
  202.             level = 3
  203.             upgrade_sound.play()
  204.             # 增加5架小型敌机、3架中型敌机和2架大型敌机
  205.             add_small_enemies(small_enemies, enemies, 5)
  206.             add_mid_enemies(mid_enemies, enemies, 3)
  207.             add_big_enemies(big_enemies, enemies, 2)
  208.             # 提升小型敌机的速度
  209.             inc_speed(small_enemies, 1)
  210.             inc_speed(mid_enemies, 1)
  211.         elif level == 3 and score > 600000:
  212.             level = 4
  213.             upgrade_sound.play()
  214.             # 增加5架小型敌机、3架中型敌机和2架大型敌机
  215.             add_small_enemies(small_enemies, enemies, 5)
  216.             add_mid_enemies(mid_enemies, enemies, 3)
  217.             add_big_enemies(big_enemies, enemies, 2)
  218.             # 提升小型敌机的速度
  219.             inc_speed(small_enemies, 1)
  220.             inc_speed(mid_enemies, 1)
  221.         elif level == 4 and score > 1000000:
  222.             level = 5
  223.             upgrade_sound.play()
  224.             # 增加5架小型敌机、3架中型敌机和2架大型敌机
  225.             add_small_enemies(small_enemies, enemies, 5)
  226.             add_mid_enemies(mid_enemies, enemies, 3)
  227.             add_big_enemies(big_enemies, enemies, 2)
  228.             # 提升小型敌机的速度
  229.             inc_speed(small_enemies, 1)
  230.             inc_speed(mid_enemies, 1)
  231.             

  232.         screen.blit(background, (0, 0))
  233.                
  234.         if life_num and not paused:
  235.             # 检测用户的键盘操作
  236.             key_pressed = pygame.key.get_pressed()

  237.             if key_pressed[K_w] or key_pressed[K_UP]:
  238.                 me.moveUp()
  239.             if key_pressed[K_s] or key_pressed[K_DOWN]:
  240.                 me.moveDown()
  241.             if key_pressed[K_a] or key_pressed[K_LEFT]:
  242.                 me.moveLeft()
  243.             if key_pressed[K_d] or key_pressed[K_RIGHT]:
  244.                 me.moveRight()

  245.             # 绘制全屏炸弹补给并检测是否获得
  246.             if bomb_supply.active:
  247.                 bomb_supply.move()
  248.                 screen.blit(bomb_supply.image, bomb_supply.rect)
  249.                 if pygame.sprite.collide_mask(bomb_supply, me):
  250.                     get_bomb_sound.play()
  251.                     bomb_num += 1
  252.                     bomb_supply.active = False

  253.             # 绘制超级子弹补给并检测是否获得
  254.             if bullet_supply.active:
  255.                 bullet_supply.move()
  256.                 screen.blit(bullet_supply.image, bullet_supply.rect)
  257.                 if pygame.sprite.collide_mask(bullet_supply, me):
  258.                     get_bullet_sound.play()
  259.                     is_double_bullet = True
  260.                     pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
  261.                     bullet_supply.active = False

  262.             # 发射子弹
  263.             if not(delay % 10):
  264.                 bullet_sound.play()
  265.                 if is_double_bullet:
  266.                     bullets = bullet2
  267.                     bullets[bullet2_index].reset((me.rect.centerx-33, me.rect.centery))
  268.                     bullets[bullet2_index+1].reset((me.rect.centerx+30, me.rect.centery))
  269.                     bullet2_index = (bullet2_index + 2) % BULLET2_NUM
  270.                 else:
  271.                     bullets = bullet1
  272.                     bullets[bullet1_index].reset(me.rect.midtop)
  273.                     bullet1_index = (bullet1_index + 1) % BULLET1_NUM

  274.                
  275.             # 检测子弹是否击中敌机
  276.             for b in bullets:
  277.                 if b.active:
  278.                     b.move()
  279.                     screen.blit(b.image, b.rect)
  280.                     enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
  281.                     if enemy_hit:
  282.                         b.active = False
  283.                         for e in enemy_hit:
  284.                             if e in mid_enemies or e in big_enemies:
  285.                                 e.hit = True
  286.                                 e.energy -= 1
  287.                                 if e.energy == 0:
  288.                                     e.active = False
  289.                             else:
  290.                                 e.active = False
  291.             
  292.             # 绘制大型敌机
  293.             for each in big_enemies:
  294.                 if each.active:
  295.                     each.move()
  296.                     if each.hit:
  297.                         screen.blit(each.image_hit, each.rect)
  298.                         each.hit = False
  299.                     else:
  300.                         if switch_image:
  301.                             screen.blit(each.image1, each.rect)
  302.                         else:
  303.                             screen.blit(each.image2, each.rect)

  304.                     # 绘制血槽
  305.                     pygame.draw.line(screen, BLACK, \
  306.                                      (each.rect.left, each.rect.top - 5), \
  307.                                      (each.rect.right, each.rect.top - 5), \
  308.                                      2)
  309.                     # 当生命大于20%显示绿色,否则显示红色
  310.                     energy_remain = each.energy / enemy.BigEnemy.energy
  311.                     if energy_remain > 0.2:
  312.                         energy_color = GREEN
  313.                     else:
  314.                         energy_color = RED
  315.                     pygame.draw.line(screen, energy_color, \
  316.                                      (each.rect.left, each.rect.top - 5), \
  317.                                      (each.rect.left + each.rect.width * energy_remain, \
  318.                                       each.rect.top - 5), 2)
  319.                         
  320.                     # 即将出现在画面中,播放音效
  321.                     if each.rect.bottom == -50:
  322.                         enemy3_fly_sound.play(-1)
  323.                 else:
  324.                     # 毁灭
  325.                     if not(delay % 3):
  326.                         if e3_destroy_index == 0:
  327.                             enemy3_down_sound.play()
  328.                         screen.blit(each.destroy_images[e3_destroy_index], each.rect)
  329.                         e3_destroy_index = (e3_destroy_index + 1) % 6
  330.                         if e3_destroy_index == 0:
  331.                             enemy3_fly_sound.stop()
  332.                             score += 10000
  333.                             each.reset()

  334.             # 绘制中型敌机:
  335.             for each in mid_enemies:
  336.                 if each.active:
  337.                     each.move()

  338.                     if each.hit:
  339.                         screen.blit(each.image_hit, each.rect)
  340.                         each.hit = False
  341.                     else:
  342.                         screen.blit(each.image, each.rect)

  343.                     # 绘制血槽
  344.                     pygame.draw.line(screen, BLACK, \
  345.                                      (each.rect.left, each.rect.top - 5), \
  346.                                      (each.rect.right, each.rect.top - 5), \
  347.                                      2)
  348.                     # 当生命大于20%显示绿色,否则显示红色
  349.                     energy_remain = each.energy / enemy.MidEnemy.energy
  350.                     if energy_remain > 0.2:
  351.                         energy_color = GREEN
  352.                     else:
  353.                         energy_color = RED
  354.                     pygame.draw.line(screen, energy_color, \
  355.                                      (each.rect.left, each.rect.top - 5), \
  356.                                      (each.rect.left + each.rect.width * energy_remain, \
  357.                                       each.rect.top - 5), 2)
  358.                 else:
  359.                     # 毁灭
  360.                     if not(delay % 3):
  361.                         if e2_destroy_index == 0:
  362.                             enemy2_down_sound.play()
  363.                         screen.blit(each.destroy_images[e2_destroy_index], each.rect)
  364.                         e2_destroy_index = (e2_destroy_index + 1) % 4
  365.                         if e2_destroy_index == 0:
  366.                             score += 6000
  367.                             each.reset()

  368.             # 绘制小型敌机:
  369.             for each in small_enemies:
  370.                 if each.active:
  371.                     each.move()
  372.                     screen.blit(each.image, each.rect)
  373.                 else:
  374.                     # 毁灭
  375.                     if not(delay % 3):
  376.                         if e1_destroy_index == 0:
  377.                             enemy1_down_sound.play()
  378.                         screen.blit(each.destroy_images[e1_destroy_index], each.rect)
  379.                         e1_destroy_index = (e1_destroy_index + 1) % 4
  380.                         if e1_destroy_index == 0:
  381.                             score += 1000
  382.                             each.reset()

  383.             # 检测我方飞机是否被撞
  384.             enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
  385.             if enemies_down and not me.invincible:
  386.                 me.active = False
  387.                 for e in enemies_down:
  388.                     e.active = False
  389.             
  390.             # 绘制我方飞机
  391.             if me.active:
  392.                 if switch_image:
  393.                     screen.blit(me.image1, me.rect)
  394.                 else:
  395.                     screen.blit(me.image2, me.rect)
  396.             else:
  397.                 # 毁灭
  398.                 if not(delay % 3):
  399.                     if me_destroy_index == 0:
  400.                         me_down_sound.play()
  401.                     screen.blit(me.destroy_images[me_destroy_index], me.rect)
  402.                     me_destroy_index = (me_destroy_index + 1) % 4
  403.                     if me_destroy_index == 0:
  404.                         life_num -= 1
  405.                         me.reset()
  406.                         pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)

  407.             # 绘制全屏炸弹数量
  408.             bomb_text = bomb_font.render(" X %d" % bomb_num, True, WHITE)
  409.             text_rect = bomb_text.get_rect()
  410.             screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
  411.             screen.blit(bomb_text, (20 + bomb_rect.width, height - 5 - text_rect.height))

  412.             # 绘制剩余生命数量
  413.             if life_num:
  414.                 for i in range(life_num):
  415.                     screen.blit(life_image, \
  416.                                 (width-10-(i+1)*life_rect.width, \
  417.                                  height-10-life_rect.height))

  418.             # 绘制得分
  419.             score_text = score_font.render("Score : %s" % str(score), True, WHITE)
  420.             screen.blit(score_text, (10, 5))

  421.         # 绘制游戏结束画面
  422.         elif life_num == 0:
  423.             # 背景音乐停止
  424.             pygame.mixer.music.stop()

  425.             # 停止全部音效
  426.             pygame.mixer.stop()

  427.             # 停止发放补给
  428.             pygame.time.set_timer(SUPPLY_TIME, 0)

  429.             if not recorded:
  430.                 recorded = True
  431.                 # 读取历史最高得分
  432.                 with open("record.txt", "r") as f:
  433.                     record_score = int(f.read())

  434.                 # 如果玩家得分高于历史最高得分,则存档
  435.                 if score > record_score:
  436.                     with open("record.txt", "w") as f:
  437.                         f.write(str(score))

  438.             # 绘制结束画面
  439.             record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
  440.             screen.blit(record_score_text, (50, 50))
  441.             
  442.             gameover_text1 = gameover_font.render("Your Score", True, (255, 255, 255))
  443.             gameover_text1_rect = gameover_text1.get_rect()
  444.             gameover_text1_rect.left, gameover_text1_rect.top = \
  445.                                  (width - gameover_text1_rect.width) // 2, height // 3
  446.             screen.blit(gameover_text1, gameover_text1_rect)
  447.             
  448.             gameover_text2 = gameover_font.render(str(score), True, (255, 255, 255))
  449.             gameover_text2_rect = gameover_text2.get_rect()
  450.             gameover_text2_rect.left, gameover_text2_rect.top = \
  451.                                  (width - gameover_text2_rect.width) // 2, \
  452.                                  gameover_text1_rect.bottom + 10
  453.             screen.blit(gameover_text2, gameover_text2_rect)

  454.             again_rect.left, again_rect.top = \
  455.                              (width - again_rect.width) // 2, \
  456.                              gameover_text2_rect.bottom + 50
  457.             screen.blit(again_image, again_rect)

  458.             gameover_rect.left, gameover_rect.top = \
  459.                                 (width - again_rect.width) // 2, \
  460.                                 again_rect.bottom + 10
  461.             screen.blit(gameover_image, gameover_rect)

  462.             # 检测用户的鼠标操作
  463.             # 如果用户按下鼠标左键
  464.             if pygame.mouse.get_pressed()[0]:
  465.                 # 获取鼠标坐标
  466.                 pos = pygame.mouse.get_pos()
  467.                 # 如果用户点击“重新开始”
  468.                 if again_rect.left < pos[0] < again_rect.right and \
  469.                    again_rect.top < pos[1] < again_rect.bottom:
  470.                     # 调用main函数,重新开始游戏
  471.                     main()
  472.                 # 如果用户点击“结束游戏”            
  473.                 elif gameover_rect.left < pos[0] < gameover_rect.right and \
  474.                      gameover_rect.top < pos[1] < gameover_rect.bottom:
  475.                     # 退出游戏
  476.                     pygame.quit()
  477.                     sys.exit()      

  478.         # 绘制暂停按钮
  479.         screen.blit(paused_image, paused_rect)

  480.         # 切换图片
  481.         if not(delay % 5):
  482.             switch_image = not switch_image

  483.         delay -= 1
  484.         if not delay:
  485.             delay = 100

  486.         pygame.display.flip()
  487.         clock.tick(60)
  488.         
  489. if __name__ == "__main__":
  490.     try:
  491.         main()
  492.     except SystemExit:
  493.         pass
  494.     except:
  495.         traceback.print_exc()
  496.         pygame.quit()
  497.         input()

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 15:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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