Jacknoshima
Jacknoshima

Reputation: 29

Python: Tkinter: find_overlapping returning a result when nothing overlapping

I'm trying to make a really simple little game in Tkinter (don't want to use Pygame, please no suggestions about Pygame), and I'm doing collision detection using find_overlapping on the canvas, however it's saying that things are overlapping when my two boxes are no where near each other.

This is my player class:

    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")

And this is my Main class:

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()

So I have my player start at x=100, y=100 and I have a box at x1=40, x2=130, y1=40, y2=90, yet when my player is at x1=78,x2=98,y1=100,y2=120 the find_overlapping is saying there are 2 objects currently overlapping, then when I'm inside my testBox, it's saying there's 0. It seems like it randomly detects that I'm overlapping something when nothing else is there except my player and the canvas itself.

I thought it might be because the find_overlapping technically overlaps with my player image, but if that were the case then it'd always return at least 1, but often it'll return 0.

If I remove the testBox rectangle then it always returns 0 no matter where it is, so I know it's something to do with that.

I tried searching and I can't seem to find anyone else that's had this issue. Am I getting something fundamentally wrong about the way that find_overlapping works? Or am I getting something fundamentally wrong about the way python/tkinter works? I'm still relatively new to it, but this just makes no sense to me...

Upvotes: 1

Views: 908

Answers (1)

Jacknoshima
Jacknoshima

Reputation: 29

Ignore this. Found the answer. Got x's and y's in the wrong order. I'm merely an idiot.

Upvotes: 1

Related Questions