We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Dragon Area

Our Unit class has its simple version of in_area - it just checks if the center point of the unit is within the given area. But the Dragon is a big creature, and it doesn't make sense to check if a single point is within the area. So, we'll use a hitbox instead!

This is the current behavior:

We want to change it so that a dragon is within an area if its hitbox overlaps with it, including when their edges touch:

Since Dragon is a child class of Unit, it can override the in_area of the Unit class with its own behavior. Yay polymorphism! The Dragon still acts like a Unit (has an in_area method), but it has its own implementation.

Assignment

Tips

  • The super() function allows you to call methods of a parent class.
  • To calculate the Dragon's hitbox:
    • x1 should be the dragon's pos_x (center x) minus half of the dragon's width.
    • y1 should be the dragon's pos_y (center y) minus half of the dragon's height.
    • x2 should be the dragon's pos_x (center x) plus half of the dragon's width.
    • y2 should be the dragon's pos_y (center y) plus half of the dragon's height.