0
\$\begingroup\$

Is there a way to tell if a sprite is touching a specific color on the screen, maybe given as a hex code. I know you can do pygame.sprite.collide for sprites, but how can you do this for a specific color on the whole screen?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Can you explain this a little bit more please? I dont understand why one would need this, since colliding is colliding, whether green, blue or red. \$\endgroup\$
    – tkausl
    Nov 7, 2014 at 2:51
  • \$\begingroup\$ @tkausl Like not colliding with another sprite, but with any color on the screen. Like if the background has a black line, how could i tell if the main sprite is touching that color. \$\endgroup\$
    – pepper5319
    Nov 7, 2014 at 2:52
  • \$\begingroup\$ I'm still not sure what you are trying to do but maybe pygame.org/docs/ref/mask.html will help (with a collisionmap or something). \$\endgroup\$
    – tkausl
    Nov 7, 2014 at 2:54
  • \$\begingroup\$ @tkausl Like if the background has a black line, how could i tell if the main sprite is touching that color. \$\endgroup\$
    – pepper5319
    Nov 7, 2014 at 2:56
  • \$\begingroup\$ Oh now i get it. Check groups.google.com/forum/#!topic/pygame-mirror-on-google-groups/… the first answer. I dont know pygame but this seems a good approach (unfortunately, googeling "pygame collision with background" gives really bad results) \$\endgroup\$
    – tkausl
    Nov 7, 2014 at 3:02

1 Answer 1

1
\$\begingroup\$

Without knowing more about your game and why you want to do this, you might go about it this way:

At initialization, for every graphic drawn in your game, run it through a filter to find if they have that color in them at all. If it does, set a flag "hasThatColor" on the sprite and make a copy of them where you clear all pixels except the ones in that color.

In the main loop of your game during collision detection, look for the flag "hasThatColor" and if that flag is set, collide using the alternative sprite. It will of course only hit when a pixel is there of that color.

\$\endgroup\$
2
  • \$\begingroup\$ This sounds fragile. For example, if another sprite is drawn on top of that graphic such that the pixels of the collision-color are obscured (which means they should not collide), this method will cause a collision against the object anyway. \$\endgroup\$
    – Anko
    Oct 5, 2015 at 9:32
  • \$\begingroup\$ That is very true. \$\endgroup\$ Oct 5, 2015 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .