鱼C论坛

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

pygame 飞机大战双人版出现的问题

[复制链接]
发表于 2021-10-7 16:33:21 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 chenpa 于 2021-10-7 17:17 编辑

第一次发帖
照着小甲鱼的代码写了个双人版的飞机大战,为了区分两个人的分数,把score1和score2写在了子弹击中敌机的碰撞检测下面。
问题出现了,为什么每次摧毁小型敌机分数都会加两次啊,有时候又只加一次
还有敌机与我方的碰撞检测,撞毁敌机后分数会加10-12次,这是为啥,萌新求解

完整代码:
链接:https://pan.baidu.com/s/1otBLJIZcrdyOQFBn01TfDA
提取码:1233


                               
登录/注册后可看大图



代码:
  
  1. delay = 100
  2.     running = True
  3.     while running:
  4.         for event in pygame.event.get():
  5.             #退出
  6.             if event.type == pygame.QUIT:
  7.                 pygame.quit()
  8.                 sys.exit()

  9.             elif event.type == INVINVIBLE_TIME:
  10.                 m1.invincible = False
  11.                 pygame.time.set_timer(INVINVIBLE_TIME,0)

  12.             elif event.type == INVINVIBLE_TIME1:
  13.                 m2.invincible = False
  14.                 pygame.time.set_timer(INVINVIBLE_TIME1,0)


  15.         screen.blit(background, (0, 0))

  16.         Keys = pygame.key.get_pressed()
  17.         if life_num1:
  18.             if Keys[K_w]:
  19.                 m1.moveUp()
  20.             if Keys[K_s]:
  21.                 m1.moveDown()
  22.             if Keys[K_a]:
  23.                 m1.moveLeft()
  24.             if Keys[K_d]:
  25.                 m1.moveRight()
  26.         # 检测玩家一的键盘操作
  27.         if life_num2:
  28.             if Keys[K_UP]:
  29.                 m2.moveUp()
  30.             if Keys[K_DOWN]:
  31.                 m2.moveDown()
  32.             if Keys[K_LEFT]:
  33.                 m2.moveLeft()
  34.             if Keys[K_RIGHT]:
  35.                 m2.moveRight()

  36.         if life_num1 != 0 or life_num2 != 0:
  37.             #绘制子弹,发射子弹
  38.             if life_num1:
  39.                 if not (delay % 10):
  40.                     # bullet_sound.play()
  41.                     bullets1 = bullet1
  42.                     bullets1[bullet1_index].reset(m1.rect.midtop)
  43.                     bullet1_index = (bullet1_index + 1) % BULLET1_NUM
  44.             if life_num2:
  45.                 if not (delay % 10):
  46.                     # bullet_sound.play()
  47.                     bullets2 = bullet2
  48.                     bullets2[bullet2_index].reset(m2.rect.midtop)
  49.                     bullet2_index = (bullet2_index + 1) % BULLET2_NUM

  50.             #检测一号子弹是否击中敌机
  51.             for b in bullets1:
  52.                 if b.active:
  53.                     b.move()
  54.                     screen.blit(b.image,b.rect)
  55.                     enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
  56.                     if enemy_hit:
  57.                         b.active = False
  58.                         for e in enemy_hit:
  59.                             if e in mid_enemies or e in big_enemies:
  60.                                 e.hit = True
  61.                                 e.energy -= 1
  62.                                 if e.energy == 0:
  63.                                     e.active = False
  64.                                     if e in mid_enemies:
  65.                                         score1 += 6000
  66.                                     if e in big_enemies:
  67.                                         score1 += 20000         [color=Red这里的加分是正常的][/color]
  68.                             else:
  69.                                 e.active = False
  70.                                 score1 += 1000           [color=Red这里的加分却会执行两次][/color]

  71.             #检测二号子弹是否击中敌机
  72.             for b in bullets2:
  73.                 if b.active:
  74.                     b.move()
  75.                     screen.blit(b.image,b.rect)
  76.                     enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
  77.                     if enemy_hit:
  78.                         b.active = False
  79.                         for e in enemy_hit:
  80.                             e.hit = True
  81.                             e.energy -= 1
  82.                             if e.energy == 0:
  83.                                 e.active = False
  84.                                 if e in mid_enemies:
  85.                                     score2 += 6000             [color=Red]这里的加分是正常的][/color]
  86.                                 if e in big_enemies:
  87.                                     score2 += 20000
  88.                             else:
  89.                                 e.active = False
  90.                                 score2 += 1000            [color=Red]这里的加分却会执行两次[/color]

  91.             #检测一号飞机是否被撞
  92.             if life_num1 > 0:
  93.                 enemies_down1 = pygame.sprite.spritecollide(m1,enemies,False,pygame.sprite.collide_mask) #第四函数,调用mask检测方法,需要有mask属性
  94.                 if enemies_down1 and not m1.invincible:
  95.                     m1.active = False
  96.                     for e in enemies_down1:
  97.                         e.active = False
  98.                         if e in small_enemies:           [color=Red]这里的加分都会执行10多次[/color]
  99.                             score1 += 1000
  100.                         if e in mid_enemies:
  101.                             score1 += 6000
  102.                         if e in big_enemies:
  103.                             score1 += 20000

  104.             # 检测二号飞机是否被撞
  105.             if life_num2 > 0:
  106.                 enemies_down2 = pygame.sprite.spritecollide(m2, enemies, False,pygame.sprite.collide_mask)  # 第四函数,调用mask检测方法,需要有mask属性
  107.                 if enemies_down2 and not m2.invincible:
  108.                     m2.active = False
  109.                     for e in enemies_down2:
  110.                         e.active = False

  111.             # 绘制大型敌机
  112.             for each in big_enemies:
  113.                 if each.active:
  114.                     each.move()
  115.                     if each.hit:  #绘制被打到的特效
  116.                         screen.blit(each.image_hit,each.rect)
  117.                         each.hit = False
  118.                     else:
  119.                         if switch_image:
  120.                             screen.blit(each.image1,each.rect)
  121.                         else:
  122.                             screen.blit(each.image2,each.rect)
  123.                     #绘制血条
  124.                     pygame.draw.line(screen,Black,\
  125.                                      (each.rect.left,each.rect.top -5),\
  126.                                      (each.rect.right,each.rect.top -5),\
  127.                                      2)
  128.                     #当生命值大于20%显示为绿色,否则显示红色
  129.                     energy_remain = each.energy / enemy.BigEnemy.energy
  130.                     if energy_remain > 0.2:
  131.                         energy_color = Green
  132.                     else:
  133.                         energy_color = Red
  134.                     pygame.draw.line(screen,energy_color, \
  135.                                      (each.rect.left, each.rect.top - 5), \
  136.                                      (each.rect.left + each.rect.width *energy_remain,\
  137.                                      each.rect.top - 5),2)

  138.                     # 即将出现在画面中,播放音效
  139.                     if each.rect.bottom == -50:
  140.                         enemy3_fly_sound.play(-1)
  141.                 else:
  142.                     #毁灭
  143.                     if not (delay % 4):
  144.                         if e3_destroy_index == 0:
  145.                             enemy3_down_sound.play()
  146.                         #调用big_enemies的destroy_images列表,开始为0
  147.                         screen.blit(each.destroy_images[e3_destroy_index],each.rect)
  148.                         e3_destroy_index = (e3_destroy_index + 1) % 6
  149.                         if e3_destroy_index == 0:
  150.                             enemy3_fly_sound.stop()
  151.                             each.reset()
  152.                             if each in enemies_down1:
  153.                                 score1 += 20000
  154.                             if each in enemies_down2:
  155.                                 score2 += 20000
  156.             # 绘制中型敌机
  157.             for each in mid_enemies:
  158.                 if each.active:
  159.                     each.move()
  160.                     if each.hit:  #绘制被打到的特效
  161.                         screen.blit(each.image_hit,each.rect)
  162.                         each.hit = False
  163.                     else:
  164.                         screen.blit(each.image,each.rect)
  165.                     #绘制血条
  166.                     pygame.draw.line(screen,Black,\
  167.                                      (each.rect.left,each.rect.top -5),\
  168.                                      (each.rect.right,each.rect.top -5),\
  169.                                      2)
  170.                     #当生命值大于20%显示为绿色,否则显示红色
  171.                     energy_remain = each.energy / enemy.MidEnemy.energy
  172.                     if energy_remain > 0.2:
  173.                         energy_color = Green
  174.                     else:
  175.                         energy_color = Red
  176.                     pygame.draw.line(screen,energy_color, \
  177.                                      (each.rect.left, each.rect.top - 5), \
  178.                                      (each.rect.left + each.rect.width * energy_remain,\
  179.                                      each.rect.top - 5),2)
  180.                 else:
  181.                     #毁灭
  182.                     if not (delay % 4):
  183.                         if e2_destroy_index == 0:
  184.                             enemy2_down_sound.play()
  185.                         #调用big_enemies的destroy_images列表,开始为0
  186.                         screen.blit(each.destroy_images[e2_destroy_index],each.rect)
  187.                         e2_destroy_index = (e2_destroy_index +1 ) % 4
  188.                         if e2_destroy_index == 0:
  189.                             each.reset()
  190.                             if each in enemies_down1:
  191.                                 score1 += 6000
  192.                             if each in enemies_down2:
  193.                                 score2 += 6000
  194.             # 绘制小型敌机
  195.             for each in small_enemies:
  196.                 if each.active:
  197.                     each.move()
  198.                     screen.blit(each.image,each.rect)
  199.                 else:
  200.                     # 毁灭
  201.                     if not (delay % 4):
  202.                         if e1_destroy_index == 0:
  203.                             enemy1_down_sound.play()
  204.                         # 调用big_enemies的destroy_images列表,开始为0
  205.                         screen.blit(each.destroy_images[e1_destroy_index], each.rect)
  206.                         e1_destroy_index = (e1_destroy_index + 1) % 4
  207.                         if e1_destroy_index == 0:
  208.                             each.reset()




  209.             #绘制一号飞机
  210.             if life_num1 > 0:
  211.                 if m1.active:
  212.                     #飞行
  213.                     if switch_image:
  214.                         screen.blit(m1.image1,m1.rect)
  215.                     else:
  216.                         screen.blit(m1.image2,m1.rect)
  217.                 else:
  218.                     # 毁灭
  219.                     if not (delay % 3):
  220.                         if m1_destroy_index == 0:
  221.                             me_down_sound.play()
  222.                         # 调用big_enemies的destroy_image列表,开始为0
  223.                         screen.blit(m1.destroy_images[m1_destroy_index],m1.rect)
  224.                         m1_destroy_index = (m1_destroy_index + 1) % 4
  225.                         if m1_destroy_index == 0:
  226.                             life_num1 -= 1
  227.                             m1.reset2()
  228.                             pygame.time.set_timer(INVINVIBLE_TIME,3*1000)
  229.             #绘制二号飞机
  230.             if life_num2 > 0:
  231.                 if m2.active:
  232.                     #飞行
  233.                     if switch_image:
  234.                         screen.blit(m2.image3,m2.rect)
  235.                     else:
  236.                         screen.blit(m2.image4,m2.rect)
  237.                 else:
  238.                     # 毁灭
  239.                     if not (delay % 3):
  240.                         if m2_destroy_index == 0:
  241.                             me_down_sound.play()
  242.                         # 调用big_enemies的destroy_image列表,开始为0
  243.                         screen.blit(m2.destroy_images[m2_destroy_index+4],m2.rect)
  244.                         m2_destroy_index = (m2_destroy_index + 1) % 4
  245.                         if m2_destroy_index == 0:
  246.                             life_num2 -= 1
  247.                             m2.reset2()
  248.                             pygame.time.set_timer(INVINVIBLE_TIME1,3*1000)


  249.             #绘制剩余生命数量
  250.             if life_num1:
  251.                 for i in range(life_num1):
  252.                     screen.blit(life_image,\
  253.                                 (10+i*life_rect.width,\
  254.                                 height-10-life_rect.height))
  255.             if life_num2:
  256.                 for i in range(life_num2):
  257.                     screen.blit(life_image,\
  258.                                 (width-10-(i+1)*life_rect.width,\
  259.                                 height-10-life_rect.height))


  260.             # 绘制得分
  261.             score_text = score_font.render('1P:%s' % str(score1), True,White)  # render()将字符串渲染成sueface对象(字符串,抗锯齿,颜色)
  262.             screen.blit(score_text, (10, 5))
  263.             score_text1 = score_font.render('2P:%s' % str(score2), True, White)  # render()将字符串渲染成sueface对象(字符串,抗锯齿,颜色)
  264.             screen.blit(score_text1, (280, 5))

  265.         #绘制游戏结束画面
  266.         if life_num1 == 0 and life_num2 == 0:
  267.             #停止声音
  268.             pygame.mixer.music.stop()
  269.             pygame.mixer.stop()

  270.             gameover_text1 = gameover_font.render('1P:%s'%str(score1),True,White)
  271.             gameover_text1_rect = gameover_text1.get_rect()
  272.             gameover_text1_rect.left , gameover_text1_rect.top = \
  273.                 (width - gameover_text1_rect.width)//2,200
  274.             screen.blit(gameover_text1,gameover_text1_rect)

  275.             gameover_text2 = gameover_font.render('2P:%s'%str(score2),True,White)
  276.             gameover_text2_rect = gameover_text2.get_rect()
  277.             gameover_text2_rect.left,gameover_text2_rect.top = \
  278.                 (width - gameover_text2_rect.width)//2,260
  279.             screen.blit(gameover_text2,gameover_text2_rect)

  280.             again_rect.left,again_rect.top = \
  281.                 (width - again_rect.width)//2,\
  282.                 gameover_text2_rect.bottom + 50
  283.             screen.blit(again_image,again_rect)

  284.             gameover_rect.left,gameover_rect.top = \
  285.                 (width - again_rect.width)//2,\
  286.                 again_rect.bottom+10
  287.             screen.blit(gameover_image,gameover_rect)


  288.             #检测用户鼠标操作,如果用户按下鼠标左键
  289.             if pygame.mouse.get_pressed()[0]:
  290.                 #获取鼠标坐标
  291.                 pos = pygame.mouse.get_pos()
  292.                 #如果用户点击重新开始
  293.                 if again_rect.left < pos[0] < again_rect.right and \
  294.                     again_rect.top < pos[1] < again_rect.bottom:
  295.                     #调用main()函数,重新开始游戏
  296.                     main()
  297.                 #如果点击‘结束游戏’
  298.                 elif gameover_rect.left < pos[0] < gameover_rect.right and \
  299.                     gameover_rect.top < pos[1] < gameover_rect.bottom:
  300.                     #退出游戏
  301.                     pygame.quit()
  302.                     sys.exit()


  303.         #切换图片
  304.         if not (delay % 5):    #被5整除时更换
  305.             switch_image = not switch_image
  306.         delay -= 1
  307.         if not delay:
  308.             delay = 100

  309.         pygame.display.flip()

  310.         clock.tick(60)
  311. if __name__ == '__main__':
  312.     try:
  313.         main()
  314.     except SystemExit:
  315.         pass
  316.     except:
  317.         traceback.print_exc()
  318.         pygame.quit()
  319.         input()
复制代码





                               
登录/注册后可看大图


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-7 19:22:55 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 23:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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