user2196555
user2196555

Reputation: 11

Pygame Collision on Rect

I'm a student in Computer Programming at my high school and really need help to make the square an obstacle that ships can't enter and bullets disappear once you shoot at the square also for the circle the same thing. And both ships die if they collide with each other. Please help I know my question isn't the best worded but i really need help.

the link to the code is here https://docs.google.com/file/d/0B-Pb_T-Vgr3-TnhRY0lvVjJHX0U/edit?usp=sharing

Upvotes: 1

Views: 282

Answers (1)

Jami Couch
Jami Couch

Reputation: 451

I would suggest that you create a GameObject class which extends pygame.sprite.Sprite and both Ship and Bullet classes which extend GameObject. This allows you to then easily add properties that both will need, such as velocity and acceleration, and you can create a collide method that is overridden by both for specific behavior. The added bonus here is that pygame.sprite.collide_rect will work properly as such:

if (pygame.sprite.collide_rect(sprite1, sprite2)):
    # sprite1 and sprite2 are colliding!
    # do something, such as calling sprite1.collide(sprite2) 
    # and sprite2.collide(sprite1)

So, pygame.sprite.collide_rect checks if two sprites collide using the Sprite.rect property of each. This functionality could also be recapitulated using rect.colliderect:

sprite1.rect.colliderect(sprite2.rect)

Upvotes: 1

Related Questions