sep-pm-platformer/project/sprite/StaticSprite.py

40 lines
1.2 KiB
Python
Raw Normal View History

2023-03-25 15:41:32 +01:00
import pygame
import sys
from sprite.Sprite import Sprite
from sprite.Spritesheet import Spritesheet
sys.path.append('./sprite')
class StaticSprite(Sprite):
def __init__(self, spritesheet: Spritesheet):
super().__init__(spritesheet)
2023-03-25 17:18:43 +01:00
def collides_with(self, collider: 'StaticSprite', tolerance: float = 0.0):
2023-03-25 15:41:32 +01:00
if not self.is_collider or not collider.is_collider:
return False
self_bounds = self.get_bounding_box()
other_bounds = collider.get_bounding_box()
self_dimensions = self_bounds.get_dimensions()
other_dimensions = other_bounds.get_dimensions()
self_position = self_bounds.get_position()
other_position = other_bounds.get_position()
2023-03-25 17:18:43 +01:00
if self_position[0] + self_dimensions[0] < other_position[0] + tolerance:
2023-03-25 15:41:32 +01:00
return False
2023-03-25 17:18:43 +01:00
if self_position[0] > other_position[0] + other_dimensions[0] - tolerance:
2023-03-25 15:41:32 +01:00
return False
2023-03-25 17:18:43 +01:00
if self_position[1] + self_dimensions[1] < other_position[1] + tolerance:
2023-03-25 15:41:32 +01:00
return False
2023-03-25 17:18:43 +01:00
if self_position[1] > other_position[1] + other_dimensions[1] - tolerance:
2023-03-25 15:41:32 +01:00
return False
return True