鱼C论坛

 找回密码
 立即注册
查看: 990|回复: 15

[已解决]飞机大战第2课以及三课自己复刻小甲鱼教学出现的问题,求大佬救命!!

[复制链接]
发表于 2020-4-8 23:06:30 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 python小鲨鱼 于 2020-4-8 23:10 编辑

其余enemy bullet myplane模块均与小甲鱼相同
出现的问题:
一:敌机被击毁之后音效重复播放,杂音很多
二:若是子弹击中敌机,我放飞机不移动的话,敌机一直显示击毁的图片和音效
三:程序运行一会之后,就算子弹没打中敌机,也会将敌机击毁




  1. import pygame
  2. import sys
  3. import traceback
  4. import myplane
  5. import enemy
  6. import bullet
  7. from pygame.locals import *

  8. # 初始化pygame,混音器
  9. pygame.init()
  10. pygame.mixer.init()

  11. # 初始化界面
  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. # 载入游戏音乐
  17. pygame.mixer.music.load("sound/game_music.ogg")
  18. pygame.mixer.music.set_volume(0.2)

  19. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  20. bullet_sound.set_volume(0.2)

  21. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  22. bomb_sound.set_volume(0.2)

  23. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  24. supply_sound.set_volume(0.2)

  25. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  26. get_bomb_sound.set_volume(0.2)

  27. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  28. get_bullet_sound.set_volume(0.2)

  29. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  30. upgrade_sound.set_volume(0.2)

  31. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  32. enemy3_fly_sound.set_volume(0.2)

  33. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  34. enemy1_down_sound.set_volume(0.2)

  35. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  36. enemy2_down_sound.set_volume(0.2)

  37. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  38. enemy3_down_sound.set_volume(0.5)

  39. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  40. me_down_sound.set_volume(0.2)


  41. def add_small_enemies(group1,group2,num):
  42.     for i in range(num):
  43.         e1 = enemy.SmallEnemy(bg_size)
  44.         group1.add(e1)
  45.         group2.add(e1)

  46. def add_mid_enemies(group1,group2,num):
  47.     for i in range(num):
  48.         e2 = enemy.MidEnemy(bg_size)
  49.         group1.add(e2)
  50.         group2.add(e2)

  51. def add_big_enemies(group1,group2,num):
  52.     for i in range(num):
  53.         e3 = enemy.BigEnemy(bg_size)
  54.         group1.add(e3)
  55.         group2.add(e3)
  56.         
  57.         

  58. # 定义主函数
  59. def main():
  60.     pygame.mixer.music.play(-1)

  61.     # 生成我方飞机
  62.     me = myplane.MyPlane(bg_size)

  63.     # 生成敌方飞机
  64.     enemies = pygame.sprite.Group()

  65.     # 生成敌方小型飞机
  66.     small_enemies = pygame.sprite.Group()
  67.     add_small_enemies(small_enemies,enemies,15)
  68.    
  69.     # 生成敌方中型飞机
  70.     mid_enemies = pygame.sprite.Group()
  71.     add_mid_enemies(mid_enemies,enemies,4)
  72.    
  73.     # 生成敌方大型飞机
  74.     big_enemies = pygame.sprite.Group()
  75.     add_big_enemies(big_enemies,enemies,2)

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

  82.         
  83.     clock = pygame.time.Clock()
  84.    

  85.     # 中弹图片索引
  86.     e1_destroy_index = 0
  87.     e2_destroy_index = 0
  88.     e3_destroy_index = 0
  89.     me_destroy_index = 0

  90.     # 用于切换图片
  91.     switch_image = True

  92.     # 用于延迟
  93.     delay = 100

  94.     running = True

  95.     while running:
  96.         for event in pygame.event.get():
  97.             if event.type == QUIT:
  98.                 pygame.quit()
  99.                 sys.exit()

  100.         # 检测用户的键盘操作
  101.         key_pressed = pygame.key.get_pressed()

  102.         if key_pressed[K_w] or key_pressed[K_UP]:
  103.             me.moveUp()
  104.         if key_pressed[K_s] or key_pressed[K_DOWN]:
  105.             me.moveDown()
  106.         if key_pressed[K_a] or key_pressed[K_LEFT]:
  107.             me.moveLeft()
  108.         if key_pressed[K_d] or key_pressed[K_RIGHT]:
  109.             me.moveRight()

  110.         # 绘制背景
  111.         screen.blit(background,(0,0))

  112.         
  113.         # 发射子弹
  114.         if not(delay % 10):
  115.             bullet1[bullet1_index].reset(me.rect.midtop)
  116.             bullet1_index = (bullet1_index + 1) % BULLET1_NUM


  117.         # 检测子弹是否击中敌机
  118.         for b in bullet1:
  119.             if b.active:
  120.                 b.move()
  121.                 screen.blit(b.image, b.rect)
  122.                 enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
  123.                 if enemy_hit:
  124.                     b.active = False
  125.                     for e in enemy_hit:
  126.                         e.active = False

  127.         # 绘制敌方大型飞机
  128.         for each in big_enemies:
  129.             if each.active:
  130.                 each.move()
  131.                 if switch_image:
  132.                     screen.blit(each.image1, each.rect)
  133.                 else:
  134.                     screen.blit(each.image2, each.rect)
  135.                 # 即将出现在界面时播放音效
  136.                 if each.rect.bottom == -50:
  137.                     enemy3_fly_sound.play(-1)
  138.             else:
  139.                 # 大型飞机毁灭
  140.                 if not(delay % 3):
  141.                     if e3_destroy_index == 0:
  142.                         enemy3_down_sound.play()
  143.                     screen.blit(each.destroy_imgaes[e3_destroy_index],each.rect)
  144.                     e3_destroy_index = (e3_destroy_index + 1) % 6
  145.                     if e3_destroy_index == 0:
  146.                         enemy3_down_sound.stop()
  147.                         each.reset()
  148.                                  
  149.                
  150.         # 绘制敌方中型飞机
  151.         for each in mid_enemies:
  152.             if each.active:
  153.                 each.move()
  154.                 screen.blit(each.image, each.rect)
  155.             else:
  156.                 # 中型飞机毁灭  
  157.                 if not(delay % 3):
  158.                     if e2_destroy_index == 0:
  159.                         enemy2_down_sound.play()
  160.                     screen.blit(each.destroy_imgaes[e2_destroy_index],each.rect)
  161.                     e2_destroy_index = (e2_destroy_index + 1) % 4
  162.                     if e2_destroy_index == 0:
  163.                         each.reset()
  164.                                  

  165.             
  166.         # 绘制敌方小型飞机
  167.         for each in small_enemies:
  168.             if each.active:
  169.                 each.move()
  170.                 screen.blit(each.image, each.rect)
  171.             else:
  172.                 # 小型飞机毁灭
  173.                 if not(delay % 3):
  174.                     if e1_destroy_index == 0:
  175.                         enemy1_down_sound.play()
  176.                     screen.blit(each.destroy_imgaes[e1_destroy_index],each.rect)
  177.                     e1_destroy_index = (e1_destroy_index + 1) % 4
  178.                     if e1_destroy_index == 0:
  179.                         each.reset()

  180.         # 检测我放飞机是否被撞
  181.         enemies_down = pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)
  182.         if enemies_down:
  183.             me.active = False
  184.             for e in enemies_down:
  185.                 e.active = False
  186.         
  187.             
  188.         # 绘制我方飞机
  189.         if me.active:
  190.             switch_image = not switch_image
  191.             if switch_image:
  192.                 screen.blit(me.image1, me.rect)
  193.             else:
  194.                 screen.blit(me.image2, me.rect)
  195.         else:
  196.             # 我方飞机毁灭
  197.             if not(delay % 3):
  198.                 if me_destroy_index == 0:
  199.                     me_down_sound.play()
  200.                 screen.blit(me.destroy_imgaes[me_destroy_index],me.rect)
  201.                 me_destroy_index = (me_destroy_index + 1) % 4
  202.                 if me_destroy_index == 0:
  203.                     print("游戏结束")
  204.                     running = False

  205.         # 切换图片
  206.         if not(delay % 5):
  207.             switch_image = not switch_image
  208.         
  209.         delay -= 1
  210.         if not delay:
  211.             delay = 100
  212.         
  213.         pygame.display.flip()
  214.         clock.tick(60)
  215.         
  216. if __name__ == "__main__":
  217.     try:
  218.         main()
  219.     except SystemExit:
  220.         pass
  221.     except:
  222.         traceback.print_exc()
  223.         pygame.quit()
  224.         input()

复制代码
最佳答案
2020-4-9 06:21:30
本帖最后由 编程鱼C 于 2020-4-9 09:02 编辑
  1. # main.py
  2. import pygame
  3. import sys
  4. import traceback
  5. import myplane
  6. import enemy
  7. import bullet
  8. import supply

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

  11. pygame.init()
  12. pygame.mixer.init()

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

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

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

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


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

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

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

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

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

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

  68.     enemies = pygame.sprite.Group()

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

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

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

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

  84.    

  85.    
  86.     # 用于切换图片
  87.     switch_image = True

  88.     # 用于延迟
  89.     delay = 100

  90.     running = True

  91.     while running:
  92.         for event in pygame.event.get():
  93.             if event.type == QUIT:
  94.                 pygame.quit()
  95.                 sys.exit()

  96.             elif event.type == MOUSEBUTTONDOWN:
  97.                 if event.button == 1 and paused_rect.collidepoint(event.pos):
  98.                     paused = not paused
  99.                     if paused:
  100.                         pygame.time.set_timer(SUPPLY_TIME, 0)
  101.                         pygame.mixer.music.pause()
  102.                         pygame.mixer.pause()
  103.                     else:
  104.                         pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
  105.                         pygame.mixer.music.unpause()
  106.                         pygame.mixer.unpause()

  107.             elif event.type == MOUSEMOTION:
  108.                 if paused_rect.collidepoint(event.pos):
  109.                     if paused:
  110.                         paused_image = resume_pressed_image
  111.                     else:
  112.                         paused_image = pause_pressed_image
  113.                 else:
  114.                     if paused:
  115.                         paused_image = resume_nor_image
  116.                     else:
  117.                        paused_image = pause_nor_image

  118.             elif event.type == KEYDOWN:
  119.                 if event.key == K_SPACE:
  120.                     if bomb_num:
  121.                         bomb_num -= 1
  122.                         bomb_sound.play()
  123.                         for each in enemies:
  124.                             if each.rect.bottom > 0:
  125.                                 each.active = False

  126.             elif event.type == SUPPLY_TIME:
  127.                 supply_sound.play()
  128.                 if choice([True, False]):
  129.                     bomb_supply.reset()
  130.                 else:
  131.                     bullet_supply.reset()

  132.             elif event.type == DOUBLE_BULLET_TIME:
  133.                 is_double_bullet = False
  134.                 pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)

  135.             elif event.type == INVINCIBLE_TIME:
  136.                 me.invincible = False
  137.                 pygame.time.set_timer(INVINCIBLE_TIME, 0)
  138.                         

  139.         
  140.                
  141.         if life_num and not paused:
  142.             # 检测用户的键盘操作
  143.             key_pressed = pygame.key.get_pressed()

  144.             if key_pressed[K_w] or key_pressed[K_UP]:
  145.                 me.moveUp()
  146.             if key_pressed[K_s] or key_pressed[K_DOWN]:
  147.                 me.moveDown()
  148.             if key_pressed[K_a] or key_pressed[K_LEFT]:
  149.                 me.moveLeft()
  150.             if key_pressed[K_d] or key_pressed[K_RIGHT]:
  151.                 me.moveRight()

  152.            

  153.             # 发射子弹
  154.             if not(delay % 10):
  155.                 bullet_sound.play()
  156.                 if is_double_bullet:
  157.                     bullets = bullet2
  158.                     bullets[bullet2_index].reset((me.rect.centerx-33, me.rect.centery))
  159.                     bullets[bullet2_index+1].reset((me.rect.centerx+30, me.rect.centery))
  160.                     bullet2_index = (bullet2_index + 2) % BULLET2_NUM
  161.                 else:
  162.                     bullets = bullet1
  163.                     bullets[bullet1_index].reset(me.rect.midtop)
  164.                     bullet1_index = (bullet1_index + 1) % BULLET1_NUM

  165.                
  166.             # 检测子弹是否击中敌机
  167.             for b in bullets:
  168.                 if b.active:
  169.                     b.move()
  170.                     screen.blit(b.image, b.rect)
  171.                     enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
  172.                     if enemy_hit:
  173.                         b.active = False
  174.                         for e in enemy_hit:
  175.                             if e in mid_enemies or e in big_enemies:
  176.                                 e.hit = True
  177.                                 e.energy -= 1
  178.                                 if e.energy == 0:
  179.                                     e.active = False
  180.                             else:
  181.                                 e.active = False
  182.             
  183.             # 绘制大型敌机
  184.             for each in big_enemies:
  185.                 if each.active:
  186.                     each.move()
  187.                     if each.hit:
  188.                         screen.blit(each.image_hit, each.rect)
  189.                         each.hit = False
  190.                     else:
  191.                         if switch_image:
  192.                             screen.blit(each.image1, each.rect)
  193.                         else:
  194.                             screen.blit(each.image2, each.rect)

  195.                   
  196.                     # 即将出现在画面中,播放音效
  197.                     if each.rect.bottom == -50:
  198.                         enemy3_fly_sound.play(-1)
  199.                 else:
  200.                   
  201.             # 绘制中型敌机:
  202.             for each in mid_enemies:
  203.                 if each.active:
  204.                     each.move()

  205.                     if each.hit:
  206.                         screen.blit(each.image_hit, each.rect)
  207.                         each.hit = False
  208.                     else:
  209.                         screen.blit(each.image, each.rect)

  210.                     

  211.             # 绘制小型敌机:
  212.             for each in small_enemies:
  213.                 if each.active:
  214.                     each.move()
  215.                     screen.blit(each.image, each.rect)
  216.                 else:
  217.                     

  218.             # 检测我方飞机是否被撞
  219.             enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
  220.             if enemies_down and not me.invincible:
  221.                 me.active = False
  222.                 for e in enemies_down:
  223.                     e.active = False
  224.             
  225.             # 绘制我方飞机
  226.             if me.active:
  227.                 if switch_image:
  228.                     screen.blit(me.image1, me.rect)
  229.                 else:
  230.                     screen.blit(me.image2, me.rect)
  231.             else:
  232.                 # 毁灭
  233.                 if not(delay % 3):
  234.                     if me_destroy_index == 0:
  235.                         me_down_sound.play()
  236.                     screen.blit(me.destroy_images[me_destroy_index], me.rect)
  237.                     me_destroy_index = (me_destroy_index + 1) % 4
  238.                     if me_destroy_index == 0:
  239.                         life_num -= 1
  240.                         me.reset()
  241.                         pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)

  242.            
  243.          
  244.         # 切换图片
  245.         if not(delay % 5):
  246.             switch_image = not switch_image

  247.         delay -= 1
  248.         if not delay:
  249.             delay = 100

  250.         pygame.display.flip()
  251.         clock.tick(60)
  252.         
  253. if __name__ == "__main__":
  254.     try:
  255.         main()
  256.     except SystemExit:
  257.         pass
  258.     except:
  259.         traceback.print_exc()
  260.         pygame.quit()
  261.         input()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-9 06:21:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 编程鱼C 于 2020-4-9 09:02 编辑
  1. # main.py
  2. import pygame
  3. import sys
  4. import traceback
  5. import myplane
  6. import enemy
  7. import bullet
  8. import supply

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

  11. pygame.init()
  12. pygame.mixer.init()

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

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

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

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


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

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

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

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

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

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

  68.     enemies = pygame.sprite.Group()

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

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

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

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

  84.    

  85.    
  86.     # 用于切换图片
  87.     switch_image = True

  88.     # 用于延迟
  89.     delay = 100

  90.     running = True

  91.     while running:
  92.         for event in pygame.event.get():
  93.             if event.type == QUIT:
  94.                 pygame.quit()
  95.                 sys.exit()

  96.             elif event.type == MOUSEBUTTONDOWN:
  97.                 if event.button == 1 and paused_rect.collidepoint(event.pos):
  98.                     paused = not paused
  99.                     if paused:
  100.                         pygame.time.set_timer(SUPPLY_TIME, 0)
  101.                         pygame.mixer.music.pause()
  102.                         pygame.mixer.pause()
  103.                     else:
  104.                         pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
  105.                         pygame.mixer.music.unpause()
  106.                         pygame.mixer.unpause()

  107.             elif event.type == MOUSEMOTION:
  108.                 if paused_rect.collidepoint(event.pos):
  109.                     if paused:
  110.                         paused_image = resume_pressed_image
  111.                     else:
  112.                         paused_image = pause_pressed_image
  113.                 else:
  114.                     if paused:
  115.                         paused_image = resume_nor_image
  116.                     else:
  117.                        paused_image = pause_nor_image

  118.             elif event.type == KEYDOWN:
  119.                 if event.key == K_SPACE:
  120.                     if bomb_num:
  121.                         bomb_num -= 1
  122.                         bomb_sound.play()
  123.                         for each in enemies:
  124.                             if each.rect.bottom > 0:
  125.                                 each.active = False

  126.             elif event.type == SUPPLY_TIME:
  127.                 supply_sound.play()
  128.                 if choice([True, False]):
  129.                     bomb_supply.reset()
  130.                 else:
  131.                     bullet_supply.reset()

  132.             elif event.type == DOUBLE_BULLET_TIME:
  133.                 is_double_bullet = False
  134.                 pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)

  135.             elif event.type == INVINCIBLE_TIME:
  136.                 me.invincible = False
  137.                 pygame.time.set_timer(INVINCIBLE_TIME, 0)
  138.                         

  139.         
  140.                
  141.         if life_num and not paused:
  142.             # 检测用户的键盘操作
  143.             key_pressed = pygame.key.get_pressed()

  144.             if key_pressed[K_w] or key_pressed[K_UP]:
  145.                 me.moveUp()
  146.             if key_pressed[K_s] or key_pressed[K_DOWN]:
  147.                 me.moveDown()
  148.             if key_pressed[K_a] or key_pressed[K_LEFT]:
  149.                 me.moveLeft()
  150.             if key_pressed[K_d] or key_pressed[K_RIGHT]:
  151.                 me.moveRight()

  152.            

  153.             # 发射子弹
  154.             if not(delay % 10):
  155.                 bullet_sound.play()
  156.                 if is_double_bullet:
  157.                     bullets = bullet2
  158.                     bullets[bullet2_index].reset((me.rect.centerx-33, me.rect.centery))
  159.                     bullets[bullet2_index+1].reset((me.rect.centerx+30, me.rect.centery))
  160.                     bullet2_index = (bullet2_index + 2) % BULLET2_NUM
  161.                 else:
  162.                     bullets = bullet1
  163.                     bullets[bullet1_index].reset(me.rect.midtop)
  164.                     bullet1_index = (bullet1_index + 1) % BULLET1_NUM

  165.                
  166.             # 检测子弹是否击中敌机
  167.             for b in bullets:
  168.                 if b.active:
  169.                     b.move()
  170.                     screen.blit(b.image, b.rect)
  171.                     enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
  172.                     if enemy_hit:
  173.                         b.active = False
  174.                         for e in enemy_hit:
  175.                             if e in mid_enemies or e in big_enemies:
  176.                                 e.hit = True
  177.                                 e.energy -= 1
  178.                                 if e.energy == 0:
  179.                                     e.active = False
  180.                             else:
  181.                                 e.active = False
  182.             
  183.             # 绘制大型敌机
  184.             for each in big_enemies:
  185.                 if each.active:
  186.                     each.move()
  187.                     if each.hit:
  188.                         screen.blit(each.image_hit, each.rect)
  189.                         each.hit = False
  190.                     else:
  191.                         if switch_image:
  192.                             screen.blit(each.image1, each.rect)
  193.                         else:
  194.                             screen.blit(each.image2, each.rect)

  195.                   
  196.                     # 即将出现在画面中,播放音效
  197.                     if each.rect.bottom == -50:
  198.                         enemy3_fly_sound.play(-1)
  199.                 else:
  200.                   
  201.             # 绘制中型敌机:
  202.             for each in mid_enemies:
  203.                 if each.active:
  204.                     each.move()

  205.                     if each.hit:
  206.                         screen.blit(each.image_hit, each.rect)
  207.                         each.hit = False
  208.                     else:
  209.                         screen.blit(each.image, each.rect)

  210.                     

  211.             # 绘制小型敌机:
  212.             for each in small_enemies:
  213.                 if each.active:
  214.                     each.move()
  215.                     screen.blit(each.image, each.rect)
  216.                 else:
  217.                     

  218.             # 检测我方飞机是否被撞
  219.             enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
  220.             if enemies_down and not me.invincible:
  221.                 me.active = False
  222.                 for e in enemies_down:
  223.                     e.active = False
  224.             
  225.             # 绘制我方飞机
  226.             if me.active:
  227.                 if switch_image:
  228.                     screen.blit(me.image1, me.rect)
  229.                 else:
  230.                     screen.blit(me.image2, me.rect)
  231.             else:
  232.                 # 毁灭
  233.                 if not(delay % 3):
  234.                     if me_destroy_index == 0:
  235.                         me_down_sound.play()
  236.                     screen.blit(me.destroy_images[me_destroy_index], me.rect)
  237.                     me_destroy_index = (me_destroy_index + 1) % 4
  238.                     if me_destroy_index == 0:
  239.                         life_num -= 1
  240.                         me.reset()
  241.                         pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)

  242.            
  243.          
  244.         # 切换图片
  245.         if not(delay % 5):
  246.             switch_image = not switch_image

  247.         delay -= 1
  248.         if not delay:
  249.             delay = 100

  250.         pygame.display.flip()
  251.         clock.tick(60)
  252.         
  253. if __name__ == "__main__":
  254.     try:
  255.         main()
  256.     except SystemExit:
  257.         pass
  258.     except:
  259.         traceback.print_exc()
  260.         pygame.quit()
  261.         input()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 08:07:59 | 显示全部楼层
老哥毅力可以啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 23:32:28 | 显示全部楼层
扁扁 发表于 2020-4-9 08:07
老哥毅力可以啊

才学没多久,还是好多不会,照猫画虎
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 23:33:15 | 显示全部楼层

你这个我改完之后运行,还是一样的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-10 00:15:27 | 显示全部楼层

自己在后面改代码中发现了自己的错误,敲代码的时候在敌方飞机的重置方法中没有把飞机的active属性设置为True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-10 00:16:44 | 显示全部楼层
在这没有设置敌机重置属性中的active属性

  1. import pygame
  2. from random import *

  3. class SmallEnemy(pygame.sprite.Sprite):
  4.     def __init__(self,bg_size):
  5.         pygame.sprite.Sprite.__init__(self)

  6.         self.image = pygame.image.load("images/enemy1.png").convert_alpha()
  7.         self.destroy_images = []
  8.         self.destroy_images.extend([\
  9.             pygame.image.load("images/enemy1_down1.png").convert_alpha(), \
  10.             pygame.image.load("images/enemy1_down2.png").convert_alpha(), \
  11.             pygame.image.load("images/enemy1_down3.png").convert_alpha(), \
  12.             pygame.image.load("images/enemy1_down4.png").convert_alpha() \
  13.             ])
  14.         self.rect = self.image.get_rect()
  15.         self.width, self.height = bg_size[0], bg_size[1]
  16.         self.speed = 2
  17.         self.active = True
  18.         self.rect.left, self.rect.top = \
  19.                         randint(0,self.width - self.rect.width ), \
  20.                         randint(-5 * self.height,0)
  21.         self.mask = pygame.mask.from_surface(self.image)

  22.     def move(self):
  23.         if self.rect.top < self.height:
  24.             self.rect.top += self.speed
  25.         else:
  26.             self.reset()

  27.     def reset(self):
  28.         self.active = True
  29.         self.rect.left, self.rect.top = \
  30.                         randint(0,self.width - self.rect.width ), \
  31.                         randint(-5 * self.height,0)


  32. class MidEnemy(pygame.sprite.Sprite):
  33.     energy = 8

  34.    
  35.     def __init__(self,bg_size):
  36.         pygame.sprite.Sprite.__init__(self)

  37.         self.image = pygame.image.load("images/enemy2.png").convert_alpha()
  38.         self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()
  39.         self.destroy_images = []
  40.         self.destroy_images.extend([\
  41.             pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
  42.             pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
  43.             pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
  44.             pygame.image.load("images/enemy2_down4.png").convert_alpha() \
  45.             ])
  46.         self.rect = self.image.get_rect()
  47.         self.width, self.height = bg_size[0], bg_size[1]
  48.         self.speed = 1
  49.         self.active = True
  50.         self.rect.left, self.rect.top = \
  51.                         randint(0,self.width - self.rect.width ), \
  52.                         randint(-10 * self.height,-self.height)
  53.         self.mask = pygame.mask.from_surface(self.image)
  54.         self.energy = MidEnemy.energy
  55.         self.hit = False

  56.     def move(self):
  57.         if self.rect.top < self.height:
  58.             self.rect.top += self.speed
  59.         else:
  60.             self.reset()

  61.     def reset(self):
  62.         self.active = True
  63.         self.energy = MidEnemy.energy
  64.         self.rect.left, self.rect.top = \
  65.                         randint(0,self.width - self.rect.width ), \
  66.                         randint(-10 * self.height,-self.height)


  67. class BigEnemy(pygame.sprite.Sprite):
  68.     energy = 20

  69.    
  70.     def __init__(self,bg_size):
  71.         pygame.sprite.Sprite.__init__(self)

  72.         self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
  73.         self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
  74.         self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()
  75.         self.destroy_images = []
  76.         self.destroy_images.extend([\
  77.             pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
  78.             pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
  79.             pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
  80.             pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
  81.             pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
  82.             pygame.image.load("images/enemy3_down6.png").convert_alpha() \
  83.             ])
  84.         self.rect = self.image1.get_rect()
  85.         self.width, self.height = bg_size[0], bg_size[1]
  86.         self.speed = 1
  87.         self.active = True
  88.         self.rect.left, self.rect.top = \
  89.                         randint(0,self.width - self.rect.width ), \
  90.                         randint(-15 * self.height, -5 * self.height)
  91.         self.mask = pygame.mask.from_surface(self.image1)
  92.         self.energy = BigEnemy.energy
  93.         self.hit = False
  94.         

  95.     def move(self):
  96.         if self.rect.top < self.height:
  97.             self.rect.top += self.speed
  98.         else:
  99.             self.reset()

  100.     def reset(self):
  101.         self.active = True
  102.         self.energy = BigEnemy.energy
  103.         self.rect.left, self.rect.top = \
  104.                         randint(0,self.width - self.rect.width ), \
  105.                         randint(-15 * self.height, -5 * self.height)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-10 08:28:15 | 显示全部楼层
python小鲨鱼 发表于 2020-4-10 00:16
在这没有设置敌机重置属性中的active属性

那我把飞机大战所有的完整代码发给你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 09:53:25 | 显示全部楼层
编程鱼C 发表于 2020-4-10 08:28
那我把飞机大战所有的完整代码发给你

好的,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 09:54:58 | 显示全部楼层

但是其他的我就不发了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 09:56:08 | 显示全部楼层

这是主要的一部分
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 10:07:46 | 显示全部楼层
编程鱼C 发表于 2020-4-11 09:56
这是主要的一部分

主要的就够了,其它模块我自己来做,感谢,我这权限不够,等权限够了加你好友
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 10:09:07 | 显示全部楼层
python小鲨鱼 发表于 2020-4-11 10:07
主要的就够了,其它模块我自己来做,感谢,我这权限不够,等权限够了加你好友

好的,有问题问
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 10:10:06 | 显示全部楼层
python小鲨鱼 发表于 2020-4-11 10:07
主要的就够了,其它模块我自己来做,感谢,我这权限不够,等权限够了加你好友

你还差40积分就能加好友,加油小鲨鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 10:10:36 | 显示全部楼层
python小鲨鱼 发表于 2020-4-11 10:07
主要的就够了,其它模块我自己来做,感谢,我这权限不够,等权限够了加你好友

你复制好我删了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 10:15:46 | 显示全部楼层

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-12 21:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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