在游戏更新中添加子弹与敌方的碰撞检测

for bullet in hero.bullets: bullet.update() screen.blit(bullet.image, bullet.rect) # 检测子弹是否与敌方发生碰撞 hit_list = pygame.sprite.spritecollide(bullet, all_sprite, True) for hit in hit_list: if isinstance(hit, Difang): # 如果碰撞到的是敌方,则将其从精灵组中删除 all_sprite.remove(hit)

在敌方更新中添加敌方与英雄的碰撞检测

for difang in all_sprite: if isinstance(difang, Difang): difang.update() # 检测敌方是否与英雄发生碰撞 if pygame.sprite.collide_rect(difang, hero): # 如果碰撞到的是英雄,则游戏结束 pygame.quit() exit()

#屏幕更新 all_sprite.draw(screen) pygame.display.update(

import randomimport pygame#初始化pygameinit#常量定义敌方出现的时间chuxian_difang = pygameUSEREVENT#窗口大小HEIGHTWIDTH = 800800#颜色white = 255255255black = 000Au = 255255153Au1 = 2552420red = 2555050blu = 5050255green =

原文地址: https://www.cveoy.top/t/topic/fdit 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录