Skip to content
Advertisement

How to make entities take damage with color collision in pygame?

How do I make entities take damage with colors? I made a game with enemies shooting blue lasers and I want the player to take damage. And the player shoots a red laser. Upon detecting colors, I want a certain statement to be true. That would lower an entity’s health by one. If color collision is a thing, it can be very useful for making many different games. Here is my code:

JavaScript

Advertisement

Answer

No, there is no such a “thing” as a “color collision”. What color have the images? Are they uniformly colored? Most likely the pictures are only different in some details. A collision which checks for a uniform color is completely useless. A collision that also looks for a uniform color is almost useless.

If you detect a collision, you know the laser and the enemy. Hence, you know the color with which you represent them. All you have to do is do an additional check after the collision is detected.


Use pygame.sprite.Sprite to implement the objects. Add an attribute that indicates the color of the object:

JavaScript

Manage the spites in pygame.sprite.Groups and use spritecollide to detect the collisions. Check the color attributes when a collision is detected

JavaScript

It is even possible to implement your own collision detection method and use it in combination with spritecollide by setting the optional collided argument:

JavaScript
JavaScript

Minimal example:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement