Recently I started learning OOP and decided to create a small game in Python using the arcade library. I decided not to use sprites and their built-in functions. Then I had a problem how to determine whether objects are touching. I wrote the following function:
def get_distance(el1, el2): return ((el1.x - el2.x) ** 2 + (el1.y - el2.y) ** 2) ** 0.5
In the update method of the MyGame class, I check:
if get_distance(self.hero, bullet) < 20: self.bullet_list.remove(bullet)
It all works, but if the bullet hits not exactly the middle of the player, then it does not disappear. Help, how can you make the definition of the contact of objects more accurate? Any complicated formula? ..
PS So I draw a player:
def draw(self): arcade.draw_circle_filled(self.x, self.y, self.r, self.color) arcade.draw_line(self.x, self.y, self.x+45*self.dx, self.y+45*self.dy, arcade.color.BLACK, 5)
And this is a bullet:
def draw(self): arcade.draw_line(self.x, self.y, self.x+9*self.dx, self.y+9*self.dy, self.color, 5)