我正在尝试用Tkinter做一个非常简单的小游戏(我不想使用Pygame,请不要建议使用Pygame),我在画布上使用find_overlapping进行碰撞检测,但是它说当我的两个盒子彼此不在一起的时候,东西是重叠的。
这是我的player类:
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说有两个对象当前重叠,当我在我的testBox中时,它说有0。它似乎随机检测到我重叠了一些东西,而那里除了我的球员和画布本身之外什么都没有。
我想这可能是因为find_overlapping在技术上与我的球员图像重叠,但如果是这样的话,它总是返回至少1,但通常它会返回0。
如果我删除testBox矩形,那么无论它在哪里,它总是返回0,所以我知道它与此有关。
我尝试过搜索,但似乎找不到其他有此问题的人。我是不是从根本上理解了find_overlapping的工作方式?或者我对python/tkinter的工作方式有什么根本性的错误?我对它还比较陌生,但这对我来说没有任何意义...
发布于 2017-04-23 10:02:26
忽略这一点。找到答案了。X和y的顺序错了。我只是个笨蛋。
https://stackoverflow.com/questions/43566605
复制相似问题