# 移动B图去碰撞A图并且画重叠区域_像素极碰撞的基本理论演示程序

### 移动B图去碰撞A图并且画重叠区域_像素极碰撞的基本理论演示程序

```"""
移动B图去碰撞A图并且画重叠区域.py
本程序演示了像素极碰撞的基本理论。
"""
__author__ = '李兴球'
__date__ = '2020/10/15'
__blog__ = 'www.lixingqiu.com'

import numpy as np
from rect import Rect
from gameturtle import *

def make_croped_area(overlaped,rectangle):
"""返回将要剪裁的区域
overlaped:Rect对象，相对于画布坐标系的
rectangle:Rect对象，也是相对于画布坐标系的
返回left,top,right,bottom，相对于rectangle的
"""
left = overlaped.left - rectangle.left
top = overlaped.top - rectangle.top
right = left + overlaped.width
bottom = top + overlaped.height
return left,top,right,bottom

"""
image:pillow图形对象
area:图形对象上的一个区域
"""
im = image.crop(area)
im_array = np.array(im)

def draw_rect(r):
if r:      # 如果重叠了
v.clear()
v.goto(r.left,r.top)
v.pendown()
for _ in range(2):
v.fd(r.width)
v.rt(90)
v.fd(r.height)
v.rt(90)
v.penup()
else:
v.clear()

root = Tk()
root.title('像素级碰撞理论示意原理图')

cv = Canvas(width=480,height=480,bg='#98f9f9')
cv.pack()

dot = GameTurtle(cv,Image.new("RGBA",(5,5),color='red'))

v = GameTurtle(cv,Image.new("RGBA",(1,1)))
a_pic = GameTurtle(cv,Image.open('a.png'))
a_pic.rect = Rect(140,140,200,200)
b_pic = GameTurtle(cv,Image.open('b.png'))

cv.tag_raise(dot.item)               # 升起来

while True:
mx,my = b_pic.mouse_pos()

b_pic.goto(mx,my)
b_pic.rect = Rect(mx-100,my-75,200,150)
r = b_pic.rect.overlap(a_pic.rect)
if r:
draw_rect(r)
r_a = make_croped_area(r,a_pic.rect)    # 返回在a图上的待剪区域
r_b = make_croped_area(r,b_pic.rect)    # 返回在b图上的待剪区域

# im_a是在a图上剪下来的图形
array = np.argwhere(mask == 2)          # 所有碰撞点的行列号
if array.size > 0:
top,left = array[0]                  # 第一个点的行列号
y,x = int(top) + r.top,int(left) + r.left # 相对于画布的坐标
dot.goto(x,y)                   # 红点到此

p1 = im_a.getpixel((int(left),int(top)))
p2 = im_b.getpixel((int(left),int(top)))
s = str(x) + ',' + str(y) + ', 在a图上的像素值' + str(p1)
s = s + ', 在b图上的像素值' + str(p2)
root.title(s)# 显示碰撞点坐标
else:
root.title("")
v.clear()

cv.update()

```