# 超级玛丽上下左右矩形碰撞测试程序

### Python海龟宝典含200多个原创的用turtle模块制作的创意程序，原名《Python趣味编程200例》。准备参加全国创意编程与智能设计大赛的同学们可以用来做参考。

```"""
超级玛丽上下左右矩形碰撞检测程序.py
本程序需要gameturtle模块0.2版支持。
"""
from gameturtle import *

class Key:
def __init__(self,cv,key):
self._canvas = cv
self._key = key
self._down = False
self._canvas.bind("" % key,self._press)
self._canvas.bind("" % key,self._release)

def _press(self,event):
self._down = True

def _release(self,event):
self._down = False

def isdown(self):
return self._down

root = Tk()
cv = Canvas(width=480,height=360)
cv.pack()

# 加载资源
mario_pics = ['bgs/mariox.png','bgs/mario2x.png']
mario_pics = [Image.open(im) for im in mario_pics]

# 生成玛丽奥
mario = GameTurtle(cv,mario_pics,pos=(40,40))
mario.dy = 0

square1 = GameTurtle(cv,Image.new("RGBA",(240,120),color='red'))

up_key = Key(cv,"Up")
down_key = Key(cv,"Down")
right_key = Key(cv,"Right")
left_key = Key(cv,"Left")

cv.focus_force()

while True:
if mario.left_collide(square1):root.title('左碰')
if mario.right_collide(square1):root.title('右碰')
if mario.top_collide(square1):root.title('上碰')
if mario.bottom_collide(square1):root.title('下碰')

cv.update()
time.sleep(0.01)

```