The way it should work is, if the Pokémon trainer defeats the gym leader, the trainer gets a badge. None of that touchy-feely "you get a badge because you proved your kindness" crap, like in this NSFW cartoon by egoraptor. Brock finds this out the hard way, though.
PokeAwesome 2 - Getting a Gym Badge [egoraptor]
Advertisement