我有以下設置:我有一個射箭者可以射箭,總是箭類的新實例,我有一個blackMonster類的實例。我的問題是,是否有可能檢測到我的一個箭頭實例是否與這個黑色怪物實例發生碰撞?什麼時候有可能如何製造?我曾經經常這麼想,而且很多,但是我還找不到解決方案,而且它變得非常令人沮喪。這裏是我的代碼...請不要太難評判我即將開始工作。在python和tkinter中檢測碰撞
from Tkinter import *
from PIL import ImageTk, Image
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, height=400, width=400)
self.master = master
self.master.bind('<Shift_L>', self.createArrow)
self.charImg = ImageTk.PhotoImage(Image.open("./Archer.gif"))
self.charLabel = Label(self, image = self.charImg)
self.charLabel.pack()
self.down = False
self.right = False
self.left = False
self.up = False
self.x_coord = 200
self.y_coord = 200
self.pack_propagate(0)
self.pack()
self.monster = blackMonster(self)
self.monster.createMonster(self.monster, 300, 300)
def createArrow(self, event):
self.arrow = Arrow(self)
self.arrow.moveArrow(self.arrow, self.x_coord, self.y_coord + 15)
def moveableImage(self):
self.charLabel.place(y=self.y_coord, x=self.x_coord)
def keyPressed(self, event):
if event.keysym == 'Down':
self.down = True
elif event.keysym == 'Right':
self.right = True
elif event.keysym == 'Left':
self.left = True
elif event.keysym == 'Up':
self.up = True
def keyReleased(self, event):
if event.keysym == 'Down':
self.down = False
elif event.keysym == 'Right':
self.right = False
elif event.keysym == 'Left':
self.left = False
elif event.keysym == 'Up':
self.up = False
def task(self):
if self.down and self.y_coord < 360:
self.y_coord += 10
elif self.right and self.x_coord < 370:
self.x_coord += 10
elif self.left and self.x_coord > 10:
self.x_coord -= 10
elif self.up and self.y_coord > 10:
self.y_coord -= 10
root.after(20,self.task)
self.moveableImage()
class Arrow(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.arrowImage = ImageTk.PhotoImage(Image.open("./arrow.gif"))
Label(self, image=self.arrowImage).pack()
self.damage = 20
def moveArrow(self, arrow, xCoord, yCoord):
arrow.place_forget()
arrow.place(x = xCoord, y = yCoord)
self.after(10, self.moveArrow, arrow, xCoord+5, yCoord)
class blackMonster(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.monsterImage = ImageTk.PhotoImage(Image.open("./monster.gif"))
Label(self, image=self.monsterImage).pack()
self.health = 100
def createMonster(self, monster, x_Coord, y_Coord):
monster.place(x = x_Coord, y = y_Coord)
root = Tk()
root.title("Frametitel")
app = App(master=root)
root.bind_all('<Key>', app.keyPressed)
root.bind_all('<KeyRelease>', app.keyReleased)
root.after(20, app.task)
app.mainloop()
你的代碼的縮進被搞砸了。如果你發佈了正確縮進的代碼,這將有所幫助,因爲否則就不可能知道python中的代碼行屬於哪個塊。 –