user73400
user73400

Reputation:

Colliding Rects

I have been working on this game, but I have a question when it comes to collision detection. Can I check for collision and have it return True:

example:

def collide(self, EnemyTank):
     tank_collision = pygame.sprite.collide_rect(self.rect, EnemyTank.rect)
     if tank_collision == True:
          return True

And then make it perform an action like this:

if player.collide == True:
     e_tank_x += 0

I am new to programming so please bear with me, I am trying as hard as I can and any comments or suggestions would also be very appreciated.

Upvotes: 1

Views: 92

Answers (1)

EnricoGiampieri
EnricoGiampieri

Reputation: 6095

I'm not an expert of pygame, but it sound perfectly legitimate. I would just take away the middle man in your initial function:

def collide(self, EnemyTank):
      return pygame.sprite.collide_rect(self.rect, EnemyTank.rect)

And you need to adjust the test you use, as you want to actually check the collision, while as you wrote it it just test if the function collide exist ;) You can also use the implicit testing, removing some unnecessary character (best abith are best learned early)

if player.collide(EnemyTank):
     do your action here

good luck with your game!

Upvotes: 3

Related Questions