As I kept building my game, I started to notice that I was putting all the methods in the main class and it got too big and hard to follow. I started to refactor it into multiple modules / classes and for example I created a new module called 'game_collisions' and a class named CollisionManager, and in that class I moved all collision related methods from the main class. This is the class:
class CollisionManager:
"""The Collision Manager class manages collisions between game entities like
ships, aliens, bullets, and asteroids."""
def __init__(self, game):
self.game = game
self.stats = game.stats
self.settings = game.settings
self.score_board = game.score_board
And one of the methods for example, is this one:
def check_asteroids_collisions(self, thunderbird_hit, phoenix_hit):
"""Check for collisions between the ships and asteroids"""
# loop through each player and check if it's alive,
# then check for collisions with asteroids and which player collided
# and activate the corresponding method
for ship in self.game.ships:
if ship.state['alive']:
if collision := pygame.sprite.spritecollideany(
ship, self.game.asteroids
):
if ship is self.game.thunderbird_ship:
thunderbird_hit()
else:
phoenix_hit()
collision.kill()
In the main class I am instantiating the Manager class like this:
self.collision_handler = CollisionManager(self)
And calling the method like this, passing as attributes the appropriate methods:
self.collision_handler.check_asteroids_collisions(self._thunderbird_ship_hit,
self._phoenix_ship_hit)
I did this with most of the methods I moved. And now for my question, Is this good practice? Creating the method in the CollisionManager like this, and calling it with methods from the main class as attributes. Can this lead to something bad? As for being readable, to me it looks good enough. I would appreciate any advice.