I am programming this game in pygame and for the collision detection I used the Masks.
I was expecting that the collision would only detect, if my spaceship touches an asteroid or laser. But it is not happing so. The collision is getting detected only sometime and it is also getting detected if the spaceship is not even touching the laser or asteroid.
This is my code for my collision detection:
if spaceship_mask.overlap(laser_mask, (spaceship_rect.x - laser_rect1.x, spaceship_rect.y - laser_rect1.y)) and not laser1_collided:
lives -= 1
laser1_collided = True
hit.play()
print("1")
if spaceship_mask.overlap(laser_mask, (spaceship_rect.x - laser_rect2.x, spaceship_rect.y - laser_rect2.y)) and not laser2_collided:
lives -= 1
laser2_collided = True
hit.play()
print("2")
if spaceship_mask.overlap(laser_mask, (spaceship_rect.x - laser_rect3.x, spaceship_rect.y - laser_rect3.y)) and not laser3_collided:
lives -= 1
laser3_collided = True
hit.play()
print("3")
if spaceship_mask.overlap(laser_mask, (spaceship_rect.x - laser_rect4.x, spaceship_rect.y - laser_rect4.y)) and not laser4_collided:
lives -= 1
laser4_collided = True
hit.play()
print("4")
if spaceship_mask.overlap(laser_mask, (spaceship_rect.x - laser_rect5.x, spaceship_rect.y - laser_rect5.y)) and not laser5_collided:
lives -= 1
laser5_collided = True
hit.play()
print("5")
if spaceship_mask.overlap(astroid_mask, (spaceship_rect.x - astroid_rect1.x, spaceship_rect.y - astroid_rect1.y)) and not astroid1_collided:
lives -= 1
astroid1_collided = True
hit.play()
print("1")
if spaceship_mask.overlap(astroid_mask, (spaceship_rect.x - astroid_rect2.x, spaceship_rect.y - astroid_rect2.y)) and not astroid2_collided:
lives -= 1
astroid2_collided = True
hit.play()
print("1")
if spaceship_mask.overlap(astroid_mask, (spaceship_rect.x - astroid_rect3.x, spaceship_rect.y - astroid_rect3.y)) and not astroid3_collided:
lives -= 1
astroid3_collided = True
hit.play()
print("1")
if spaceship_mask.overlap(astroid_mask, (spaceship_rect.x - astroid_rect4.x, spaceship_rect.y - astroid_rect4.y)) and not astroid4_collided:
lives -= 1
astroid4_collided = True
hit.play()
print("1")
New contributor
AryaTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0