python飞机大战的效果_python飞机大战添加爆炸效果

这段代码展示了在游戏编程中如何处理子弹与外星人的碰撞检测,当子弹击中外星人时更新得分、创建爆炸效果,并在所有外星人被消灭后加快游戏节奏。同时,还包括了外星人到达屏幕边缘时的处理逻辑和游戏屏幕的更新方法。
摘要由CSDN通过智能技术生成

#导入Bomb类fromBomb import Bomb

#修改代码

def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets,bomb):"""更新子弹的位置,并删除已消失的子弹"""# 更新子弹的位置

bullets.update()

# 删除已消失的子弹for bullet inbullets.copy():if bullet.rect.bottom <= 0:

bullets.remove(bullet)

check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets,bomb)

def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets,bomb):

# 检查是否有子弹击中了外星人

# 如果是这样,就删除相应的外星人和子弹""" collisions =pygame.sprite.groupcollide(bullets, aliens, True, True)ifcollisions:for aliens incollisions.values():

stats.score+= ai_settings.alien_points *len(aliens)

sb.prep_score()

check_high_score(stats, sb)""" for bullet inbullets:

# 检查子弹与飞机的碰撞,及击中飞机

collisions=pygame.sprite.groupcollide(aliens, bullets, True, True)ifcollisions:

current_pos=pygame.Vector2(bullet.rect.x, bullet.rect.y)ifbullet.rect.collidepoint(current_pos):

bomb.position[0] = (bullet.rect.x - 20)

bomb.position[1] = (bullet.rect.y - 40)

bomb.visible=Trueif len(aliens) == 0:

# 删除现有的子弹, 加快游戏节奏,并新建一群外星人

bullets.empty()

ai_settings.increase_speed()

# 提高等级

stats.level+= 1sb.prep_level()

create_fleet(ai_settings, screen, ship, aliens)

def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets,bomb):"""检查是否有外星人到达屏幕边缘

然后更新外星人群中的所有外星人位置"""bomb.action()

check_fleet_edges(ai_settings, aliens)

check_alien_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets)

aliens.update()

# 检测外星人和飞船之间的碰撞ifpygame.sprite.spritecollideany(ship, aliens):

ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)

def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button,introduce_button,bomb):"""更新屏幕上的图像,并切换到新屏幕"""# 每次循环时都重绘屏幕

screen.fill(ai_settings.bg_color)

# 在飞船和外星人后面重绘所有子弹for bullet inbullets.sprites():

bullet.draw_bullet()

ship.blitme()

aliens.draw(screen)

# 绘制爆炸图片

# # 绘制爆炸图片

#self.bomb.draw()

bomb.draw()

# 显示得分

sb.show_score()

# 如果游戏处于非活动状态,就绘制Play按钮ifnot stats.game_active:

play_button.draw_button()

introduce_button.drawintroduce_button()

# 让最近绘制的屏幕可见

pygame.display.flip()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值