Part 14: Collision of cannons and rockets

14_cannon_rocket_sprites_collision

We shall detect the collision of the cannons (circle sprites) with the rocket sprite and detect a problem that we would face.


Adding on to Part 13, we shall add the code for collision as in Part 11

This must be done before the display update.

    #sprite collide is for sprites in a group
    for circle in wall_group:
       if(rocket.rect.colliderect(circle.rect)):
            print("collision  :  " + str(id(circle)))
            #wall_group.remove(circle)
            #wall_group.draw(SCREEN)
            
    pygame.display.update()

If you run this code, the rocket shows collision nowhere near the cannon, the rectangle we got from the circles is huge, that is why.

I tried to get this to work, but failed.
The other methods:

  • The cannons need not be sprites but normal python objects
  • Secondly, use images for cannons instead of drawing circles in the sprite

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s