2014-02-09 3 views
0

사용자가 블록을 제어하고 파이프를 통해 탐색하는 프로그램이 있습니다.이 게임은 flappy bird와 비슷합니다. 하나의 사각형이 다른 하나의 사각형에 닿거나 충돌하는 경우 tkinter에 내게 알려주는 방법이 있는지 알고 싶습니다. 캔버스에 대한 find_overlapping 메서드가 있다는 것을 알고 있지만 파이프의 사각형에 find_overlapping을 사용하면 파이프의 ID가 나옵니다! 나는 캔버스 아이템이 다른 캔버스 아이템이 그것을 만지는지를 알 수있는 방법이 있는지 알고 싶습니다!캔버스 아이템 찾기 다른 캔버스 아이템을 만지고 있습니다. Tkinter

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

Canvas.find_overlapping 돌아 가기 BBOX의 형태의 모든 ID를 가진 튜플 :

덕분에 여기

내 코드입니다. 아이디가 반환 된 경우 유일한 아이디이기 때문일 수 있습니다.

canvas.find_overlapping(*canvas.bbox(aShape))과 같은 다른 모양의 bbox과 함께 사용할 수 있습니다.

사각형에 겹침이 적용되고 원에서 잘못 될 수 있습니다.

+0

아니요.하지만 파이프가 완전히 바운딩되어 있기 때문에 파이프의 ID를 반환합니다. – user2658538

+0

사실, 충돌에 대해 테스트하고있는 모양은 find_overlapping 출력에 있지만 튜플에 다른 모양이있을 수 있습니다. 따라서 충돌 테스트는 find_overlapping 출력의 길이를 테스트하기 위해 (이미 예제에서와 같이) 수행 할 수 있습니다. – FabienAndre