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?
-
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\$– tkauslCommented 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\$– pepper5319Commented 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\$– tkauslCommented 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\$– pepper5319Commented 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\$– tkauslCommented Nov 7, 2014 at 3:02
1 Answer
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.
-
\$\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\$– AnkoCommented Oct 5, 2015 at 9:32
-