Question

I have following setup: I have an archer who can shoot arrows, which are always new instances of the class arrow and I have an instance of the class blackMonster. My Question is whether it is even possible or not to detect whether one of my arrow instances had a collision with this black monster instance ? And when it is possible how is it made ? I used to think so often and much about it, but I couldn't find a solution yet and it is getting really frustrating. Here is my code... Please don't judge it too hard I am about to work on it.

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

Was it helpful?

Solution

You're using labels and place to represent the monster and arrow? The coding would be much easier if you used a canvas rather than labels. The canvas has methods to easily get the coordinates of objects that have been draw. Then it's it's nothing more than a little math. You get the current coordinates of the arrow, and you get the coordinates of the monster, then check if the coordinate of the tip of the arrow is inside the space taken up by the monster. You can get the coordinates of ana object using the bbox method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top