鱼C论坛

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

如何修改一次齐发四个子弹

[复制链接]
发表于 2024-4-2 21:20:30 | 显示全部楼层 |阅读模式

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

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

x
  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/background2.png').convert()
  16. ice_bg = pygame.image.load('images/ice_bg.png').convert_alpha()
  17. # 设置一些颜色
  18. BLACK = (0,0,0)
  19. WHITE = (255,255,255)
  20. GREEN = (0,255,0)
  21. RED = (255,0,0)
  22. # 加载背景音乐及音效
  23. pygame.mixer.music.load("sound/game_music.ogg")
  24. pygame.mixer.music.set_volume(0.2)
  25. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  26. bullet_sound.set_volume(0.2)
  27. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  28. bomb_sound.set_volume(0.2)
  29. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  30. supply_sound.set_volume(0.2)
  31. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  32. get_bomb_sound.set_volume(0.2)
  33. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  34. get_bullet_sound.set_volume(0.2)
  35. get_heart_sound = pygame.mixer.Sound("sound/get_heart.wav")
  36. get_heart_sound.set_volume(0.2)
  37. get_ice_sound = pygame.mixer.Sound("sound/get_ice.wav")
  38. get_ice_sound.set_volume(0.2)
  39. lose_ice_sound = pygame.mixer.Sound("sound/lose_ice.wav")
  40. lose_ice_sound.set_volume(0.2)
  41. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  42. upgrade_sound.set_volume(0.2)
  43. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  44. enemy3_fly_sound.set_volume(0.2)
  45. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  46. enemy1_down_sound.set_volume(0.2)
  47. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  48. enemy2_down_sound.set_volume(0.2)
  49. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  50. enemy3_down_sound.set_volume(0.5)
  51. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  52. me_down_sound.set_volume(0.2)
  53. # 定义生成小型敌机函数
  54. def add_small_enemies(group1,group2,num):
  55.     for i in range(num):
  56.         e1 = enemy.SmallEnemy(bg_size)
  57.         group1.add(e1)
  58.         group2.add(e1)
  59. # 定义生成中型敌机函数
  60. def add_mid_enemies(group1,group2,num):
  61.     for i in range(num):
  62.         e2 = enemy.MidEnemy(bg_size)
  63.         group1.add(e2)
  64.         group2.add(e2)
  65. # 定义生成大型敌机函数
  66. def add_big_enemies(group1,group2,num):
  67.     for i in range(num):
  68.         e3 = enemy.BigEnemy(bg_size)
  69.         group1.add(e3)
  70.         group2.add(e3)
  71. # 定义改变敌机速度函数
  72. def inc_speed(target, inc):
  73.     for each in target:
  74.         each.speed += inc
  75. # 主程序
  76. def main():
  77.     # 循环播放背景音乐
  78.     pygame.mixer.music.play(-1)
  79.     # 生成我方飞机
  80.     me = myplane.MyPlane(bg_size)
  81.     # 创建敌机组
  82.     enemies = pygame.sprite.Group()
  83.     # 生成小型敌机
  84.     small_enemies = pygame.sprite.Group()
  85.     add_small_enemies(small_enemies,enemies,15)
  86.     # 生成中型敌机
  87.     mid_enemies = pygame.sprite.Group()
  88.     add_mid_enemies(mid_enemies,enemies,4)
  89.     # 生成大型敌机
  90.     big_enemies = pygame.sprite.Group()
  91.     add_big_enemies(big_enemies,enemies,2)
  92.     # 初始化子弹
  93.     bullet1 = []
  94.     bullet1_index = 0
  95.     BULLET1_NUM = 4
  96.     for i in range(BULLET1_NUM):
  97.         bullet1.append(bullet.Bullet1(me.rect.midtop))
  98.     # 初始化超级子弹
  99.     bullet2 = []
  100.     bullet2_index = 0
  101.     BULLET2_NUM = 8
  102.     for i in range(BULLET2_NUM // 2):
  103.         bullet2.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery)))
  104.         bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery)))
  105.     # 初始化四层子弹
  106.     bullet3 = []
  107.     bullet3_index = 0
  108.     bullet3_num = 16
  109.     for i in range(bullet3_num // 4):
  110.         bullet3.append(bullet.Bullet3((me.rect.centerx - 63, me.rect.centery)))
  111.         bullet3.append(bullet.Bullet3((me.rect.centerx - 23, me.rect.centery)))
  112.         bullet3.append(bullet.Bullet3((me.rect.centerx + 23, me.rect.centery)))
  113.         bullet3.append(bullet.Bullet3((me.rect.centerx + 63, me.rect.centery)))
  114.     # 初始化延时器
  115.     clock = pygame.time.Clock()
  116.     # 定义绘制飞机索引
  117.     e1_destroy_index = 0
  118.     e2_destroy_index = 0
  119.     e3_destroy_index = 0
  120.     me_destroy_index = 0
  121.     # 初始化得分
  122.     score = 0
  123.     score_font = pygame.font.Font('font/font.ttf',36)
  124.     # 初始化暂停按钮和状态
  125.     paused = False
  126.     pause_nor_image = pygame.image.load('images/pause_nor.png').convert_alpha()
  127.     pause_pressed_image = pygame.image.load('images/pause_pressed.png').convert_alpha()
  128.     resume_nor_image = pygame.image.load('images/resume_nor.png').convert_alpha()
  129.     resume_pressed_image = pygame.image.load('images/resume_pressed.png').convert_alpha()
  130.     paused_rect = pause_nor_image.get_rect()
  131.     paused_rect.left,paused_rect.top = width - paused_rect.width - 10,10
  132.     paused_image = pause_nor_image
  133.     # 初始化禁音按钮和状态
  134.     silence = False
  135.     volume_on_pressed_image = pygame.image.load('images/sound_open.png').convert_alpha()
  136.     volume_on_image = pygame.image.load('images/sound_open_not.png').convert_alpha()
  137.     volume_off_pressed_image = pygame.image.load('images/sound_close.png').convert_alpha()
  138.     volume_off_image = pygame.image.load('images/sound_close_not.png').convert_alpha()
  139.     volume_rect = volume_on_image.get_rect()
  140.     volume_rect.left, volume_rect.top = width - paused_rect.width - 10, 65
  141.     volume_image = volume_on_image
  142.     # 初始化等级
  143.     level = 1
  144.     # 初始化全屏炸弹显示
  145.     bomb_image = pygame.image.load("images/bomb.png").convert_alpha()
  146.     bomb_rect = bomb_image.get_rect()
  147.     bomb_font = pygame.font.Font("font/font.ttf", 48)
  148.     bomb_num = 3
  149.     # 初始化补给
  150.     bullet_supply = supply.Bullet_Supply(bg_size)
  151.     bomb_supply = supply.Bomb_Supply(bg_size)
  152.     heart_supply = supply.Heart_Supply(bg_size)
  153.     ice_supply = supply.Ice_Supply(bg_size)
  154.     fire_supply = supply.Fire_Supply(bg_size)
  155.     SUPPLY_TIME = USEREVENT
  156.     pygame.time.set_timer(SUPPLY_TIME,30 * 1000)
  157.     DOUBLE_BULLET_TIME = USEREVENT + 1
  158.     is_double_bullet = False
  159.     is_super_bullet = False
  160.     # 初始化无敌状态
  161.     INVINCIBLE_TIME = USEREVENT + 2
  162.     life_image = pygame.image.load('images/life.png').convert_alpha()
  163.     life_rect = life_image.get_rect()
  164.     life_num = 3
  165.     # 避免反复读取文件
  166.     recorded = False
  167.     # 初始化游戏结束元素
  168.     gameover_font = pygame.font.Font("font/font.TTF", 48)
  169.     again_image = pygame.image.load("images/again.png").convert_alpha()
  170.     again_rect = again_image.get_rect()
  171.     gameover_image = pygame.image.load("images/gameover.png").convert_alpha()
  172.     gameover_rect = gameover_image.get_rect()
  173.     flozen = False
  174.     FLOZEN_TIME = USEREVENT + 3
  175.     bg_posy = -700
  176.     switch_image = True
  177.     delay = 100
  178.     running = True
  179.     while running:
  180.         for event in pygame.event.get():
  181.             if event.type == QUIT:
  182.                 # 退出
  183.                 sys.exit()
  184.             elif event.type == MOUSEBUTTONDOWN:
  185.                 if event.button == 1 and paused_rect.collidepoint(event.pos):
  186.                     # 暂停或开始
  187.                     paused = not paused
  188.                     if paused:
  189.                         # 停止发放补给
  190.                         pygame.time.set_timer(SUPPLY_TIME, 0)
  191.                         # 停止播放音乐
  192.                         pygame.mixer.music.pause()
  193.                         pygame.mixer.pause()
  194.                     elif not paused and not silence:
  195.                         # 设置发放补给
  196.                         pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
  197.                         # 继续播放音乐
  198.                         pygame.mixer.music.unpause()
  199.                         pygame.mixer.unpause()
  200.                 if event.button == 1 and volume_rect.collidepoint(event.pos):
  201.                     silence = not silence
  202.                     if silence:
  203.                         pygame.mixer.music.pause()
  204.                         pygame.mixer.pause()
  205.                     elif not paused and not silence:
  206.                         pygame.mixer.music.unpause()
  207.                         pygame.mixer.unpause()
  208.             elif event.type == MOUSEMOTION:
  209.                 # 如果用户点击了暂停按钮
  210.                 if paused_rect.collidepoint(event.pos):
  211.                     if paused:
  212.                         paused_image = resume_pressed_image
  213.                     else:
  214.                         paused_image = pause_pressed_image
  215.                 else:
  216.                     if paused:
  217.                         paused_image = resume_nor_image
  218.                     else:
  219.                         paused_image = pause_nor_image
  220.                 # 如果用户点击了禁音按钮
  221.                 if volume_rect.collidepoint(event.pos):
  222.                     if silence:
  223.                         volume_image = volume_off_pressed_image
  224.                     else:
  225.                         volume_image = volume_on_pressed_image
  226.                 else:
  227.                     if silence:
  228.                         volume_image = volume_off_image
  229.                     else:
  230.                         volume_image = volume_on_image
  231.             elif event.type == KEYDOWN:
  232.                 # 用户使用了全屏炸弹
  233.                 if event.key == K_SPACE:
  234.                     if bomb_num:
  235.                         bomb_num -= 1
  236.                         if not silence:
  237.                             bomb_sound.play()
  238.                         for each in enemies:
  239.                             if each.rect.bottom > 0:
  240.                                 each.active = False
  241.             elif event.type == SUPPLY_TIME:
  242.                 # 发放补给
  243.                 if not silence:
  244.                     supply_sound.play()
  245.                 # 随机的补给内容
  246.                 choose = choice([1,2,3,4,5])
  247.                 if choose == 1:
  248.                     bomb_supply.reset()
  249.                 elif choose == 2:
  250.                     bullet_supply.reset()
  251.                 elif choose == 3:
  252.                     fire_supply.reset()
  253.                 elif choose == 4:
  254.                     ice_supply.reset()
  255.                 elif choose == 5:
  256.                     heart_supply.reset()
  257.             elif event.type == DOUBLE_BULLET_TIME:
  258.                 # 结束超级子弹的使用
  259.                 is_double_bullet = False
  260.                 is_super_bullet = False
  261.                 pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)
  262.             elif event.type == INVINCIBLE_TIME:
  263.                 me.invincible = False
  264.                 pygame.time.set_timer(INVINCIBLE_TIME,0)
  265.             elif event.type == FLOZEN_TIME:
  266.                 if not silence:
  267.                     lose_ice_sound.play()
  268.                 flozen = False
  269.                 pygame.time.set_timer(FLOZEN_TIME, 0)
  270.         # 级别上升
  271.         if level == 1 and score > 50000:
  272.             level = 2
  273.             if not silence:
  274.                 upgrade_sound.play()
  275.             add_small_enemies(small_enemies,enemies,3)
  276.             add_mid_enemies(mid_enemies,enemies,2)
  277.             add_big_enemies(big_enemies,enemies,1)
  278.             inc_speed(small_enemies,1)
  279.         # 级别上升
  280.         elif level == 1 and score > 300000:
  281.             level = 2
  282.             if not silence:
  283.                 upgrade_sound.play()
  284.             add_small_enemies(small_enemies,enemies,5)
  285.             add_mid_enemies(mid_enemies,enemies,3)
  286.             add_big_enemies(big_enemies,enemies,2)
  287.             inc_speed(small_enemies,1)
  288.             inc_speed(mid_enemies,1)
  289.         # 级别上升
  290.         elif level == 3 and score > 600000:
  291.             level = 4
  292.             if not silence:
  293.                 upgrade_sound.play()
  294.             add_small_enemies(small_enemies,enemies,5)
  295.             add_mid_enemies(mid_enemies,enemies,3)
  296.             add_big_enemies(big_enemies,enemies,2)
  297.             inc_speed(small_enemies,1)
  298.             inc_speed(mid_enemies,1)
  299.         # 级别上升
  300.         elif level == 4 and score > 1000000:
  301.             level = 5
  302.             if not silence:
  303.                 upgrade_sound.play()
  304.             add_small_enemies(small_enemies,enemies,5)
  305.             add_mid_enemies(mid_enemies,enemies,3)
  306.             add_big_enemies(big_enemies,enemies,2)
  307.             inc_speed(small_enemies,1)
  308.             inc_speed(mid_enemies,1)
  309.         # 绘制背景
  310.         if bg_posy >= 0 and not paused and not flozen:
  311.             bg_posy = -700
  312.         if not paused and not flozen:
  313.             bg_posy += 0.5
  314.         screen.blit(background, (0, bg_posy))
  315.         if life_num and not paused:
  316.             if not flozen:
  317.                 key_pressed = pygame.key.get_pressed()
  318.                 if key_pressed[K_w] or key_pressed[K_UP]:
  319.                     me.moveUp()
  320.                 if key_pressed[K_s] or key_pressed[K_DOWN]:
  321.                     me.moveDown()
  322.                 if key_pressed[K_a] or key_pressed[K_LEFT]:
  323.                     me.moveLeft()
  324.                 if key_pressed[K_d] or key_pressed[K_RIGHT]:
  325.                     me.moveRight()
  326.             if bomb_supply.active:
  327.                 bomb_supply.move()
  328.                 screen.blit(bomb_supply.image, bomb_supply.rect)
  329.                 if pygame.sprite.collide_mask(bomb_supply, me):
  330.                     if not silence:
  331.                         get_bomb_sound.play()
  332.                     if bomb_num < 3:
  333.                         bomb_num += 1
  334.                     bomb_supply.active = False
  335.             if bullet_supply.active:
  336.                 bullet_supply.move()
  337.                 screen.blit(bullet_supply.image, bullet_supply.rect)
  338.                 if pygame.sprite.collide_mask(bullet_supply, me):
  339.                     if not silence:
  340.                         get_bullet_sound.play()
  341.                     is_double_bullet = True
  342.                     pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
  343.                     bullet_supply.active = False
  344.             if heart_supply.active:
  345.                 heart_supply.move()
  346.                 screen.blit(heart_supply.image, heart_supply.rect)
  347.                 if pygame.sprite.collide_mask(heart_supply, me):
  348.                     if not silence:
  349.                         get_heart_sound.play()
  350.                     if life_num < 3 and life_num > 0:
  351.                         life_num += 1
  352.                     heart_supply.active = False
  353.             if ice_supply.active:
  354.                 ice_supply.move()
  355.                 screen.blit(ice_supply.image, ice_supply.rect)
  356.                 if pygame.sprite.collide_mask(ice_supply, me):
  357.                     if not silence:
  358.                         get_ice_sound.play()
  359.                     pygame.time.set_timer(FLOZEN_TIME, 5 * 1000)
  360.                     flozen = True
  361.                     ice_supply.active = False
  362.             if fire_supply.active:
  363.                 fire_supply.move()
  364.                 screen.blit(fire_supply.image, fire_supply.rect)
  365.                 if pygame.sprite.collide_mask(fire_supply, me):
  366.                     if not silence:
  367.                         get_bullet_sound.play()
  368.                     is_super_bullet = True
  369.                     pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
  370.                     fire_supply.active = False
  371.             if not(delay % 10) and not flozen:
  372.                 if not silence:
  373.                     bullet_sound.play()
  374.                 if is_double_bullet:
  375.                     bullets = bullet2
  376.                     bullets[bullet2_index].reset((me.rect.centerx-33, me.rect.centery))
  377.                     bullets[bullet2_index + 1].reset((me.rect.centerx+30, me.rect.centery))
  378.                     bullet2_index = (bullet2_index + 2) % BULLET2_NUM
  379.                 elif is_super_bullet:
  380.                     bullets = bullet3
  381.                     bullets[bullet3_index].reset((me.rect.centerx - 63, me.rect.centery))
  382.                     bullets[bullet3_index + 1].reset((me.rect.centerx - 23, me.rect.centery))
  383.                     bullets[bullet3_index + 2].reset((me.rect.centerx + 23, me.rect.centery))
  384.                     bullets[bullet3_index + 3].reset((me.rect.centerx + 63, me.rect.centery))
  385.                     bullet3_index = (bullet3_index + 4) % bullet3_num
  386.                 else:
  387.                     bullets = bullet1
  388.                     bullets[bullet1_index].reset(me.rect.midtop)
  389.                     bullet1_index = (bullet1_index + 1) % BULLET1_NUM
  390.             for b in bullets:
  391.                 if b.active and not flozen:
  392.                     b.move()
  393.                     screen.blit(b.image,b.rect)
  394.                     enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
  395.                     if enemy_hit:
  396.                         b.active = False
  397.                         for e in enemy_hit:
  398.                             if e in mid_enemies or e in big_enemies:
  399.                                 e.hit = True
  400.                                 e.energy -= 1
  401.                                 if e.energy == 0:
  402.                                     e.active = False
  403.                             else:
  404.                                 e.active = False
  405.             for each in big_enemies:
  406.                 if each.active:
  407.                     each.move(flozen)
  408.                     if each.hit:
  409.                         screen.blit(each.image_hit,each.rect)
  410.                         each.hit = False
  411.                     else:
  412.                         if switch_image:
  413.                             screen.blit(each.image1,each.rect)
  414.                         else:
  415.                             screen.blit(each.image2,each.rect)
  416.                     pygame.draw.line(screen,BLACK,\
  417.                         (each.rect.left,each.rect.top - 5),\
  418.                         (each.rect.right,each.rect.top - 5),\
  419.                         2)
  420.                     energy_remain = each.energy / enemy.BigEnemy.energy
  421.                     if energy_remain > 0.2:
  422.                         energy_color = GREEN
  423.                     else:
  424.                         energy_color = RED
  425.                     pygame.draw.line(screen,energy_color,\
  426.                         (each.rect.left,each.rect.top - 5),\
  427.                         (each.rect.left + each.rect.width * energy_remain,\
  428.                             each.rect.top - 5),2)
  429.                     if each.rect.bottom == -50 and not silence:
  430.                         enemy3_fly_sound.play(-1)
  431.                 else:
  432.                     if not (delay % 3):
  433.                         if e3_destroy_index == 0 and not silence:
  434.                             enemy3_down_sound.play()
  435.                         screen.blit(each.destroy_images[e3_destroy_index],each.rect)
  436.                         e3_destroy_index = (e3_destroy_index + 1) % 6
  437.                         if e3_destroy_index == 0:
  438.                             enemy3_fly_sound.stop()
  439.                             score += 10000
  440.                             each.reset()
  441.             for each in mid_enemies:
  442.                 if each.active:
  443.                     each.move(flozen)
  444.                     if each.hit:
  445.                         screen.blit(each.image_hit,each.rect)
  446.                         each.hit = False
  447.                     else:
  448.                         screen.blit(each.image,each.rect)
  449.                     pygame.draw.line(screen,BLACK,\
  450.                         (each.rect.left,each.rect.top - 5),\
  451.                         (each.rect.right,each.rect.top - 5),\
  452.                         2)
  453.                     energy_remain = each.energy / enemy.MidEnemy.energy
  454.                     if energy_remain > 0.2:
  455.                         energy_color = GREEN
  456.                     else:
  457.                         energy_color = RED
  458.                     pygame.draw.line(screen,energy_color,\
  459.                         (each.rect.left,each.rect.top - 5),\
  460.                         (each.rect.left + each.rect.width * energy_remain,\
  461.                             each.rect.top - 5),2)
  462.                 else:
  463.                     if not (delay % 3):
  464.                         if e2_destroy_index == 0 and not silence:
  465.                             enemy2_down_sound.play()
  466.                         screen.blit(each.destroy_images[e2_destroy_index],each.rect)
  467.                         e2_destroy_index = (e2_destroy_index + 1) % 4
  468.                         if e2_destroy_index == 0:
  469.                             score += 6000
  470.                             each.reset()
  471.             for each in small_enemies:
  472.                 if each.active:
  473.                     each.move(flozen)
  474.                     screen.blit(each.image,each.rect)
  475.                 else:
  476.                     if not (delay % 3):
  477.                         if e1_destroy_index == 0 and not silence:
  478.                             enemy1_down_sound.play()
  479.                         screen.blit(each.destroy_images[e1_destroy_index],each.rect)
  480.                         e1_destroy_index = (e1_destroy_index + 1) % 4
  481.                         if e1_destroy_index == 0:
  482.                             score += 1000
  483.                             each.reset()
  484.             enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
  485.             if enemies_down and not me.invincible:
  486.                 me.active = False
  487.                 for e in enemies_down:
  488.                     e.active = False
  489.             if me.active:
  490.                 if not me.invincible:
  491.                     if delay // 5 % 2 and not flozen:
  492.                         screen.blit(me.image1, me.rect)
  493.                     else:
  494.                         screen.blit(me.image2, me.rect)
  495.                 else:
  496.                     if delay % 10 < 7:
  497.                         screen.blit(me.image1, me.rect)
  498.             else:
  499.                 if not silence:
  500.                     me_down_sound.play()
  501.                 if not (delay % 3):
  502.                     screen.blit(me.destroy_images[me_destroy_index],me.rect)
  503.                     me_destroy_index = (me_destroy_index + 1) % 4
  504.                     if me_destroy_index == 0:
  505.                         life_num -= 1
  506.                         me.reset()
  507.                         pygame.time.set_timer(INVINCIBLE_TIME,3 * 1000)
  508.             if flozen:
  509.                 screen.blit(ice_bg,(0,0))
  510.             bomb_text = bomb_font.render("× %d" % bomb_num, True, WHITE)
  511.             text_rect = bomb_text.get_rect()
  512.             screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
  513.             screen.blit(bomb_text, (20 + bomb_rect.width, height - 5 - text_rect.height))
  514.             if life_num:
  515.                 for i in range(life_num):
  516.                     screen.blit(life_image, \
  517.                                 (width-10-(i+1)*life_rect.width, \
  518.                                  height-10-life_rect.height))
  519.             score_text = score_font.render('Score : %s'% str(score),True,WHITE)
  520.             screen.blit(score_text,(10,5))
  521.         elif life_num == 0:
  522.             pygame.mixer.music.stop()
  523.             pygame.mixer.stop()
  524.             pygame.time.set_timer(SUPPLY_TIME,0)
  525.             if not recorded:
  526.                 recorded = True
  527.                 with open("record.txt", "r") as f:
  528.                     record_score = int(f.read())
  529.                 if score > record_score:
  530.                     with open("record.txt", "w") as f:
  531.                         f.write(str(score))
  532.             record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
  533.             screen.blit(record_score_text, (50, 50))
  534.             gameover_text1 = gameover_font.render("Your Score", True, (255, 255, 255))
  535.             gameover_text1_rect = gameover_text1.get_rect()
  536.             gameover_text1_rect.left, gameover_text1_rect.top = \
  537.                                  (width - gameover_text1_rect.width) // 2, height // 3
  538.             screen.blit(gameover_text1, gameover_text1_rect)
  539.             gameover_text2 = gameover_font.render(str(score), True, (255, 255, 255))
  540.             gameover_text2_rect = gameover_text2.get_rect()
  541.             gameover_text2_rect.left, gameover_text2_rect.top = \
  542.                                  (width - gameover_text2_rect.width) // 2, \
  543.                                  gameover_text1_rect.bottom + 10
  544.             screen.blit(gameover_text2, gameover_text2_rect)
  545.             again_rect.left, again_rect.top = \
  546.                              (width - again_rect.width) // 2, \
  547.                              gameover_text2_rect.bottom + 50
  548.             screen.blit(again_image, again_rect)
  549.             gameover_rect.left, gameover_rect.top = \
  550.                                 (width - again_rect.width) // 2, \
  551.                                 again_rect.bottom + 10
  552.             screen.blit(gameover_image, gameover_rect)
  553.             if pygame.mouse.get_pressed()[0]:
  554.                 pos = pygame.mouse.get_pos()
  555.                 if again_rect.left < pos[0] < again_rect.right and \
  556.                     again_rect.top < pos[1] < again_rect.bottom:
  557.                     main()
  558.                 elif gameover_rect.left < pos[0] < gameover_rect.right and \
  559.                      gameover_rect.top < pos[1] < gameover_rect.bottom:
  560.                     pygame.quit()
  561.                     sys.exit()
  562.         screen.blit(paused_image,paused_rect)
  563.         screen.blit(volume_image, volume_rect)
  564.         if not(delay % 5):
  565.             switch_image = not switch_image
  566.         delay -= 1
  567.         if not delay:
  568.             delay = 100
  569.         pygame.display.flip()
  570.         clock.tick(60)
  571. if __name__ == '__main__':
  572.     try:
  573.         main()
  574.     except SystemExit:
  575.         pass
  576.     except:
  577.         traceback.print_exc()
  578.         pygame.quit()
  579.         input()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-4-2 21:21:02 | 显示全部楼层
要一次发射四个子弹,可以在射击的逻辑中添加一段代码,在生成单个子弹的位置前后各添加两个子弹的位置,这样就能实现一次发射四个子弹了。具体实现如下:

1. 在初始化子弹部分:
  1. bullet1 = []
  2. bullet1_index = 0
  3. BULLET1_NUM = 4
  4. for i in range(BULLET1_NUM):
  5.     bullet1.append(bullet.Bullet1(me.rect.midtop))
复制代码


2. 修改射击的逻辑:
  1. # 发射子弹
  2. if not me.is_hit:
  3.     if delay == 10:
  4.         bullet1[bullet1_index].reset(me.rect.midtop)
  5.         bullet_sound.play()
  6.         bullet1_index = (bullet1_index + 1) % BULLET1_NUM
  7.         bullet1[bullet1_index].reset((me.rect.centerx - 33, me.rect.centery))
  8.         bullet1[bullet1_index].reset((me.rect.centerx + 30, me.rect.centery))
  9.         bullet1[bullet1_index].reset((me.rect.centerx - 63, me.rect.centery))
  10.         bullet1[bullet1_index].reset((me.rect.centerx - 23, me.rect.centery))
  11.         bullet1[bullet1_index].reset((me.rect.centerx + 23, me.rect.centery))
  12.         bullet1[bullet1_index].reset((me.rect.centerx + 63, me.rect.centery))
  13.         bullet1[bullet1_index].reset((me.rect.centerx - 93, me.rect.centery))
  14.         bullet1[bullet1_index].reset((me.rect.centerx - 53, me.rect.centery))
  15.         bullet1[bullet1_index].reset((me.rect.centerx + 53, me.rect.centery))
  16.         bullet1[bullet1_index].reset((me.rect.centerx + 93, me.rect.centery))
  17.         delay = 0
  18.     delay += 1
复制代码


3. 修改子弹的绘制逻辑:
  1. # 绘制子弹
  2. for b in bullet1:
  3.     if b.active:
  4.         b.move()
  5.         screen.blit(b.image, b.rect)
  6.         # 子弹碰撞检测
  7.         enemies_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
  8.         if enemies_hit:
  9.             b.active = False
  10.             for e in enemies_hit:
  11.                 e.hit = True
  12.                 e.energy -= 1
  13.                 if e.energy == 0:
  14.                     e.active = False
  15.                 if e.active and e.hit:
  16.                     e.hit = False
复制代码


这样就实现了一次发射四个子弹的效果了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 03:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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