当前位置:网站首页>PyGame accurately detects image collision
PyGame accurately detects image collision
2022-06-29 12:56:00 【Leleprogrammer】
In use Pygame When writing a game , One might encounter two Rect Objects collide, but there is a problem of space between objects , here , We will teach you a method to accurately detect image collision .
Suppose we have two images ( The type is Surface):surface1,surface2
First , Get these two surface Object's rect:
rect1=surface1.get_rect()
rect2=surface2.get_rect()then , Get their corresponding mask object , use pygame.mask.from_surface() Method , Use as follows :
pygame.mask.from_surface(surface)
Enter yes Surface object , return Mask object
The code is as follows :
mask1=pygame.mask.from_surface(surface1)
mask2=pygame.mask.from_surface(surface2)Next , Collision detection , We need to use overlap Method , Use as follows :
mask.overlap(other,offset)
mask It's a Mask object
other It's another one Mask object
offset It's a tuple , There are two in the tuple rect Of x Coordinate interpolation and y Coordinate interpolation
There are two kinds of return values , If it is a tuple, it means that it has collided , If None It means there is no collision
The code is as follows :
offset=abs(rect1.x-rect2.x),abs(rect1.y-rect2.y)
if rect1.overlap(rect2,offset)!=None: # Has collided
print(" Has collided ")The above code is just an example , For reference , The naming does not need to be the same as above , Just follow the naming conventions
Today's teaching is here , If it helps you solve the problem , Welcome to pay attention ! Thank you for your support ~
边栏推荐
- C#实现堆栈结构的定义、入栈、出栈
- qt json
- 倍福控制第三方伺服走CSV模式--以汇川伺服为例
- 面试突击61:说一下MySQL事务隔离级别?
- Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
- InDesign plug-in - general function development -js debugger open and close -js script development -id plug-in
- Comment calculer Win / Tai / Loss in paired t - test
- 如何計算win/tai/loss in paired t-test
- C#实现顺序表定义、插入、删除、查找操作
- How to create new user for ORACLE 19c (CDB & PDB)
猜你喜欢

推荐模型复现(一):熟悉Torch-RecHub框架与使用

多项目开发入门-业务场景关联基础入门测试 工资表

How to install oracle19c in Centos8

Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released

LeetCode_双指针_中等_328.奇偶链表

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

倍福控制器连接松下EtherCAT伺服注意事项

Interpolated scatter data

1. opencv realizes simple color recognition

Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)
随机推荐
Adjacency matrix and adjacency table structure of C # realization graph
Uncover the practice of Baidu intelligent test in the field of automatic test execution
InDesign plug-in - general function development -js debugger open and close -js script development -id plug-in
Testing -- automated testing: about the unittest framework
Bison uses error loop records
oracle 19c : change the user sys/system username pasword under Linux
C#实现堆栈结构的定义、入栈、出栈
C#实现二叉树的层次遍历
墨菲安全入选中关村科学城24个重点项目签约
在印度与软件相关的发明可不可以申请专利?
Can software related inventions be patented in India?
qt 自定义控件 :取值范围
二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
[JUC series] ThreadLocal of synchronization tool class
Gbase8s database into temp clause creates a temporary table to save query results.
Interview shock 61: tell me about MySQL transaction isolation level?
Set operator of gbase8s database in combined query
[comprehensive case] credit card virtual transaction identification
How to install oracle19c in Centos8
1. Opencv实现简单颜色识别