2017-04-23 113 views
1

我想在Tkinter中做一個非常簡單的小遊戲(不想使用Pygame,請關於Pygame沒有任何建議),我正在做碰撞檢測在畫布上使用find_overlapping,但是它說當我的兩個盒子沒有彼此靠近時,事物是重疊的。Python:Tkinter:find_overlapping當沒有重疊時返回一個結果

這是我的播放器類:

from tkinter import * 

class Player(Canvas): 

    def __init__(self,parent,frame): 
     Canvas.__init__(self,parent) 
     self.parent = parent 
     self.frame = frame 

    def drawPlayer(self): 
     self.pImage = PhotoImage(file="D:\James\Pictures\Test Images\Test_Image.png") 
     self.image = self.parent.create_image(100,100,image=self.pImage, anchor="nw") 

     self.frame.bind("<Key-Up>", self.movePlayerUp) 
     self.frame.bind("<Key-Down>", self.movePlayerDown) 
     self.frame.bind("<Key-Left>", self.movePlayerLeft) 
     self.frame.bind("<Key-Right>", self.movePlayerRight) 

     self.bounds = self.parent.bbox(self.image) 
     print(self.bounds) 

    def movePlayerUp(self, event): 
     self.playerCoords = self.parent.coords(self.image) 
     self.bounds = self.parent.bbox(self.image) 
     print(self.bounds) 
     print(self.parent.find_overlapping(self.bounds[0],self.bounds[2]-1,self.bounds[1],self.bounds[3]-1)) 
     if self.playerCoords[1] > 2: 
      self.parent.move(self.image,0,-2) 
      print("up") 

    def movePlayerDown(self, event): 
     self.playerCoords = self.parent.coords(self.image) 
     self.bounds = self.parent.bbox(self.image) 
     print(self.bounds) 
     print(self.parent.find_overlapping(self.bounds[0],self.bounds[2]+1,self.bounds[1],self.bounds[3]+1)) 
     if self.playerCoords[1] + 20 < 301: 
      self.parent.move(self.image,0,2) 
      print("down") 

    def movePlayerLeft(self, event): 
     self.playerCoords = self.parent.coords(self.image) 
     self.bounds = self.parent.bbox(self.image) 
     print(self.bounds) 
     print(self.parent.find_overlapping(self.bounds[0]-1,self.bounds[2],self.bounds[1]-1,self.bounds[3])) 
     if self.playerCoords[0] > 2: 
      self.parent.move(self.image,-2,0) 
      print("left") 

    def movePlayerRight(self, event): 
     self.playerCoords = self.parent.coords(self.image) 
     self.bounds = self.parent.bbox(self.image) 
     print(self.bounds) 
     print(self.parent.find_overlapping(self.bounds[0]+1,self.bounds[2],self.bounds[1]+1,self.bounds[3])) 
     if self.playerCoords[0] + 20 < 301: 
      self.parent.move(self.image,2,0) 
      print("right") 

,這是我的主類:

from tkinter import * 
from time import * 
import sqlite3 
import ModulePlayer 

class Loops(Frame): 

    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent 

     self.allow = False 
     self.i = 1 

     self.canvas = Canvas(self.parent, width=300, height=300, bg="lime") 
     self.testBox = self.canvas.create_rectangle(40,40,130,90) 
     ModulePlayer.Player(self.canvas,self.parent).drawPlayer() 

     self.canvas.pack() 

root = Tk() 
root.resizable(False, False) 
root.geometry("600x400") 

Loops(root).pack() 

root.mainloop() 

所以,我有我的球員開始在x = 100,Y = 100,我有一個盒子在x1 = 40,x2 = 130,y1 = 40,y2 = 90時,當我的玩家在x1 = 78,x2 = 98,y1 = 100,y2 = 120時,find_overlapping表示有2個對象當前重疊,當我在我的testBox裏面,它是說有0。它似乎隨機檢測到我重疊的東西,除了我的播放器和canv本身。

我想這可能是因爲find_overlapping技術上與我的球員圖像重疊,但如果是這樣的話,那麼它會始終返回至少1,但通常會返回0

如果我刪除testBox矩形然後它總是返回0,無論它在哪裏,所以我知道這是有關的。

我試過尋找,我似乎無法找到其他人有這個問題。我對find_overlapping的工作方式有些根本性的錯誤了嗎?或者,我對python/tkinter的工作方式有些根本性的錯誤?我還是比較新,但這對我來說毫無意義......

回答

1

忽略這一點。找到答案。按錯誤順序得到了x和y。我只是一個白癡。