Python精灵模块的pixelcollide指令

所有命令 单独命令 屏幕命令 角色命令

角色命令 >> Python精灵模块的pixelcollide指令

简介:像素级碰撞检测方法,self是主碰方,other是另一个角色,为被碰方。
本方法用self去碰撞other,other为相对不动的,只适合于图形角色。
threshold是一个阈值。我们认为超过阈值的为不透明的地方,否则为透明的。
返回重叠的第一个坐标点,重叠区域矩形,self在此点的像素值,other在此点的像素值。
例子:
"""
pixelcollide像素级碰撞检测命令返回结果说明。
本例中返回的是r,的包括以下内容:
(hitx,hity),pixel1,pixel2,ret)

hitx,hity是相对于整个画布的碰撞点坐标
pixel1是主碰方,在这里是箭头在碰撞点的像素值
pixel2是被碰方,在这里是彩色方块在碰撞点的像素值
ret是两者重叠的矩形区域。
"""
from sprites import Sprite,mouse_pos,Mouse

arrow = Sprite('a.png')
cat = Sprite('square.png')
screen = cat.screen

leftkey = Mouse()

while True:     
    mx,my = mouse_pos()
    arrow.goto(mx,my)
    if leftkey.downup():   # 如果按下并松开
        arrow.left(90)
    r = arrow.pixelcollide(cat)
    print(r)    
writed by lixingqiu 关注"异编程"微信公众号,获取更多关于Python精灵模块的咨讯: