如何找到一个画布项目正在触摸另一个画布项目,Tkinter

时间:2014-02-09 17:03:52

标签: python canvas tkinter flappy-bird-clone

我有一个程序,用户控制一个块并通过管道导航,类似于游戏飞扬的鸟。我想知道tkinter是否有办法告诉我一个矩形是否触及另一个矩形。我知道画布有一个find_overlapping方法,但是当我在管道的矩形上使用find_overlapping时,它给了我管道的id!我想知道画布项是否有办法知道另一个画布项是否正在触摸它!

由于

这是我的代码:

from Tkinter import *
from random import *
root=Tk()
c=Canvas(root,width=600,height=600)
speed=20
num=1
first=True
b=0
pipes=[]
done=False
class Pipe():
    def __init__(self,canvas,pipes,length,width=75,color="Green",position=600,speed=5):
        self.speed=speed
        self.canvas=canvas
        self.length=length
        self.width=width
        self.color=color
        self.position=position
        self.current1=self.canvas.create_rectangle((self.position,0,self.position+self.width,self.length),outline=self.color,fill=self.color)
        self.current2=self.canvas.create_rectangle((self.position,600,self.position+self.width,self.length+150),outline=self.color,fill=self.color)
        self.pipes=pipes
        self.pipes.append(self.current1)
        self.pipes.append(self.current2)
    def move(self):
        global done
        if (len(self.canvas.find_overlapping(self.position,0,self.position+self.width,self.length))==1 and len(self.canvas.find_overlapping(self.position,600,self.position+self.width,self.length+150))==1) and not done:
            self.position-=3
            self.canvas.coords(self.current1,(self.position,0,self.position+self.width,self.length))
            self.canvas.coords(self.current2,(self.position,600,self.position+self.width,self.length+150))
            if self.position>-75:
                self.canvas.after(self.speed,self.move)
            else:
                self.pipes.remove(self.current1)
                self.pipes.remove(self.current2)
                self.canvas.delete(self.current1)
                self.canvas.delete(self.current2)
        else:
            print self.canvas.find_overlapping(self.position,0,self.position+self.width,self.length)
            print
            print self.canvas.find_overlapping(self.position,600,self.position+self.width,self.length+150)
            done=True
class Player():
    def __init__(self,canvas,x=150,y=300,size=40):
        self.size=size
        self.faller=True
        self.x=x
        self.y=y
        self.fell=5
        self.canvas=canvas
        #For now
        self.current=self.canvas.create_rectangle((self.x-20,self.y-20,self.x+20,self.y+20),tags="user",outline="Blue",fill="Blue")
        self.canvas.after(100,self.fall)
        self.canvas.bind("<1>",self.jump)
    def fall(self):
        global done
        if self.faller and not done:
            self.y+=self.fell
            self.fell*=1.001
            self.canvas.coords(self.current,(self.x-20,self.y-20,self.x+20,self.y+20))
            self.canvas.after(30,self.fall)
        elif done:
            a=600-self.y+20
            a/=50
            while self.y<580:
                self.y+=a
                self.canvas.coords(self.current,(self.x-20,self.y-20,self.x+20,self.y+20))

    def jump(self,e):
        if not done:
            self.faller=False
            for x in range(10):
                self.canvas.after(100,self.move)
            self.faller=True
            self.fell=5
    def move(self):
        self.y-=7.5
        self.canvas.coords(self.current,(self.x-20,self.y-20,self.x+20,self.y+20))
    def changey(self,a):
        self.y=a
def run():
    global b,first,done
    if not done:

        if first:
            b=randint(5,450)
            first=False
        else:
            if b<225:
                b=randint(5,b+225)
            if b>225:
                b=randint(b-225,450)
            else:
                b=randint(0,600)
        a=Pipe(c,pipes,b)
        a.move()
        c.after(2000,run)
    else:
        return
root.focus_set()
user=Player(c)
c.after(2000,run)
c.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:3)

Canvas.find_overlapping返回一个包含bbox中所有形状ID的元组。如果仅返回您的id,则可能是因为它是唯一的。

您可以将其与bbox这样的其他形状一起使用:canvas.find_overlapping(*canvas.bbox(aShape))

请注意,重叠适用于矩形,例如圆圈可能有误。