Python:Tkinter:find_overlapping在没有重叠时返回结果
我正试图在Tkinter中制作一个非常简单的小游戏(不想使用Pygame,请不要对Pygame提出任何建议),我正在使用画布上的find_重叠来进行碰撞检测,但是这意味着当我的两个盒子彼此不近时,事情是重叠的 这是我的玩家课程:Python:Tkinter:find_overlapping在没有重叠时返回结果,python,canvas,tkinter,Python,Canvas,Tkinter,我正试图在Tkinter中制作一个非常简单的小游戏(不想使用Pygame,请不要对Pygame提出任何建议),我正在使用画布上的find_重叠来进行碰撞检测,但是这意味着当我的两个盒子彼此不近时,事情是重叠的 这是我的玩家课程: from tkinter import * class Player(Canvas): def __init__(self,parent,frame): Canvas.__init__(self,parent) sel
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")
我的播放器从x=100,y=100开始,在x1=40,x2=130,y1=40,y2=90有一个盒子,但是当我的播放器在x1=78,x2=98,y1=100,y2=120时,find_重叠表示当前有两个对象重叠,然后当我在测试盒内时,它表示有0。它似乎随机检测到我在重叠某个东西,而除了我的播放器和画布本身,没有其他东西
我想这可能是因为find_重叠在技术上与我的播放器图像重叠,但如果是这种情况,那么它总是至少返回1,但通常会返回0
如果我删除testBox矩形,那么无论它在哪里,它总是返回0,所以我知道这与此有关
我试着搜索,但似乎找不到其他有这个问题的人。我发现重叠工作的方式有什么根本性的错误吗?或者说我在python/tkinter的工作方式上有什么根本性的错误?我对它还比较陌生,但这对我来说毫无意义…忽略这一点。找到了答案。x和y的顺序不对。我只是个白痴
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()