pygame 飞机大战双人版出现的问题
本帖最后由 chenpa 于 2021-10-7 17:17 编辑第一次发帖{:10_254:}
照着小甲鱼的代码写了个双人版的飞机大战,为了区分两个人的分数,把score1和score2写在了子弹击中敌机的碰撞检测下面。
问题出现了,为什么每次摧毁小型敌机分数都会加两次啊,有时候又只加一次{:10_269:}
还有敌机与我方的碰撞检测,撞毁敌机后分数会加10-12次,这是为啥,萌新求解{:10_266:}
完整代码:
链接:https://pan.baidu.com/s/1otBLJIZcrdyOQFBn01TfDA
提取码:1233
static/image/hrline/line2.png
代码:
delay = 100
running = True
while running:
for event in pygame.event.get():
#退出
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == INVINVIBLE_TIME:
m1.invincible = False
pygame.time.set_timer(INVINVIBLE_TIME,0)
elif event.type == INVINVIBLE_TIME1:
m2.invincible = False
pygame.time.set_timer(INVINVIBLE_TIME1,0)
screen.blit(background, (0, 0))
Keys = pygame.key.get_pressed()
if life_num1:
if Keys:
m1.moveUp()
if Keys:
m1.moveDown()
if Keys:
m1.moveLeft()
if Keys:
m1.moveRight()
# 检测玩家一的键盘操作
if life_num2:
if Keys:
m2.moveUp()
if Keys:
m2.moveDown()
if Keys:
m2.moveLeft()
if Keys:
m2.moveRight()
if life_num1 != 0 or life_num2 != 0:
#绘制子弹,发射子弹
if life_num1:
if not (delay % 10):
# bullet_sound.play()
bullets1 = bullet1
bullets1.reset(m1.rect.midtop)
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
if life_num2:
if not (delay % 10):
# bullet_sound.play()
bullets2 = bullet2
bullets2.reset(m2.rect.midtop)
bullet2_index = (bullet2_index + 1) % BULLET2_NUM
#检测一号子弹是否击中敌机
for b in bullets1:
if b.active:
b.move()
screen.blit(b.image,b.rect)
enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
if enemy_hit:
b.active = False
for e in enemy_hit:
if e in mid_enemies or e in big_enemies:
e.hit = True
e.energy -= 1
if e.energy == 0:
e.active = False
if e in mid_enemies:
score1 += 6000
if e in big_enemies:
score1 += 20000
else:
e.active = False
score1 += 1000
#检测二号子弹是否击中敌机
for b in bullets2:
if b.active:
b.move()
screen.blit(b.image,b.rect)
enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.hit = True
e.energy -= 1
if e.energy == 0:
e.active = False
if e in mid_enemies:
score2 += 6000 这里的加分是正常的]
if e in big_enemies:
score2 += 20000
else:
e.active = False
score2 += 1000 这里的加分却会执行两次
#检测一号飞机是否被撞
if life_num1 > 0:
enemies_down1 = pygame.sprite.spritecollide(m1,enemies,False,pygame.sprite.collide_mask) #第四函数,调用mask检测方法,需要有mask属性
if enemies_down1 and not m1.invincible:
m1.active = False
for e in enemies_down1:
e.active = False
if e in small_enemies: 这里的加分都会执行10多次
score1 += 1000
if e in mid_enemies:
score1 += 6000
if e in big_enemies:
score1 += 20000
# 检测二号飞机是否被撞
if life_num2 > 0:
enemies_down2 = pygame.sprite.spritecollide(m2, enemies, False,pygame.sprite.collide_mask)# 第四函数,调用mask检测方法,需要有mask属性
if enemies_down2 and not m2.invincible:
m2.active = False
for e in enemies_down2:
e.active = False
# 绘制大型敌机
for each in big_enemies:
if each.active:
each.move()
if each.hit:#绘制被打到的特效
screen.blit(each.image_hit,each.rect)
each.hit = False
else:
if switch_image:
screen.blit(each.image1,each.rect)
else:
screen.blit(each.image2,each.rect)
#绘制血条
pygame.draw.line(screen,Black,\
(each.rect.left,each.rect.top -5),\
(each.rect.right,each.rect.top -5),\
2)
#当生命值大于20%显示为绿色,否则显示红色
energy_remain = each.energy / enemy.BigEnemy.energy
if energy_remain > 0.2:
energy_color = Green
else:
energy_color = Red
pygame.draw.line(screen,energy_color, \
(each.rect.left, each.rect.top - 5), \
(each.rect.left + each.rect.width *energy_remain,\
each.rect.top - 5),2)
# 即将出现在画面中,播放音效
if each.rect.bottom == -50:
enemy3_fly_sound.play(-1)
else:
#毁灭
if not (delay % 4):
if e3_destroy_index == 0:
enemy3_down_sound.play()
#调用big_enemies的destroy_images列表,开始为0
screen.blit(each.destroy_images,each.rect)
e3_destroy_index = (e3_destroy_index + 1) % 6
if e3_destroy_index == 0:
enemy3_fly_sound.stop()
each.reset()
if each in enemies_down1:
score1 += 20000
if each in enemies_down2:
score2 += 20000
# 绘制中型敌机
for each in mid_enemies:
if each.active:
each.move()
if each.hit:#绘制被打到的特效
screen.blit(each.image_hit,each.rect)
each.hit = False
else:
screen.blit(each.image,each.rect)
#绘制血条
pygame.draw.line(screen,Black,\
(each.rect.left,each.rect.top -5),\
(each.rect.right,each.rect.top -5),\
2)
#当生命值大于20%显示为绿色,否则显示红色
energy_remain = each.energy / enemy.MidEnemy.energy
if energy_remain > 0.2:
energy_color = Green
else:
energy_color = Red
pygame.draw.line(screen,energy_color, \
(each.rect.left, each.rect.top - 5), \
(each.rect.left + each.rect.width * energy_remain,\
each.rect.top - 5),2)
else:
#毁灭
if not (delay % 4):
if e2_destroy_index == 0:
enemy2_down_sound.play()
#调用big_enemies的destroy_images列表,开始为0
screen.blit(each.destroy_images,each.rect)
e2_destroy_index = (e2_destroy_index +1 ) % 4
if e2_destroy_index == 0:
each.reset()
if each in enemies_down1:
score1 += 6000
if each in enemies_down2:
score2 += 6000
# 绘制小型敌机
for each in small_enemies:
if each.active:
each.move()
screen.blit(each.image,each.rect)
else:
# 毁灭
if not (delay % 4):
if e1_destroy_index == 0:
enemy1_down_sound.play()
# 调用big_enemies的destroy_images列表,开始为0
screen.blit(each.destroy_images, each.rect)
e1_destroy_index = (e1_destroy_index + 1) % 4
if e1_destroy_index == 0:
each.reset()
#绘制一号飞机
if life_num1 > 0:
if m1.active:
#飞行
if switch_image:
screen.blit(m1.image1,m1.rect)
else:
screen.blit(m1.image2,m1.rect)
else:
# 毁灭
if not (delay % 3):
if m1_destroy_index == 0:
me_down_sound.play()
# 调用big_enemies的destroy_image列表,开始为0
screen.blit(m1.destroy_images,m1.rect)
m1_destroy_index = (m1_destroy_index + 1) % 4
if m1_destroy_index == 0:
life_num1 -= 1
m1.reset2()
pygame.time.set_timer(INVINVIBLE_TIME,3*1000)
#绘制二号飞机
if life_num2 > 0:
if m2.active:
#飞行
if switch_image:
screen.blit(m2.image3,m2.rect)
else:
screen.blit(m2.image4,m2.rect)
else:
# 毁灭
if not (delay % 3):
if m2_destroy_index == 0:
me_down_sound.play()
# 调用big_enemies的destroy_image列表,开始为0
screen.blit(m2.destroy_images,m2.rect)
m2_destroy_index = (m2_destroy_index + 1) % 4
if m2_destroy_index == 0:
life_num2 -= 1
m2.reset2()
pygame.time.set_timer(INVINVIBLE_TIME1,3*1000)
#绘制剩余生命数量
if life_num1:
for i in range(life_num1):
screen.blit(life_image,\
(10+i*life_rect.width,\
height-10-life_rect.height))
if life_num2:
for i in range(life_num2):
screen.blit(life_image,\
(width-10-(i+1)*life_rect.width,\
height-10-life_rect.height))
# 绘制得分
score_text = score_font.render('1P:%s' % str(score1), True,White)# render()将字符串渲染成sueface对象(字符串,抗锯齿,颜色)
screen.blit(score_text, (10, 5))
score_text1 = score_font.render('2P:%s' % str(score2), True, White)# render()将字符串渲染成sueface对象(字符串,抗锯齿,颜色)
screen.blit(score_text1, (280, 5))
#绘制游戏结束画面
if life_num1 == 0 and life_num2 == 0:
#停止声音
pygame.mixer.music.stop()
pygame.mixer.stop()
gameover_text1 = gameover_font.render('1P:%s'%str(score1),True,White)
gameover_text1_rect = gameover_text1.get_rect()
gameover_text1_rect.left , gameover_text1_rect.top = \
(width - gameover_text1_rect.width)//2,200
screen.blit(gameover_text1,gameover_text1_rect)
gameover_text2 = gameover_font.render('2P:%s'%str(score2),True,White)
gameover_text2_rect = gameover_text2.get_rect()
gameover_text2_rect.left,gameover_text2_rect.top = \
(width - gameover_text2_rect.width)//2,260
screen.blit(gameover_text2,gameover_text2_rect)
again_rect.left,again_rect.top = \
(width - again_rect.width)//2,\
gameover_text2_rect.bottom + 50
screen.blit(again_image,again_rect)
gameover_rect.left,gameover_rect.top = \
(width - again_rect.width)//2,\
again_rect.bottom+10
screen.blit(gameover_image,gameover_rect)
#检测用户鼠标操作,如果用户按下鼠标左键
if pygame.mouse.get_pressed():
#获取鼠标坐标
pos = pygame.mouse.get_pos()
#如果用户点击重新开始
if again_rect.left < pos < again_rect.right and \
again_rect.top < pos < again_rect.bottom:
#调用main()函数,重新开始游戏
main()
#如果点击‘结束游戏’
elif gameover_rect.left < pos < gameover_rect.right and \
gameover_rect.top < pos < gameover_rect.bottom:
#退出游戏
pygame.quit()
sys.exit()
#切换图片
if not (delay % 5): #被5整除时更换
switch_image = not switch_image
delay -= 1
if not delay:
delay = 100
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
static/image/hrline/line4.png
{:10_266:}
页:
[1]