当前位置:网站首页>飞机大战碰撞检测
飞机大战碰撞检测
2022-07-27 16:17:00 【道而起】
碰撞检测
目标
- 了解碰撞检测方法
- 碰撞实现
一、了解碰撞检测方法
pygame 提供了 两个非常方便 的方法可以实现碰撞检测:
pygame.sprite.groupcollide()
- 两个精灵组 中 所有的精灵 的碰撞检测
groupcollide(group1, group2, dokill1, dokill2, collided = None) -> Sprite_dict
- 如果将 dokill 设置为 True,则 发生碰撞的精灵将被自动移除
- collided 参数是用于 计算碰撞的回调函数
- 如果没有指定,则每个精灵必须有一个 rect 属性
pygame.sprite.spritecollide()
- 判断 某个精灵 和 指定精灵组 中的精灵的碰撞
spritecollide(sprite, group, dokill, collided = None) -> Sprite_list
- 如果将 dokill 设置为 True,则 指定精灵组 中 发生碰撞的精灵将被自动移除
- collided 参数是用于 计算碰撞的回调函数
- 如果没有指定,则每个精灵必须有一个 rect 属性
- 返回 精灵组 中跟 精灵 发生碰撞的 精灵列表
二、碰撞实现
def __check_collide(self):
# 1. 子弹摧毁敌机
pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
# 2. 敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# 判断列表时候有内容
if len(enemies) > 0:
# 让英雄牺牲
self.hero.kill()
# 结束游戏
PlaneGame.__game_over()
总结
本章为飞机大战最终章,结合之前几章的内容,写到这里,飞机大战整个游戏就完成了,感兴趣的朋友自己做一遍吧!明天会将飞机大战所有实现源码整个发布出来,喜欢的朋友不要错过哦!
有哪里不足或者有更好的建议,欢迎留言吐槽,有哪里不懂的小伙伴可以私信我,我会一一答复,感谢认可,感谢支持!
边栏推荐
- [mit 6.s081] LEC 3: OS organization and system calls notes
- 你有没有在MySQL的order by上栽过跟头
- USB充电式暖手宝芯片-DLTAP602SC-杰力科创
- INSUFFICIENT_ ACCESS_ ON_ CROSS_ REFERENCE_ ENTITY APEX / SALESFORCE
- Preliminary introduction to C miscellaneous lecture linked list
- LED带风扇护眼学习台灯触摸芯片-DLT8S12A
- MySQL 主从复制数据不一致,怎么办?
- Conflict between blur event and click event in input box
- Login page tablelayout
- [mit 6.s081] LEC 6: isolation & system call entry/exit notes
猜你喜欢
![[mit 6.s081] LEC 10: multiprocessors and locking notes](/img/62/ca6362830321feaf450865132cdea9.png)
[mit 6.s081] LEC 10: multiprocessors and locking notes

2021.7.18 notes MySQL data type

Generate PDM file from Navicat export table

2021.8.1 notes DBA

Quick access to website media resources

阿里架构师耗时280个小时整理的1015页分布式全栈小册,轻松入手分布式系统
![[mit 6.s081] LEC 3: OS organization and system calls notes](/img/34/073d00245eb39844bbe1740f65fe07.png)
[mit 6.s081] LEC 3: OS organization and system calls notes

2021.7.13笔记 子查询

迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创

1. OpenCV image basic operation
随机推荐
你有没有在MySQL的order by上栽过跟头
Preliminary introduction to C miscellaneous lecture linked list
[mit 6.s081] LEC 10: multiprocessors and locking notes
兆骑科创海内外引进高层次人才,创新创业项目对接
Must the MySQL query column be consistent with the group by field?
uniapp H5跨域问题
Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)
MySQL learning Day1 DDL, DML, DQL basic query
输入框blur事件与click事件冲突问题
Graphical interface programming
MySQL code database creation parking management system foreign key
1. OpenCV image basic operation
迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创
2021.7.30 note index
USB充电式暖手宝芯片-DLTAP602SC-杰力科创
org.gradle.api. UncheckedIOException: Could not load properties for module ‘gradle-kotlin-dsl‘ from C
2021.7.13笔记 子查询
A case to understand MySQL view
Alibaba architects spent 280 hours sorting out 1015 pages of distributed full stack pamphlets to easily start the distributed system
微信小程序多文件上传