A horned lizard sits motionless in the desert sun, eyeing a young coyote skulking nearby. Five inches long, with a crown of horns like a dinosaur, the lizard's mottled skin helps it blend into the background.

Nevertheless, the coyote spots it. The predator pounces and holds the lizard down. Then it gingerly nibbles – and immediately regrets it.

A stream of nasty-tasting blood squirts from the lizard's eyes, straight into the coyote's mouth. The coyote steps back, shaking its head from side to side in disgust. It retreats, wiping its muzzle, while the uninjured lizard skitters away to safety.

To the disappointment of kids who want to see them squirt blood, horned lizards rarely squirt at humans. If you catch one, it will sit demurely in your palm. It won't bite. It might hiss, or puff itself up at little. But only canids and felids like bobcats, are treated to a stream of foul-tasting blood.

The blood squirt is far from their only trick. Horned lizards have evolved a unique arsenal of defences, which they use selectively to drive off the many predators that attack them. They might look like miniature Smaugs, but they need all the help they can get not to end up as dinner.

There are 17 known species of horned lizard, all in the genus Phrynosoma. Most of them seem to be capable of squirting blood.

Actually, anything that can find one would eat it

They differ in colour, size and the number and arrangement of horns and spines along their backs. Most are about the size of a pack of playing cards. They have wide, flat bodies, like spiky satellite dishes, and short stumpy legs.

"They are short-legged and stubby-bodied," says Larry Powell of the University of Calgary in Alberta, Canada. "When they run, they give it their best, but they're just not built for speed."

Instead of being sleek like an iguana, horned lizards are built for defence. They have to be, since so many things try to eat them.

Horned lizards mostly live in dry places: from Guatemala and Mexico, through the deserts of Arizona and California, to the dry prairies of southern Canada. They are prey for hawks, shrikes, roadrunners, snakes, coyotes, foxes, wolves, bobcats and even carnivorous grasshopper mice. "Actually," Powell says, "anything that can find one would eat it."

So they do their best not to be found.

Camouflage is their first line of defence. They match their colours to their background, blending in against brown scrubby brush or the mottled greys of mud.

Some species mimic inedible objects. The round-tailed horned lizard is almost indistinguishable from the rocks it hides in, when it hunches up its back and tucks in its legs.

I'm trying to get an idea of what's going on in the lizard's brain

Many predators detect prey through movement, so horned lizards have mastered the art of stillness. The arrangement of the horns along their sides even breaks up the shadows they cast on the ground, like a cloak of invisibility.

"They all stick to this basic program of being short, tubby, spiky, and hard to find," says Powell.

On a good day, Powell is lucky to find three. "I sometimes wonder if the only individuals we get data on are the ones with bad nerves, the ones who run, since if they stay put you probably won't see them."

When a predator approaches, the lizards are reluctant to move. They only do so after evaluating the danger. They carefully consider what is attacking, and what the appropriate defence might be.

Wade Sherbrooke of the American Museum of Natural History, based in Tucson, Arizona, has spent almost four decades trying to understand how horned lizards think. In 2014, P. sherbrookei was named after him.

"I'm trying to get an idea of what's going on in the lizard's brain, how it's categorizing the world out there that it has to deal with," says Sherbrooke. It all seems to come down to the predator's attack style.

Canids such as dogs use teeth and claws to tear their prey into bite-size pieces. Snakes swallow their meals whole. Meanwhile, a grasshopper mouse will prefer to nibble, zombie-style, through the skull to get to the brains.

It's sort of like a person wearing a fat suit

Sherbrooke set Texas horned lizards in cages, and pitted them against whip snakes and rattlesnakes to see if the lizards could tell the difference. No problem, it turns out. "They're better at identifying snakes than most people in Tucson," says Sherbrooke.

Whip snakes are lightning fast, and actively hunt their prey. The portly horned lizard cannot outrun one, so it opts for camouflage and keeping still.

However, rattlesnakes don't chase. They wait for prey to come to them before biting, injecting venom and swallowing. So when a horned lizard encounters a rattlesnake, it runs for its life, safe in the knowledge that the snake will probably not follow.

Neither defence works every time, but on average, standing still for a whip snake and running from a rattlesnake are a horned lizard's best bets.

If the lizard does get caught, all is not lost.

Snakes, and some birds such as roadrunners, need to swallow their prey whole. That means they have to get their mouth around the entire body. A horned lizard will not make it easy.

A Texas horned lizard or Alberta's short-horned lizard will tip up on its front legs, fan out its ribs to form a dorsal shield, or puff up its torso to make itself as big as possible. "It's sort of like a person wearing a fat suit," says Powell.

The technique works. A whip snake trying to get its mouth around a Texas horned lizard may well give up, because it just can't fit the whole thing in.

Instead of puffing up, regal horned lizards roll over. When threatened by a whip snake, the lizard flips onto its back.

This exposes a pure white belly, white legs, and the white outline of its spines along its edge – which rather resemble teeth. The lizard also splays its short legs out stiffly at its sides, like a wrestler struggling not to be turned over.

Some scientists suggest they are playing dead, but Sherbrooke disagrees. "What good does playing dead do when you have a predator trying to eat you?" he asks.

He thinks they are advertising their size and spininess. "If you're small, you get eaten by a lot of things," he says, so appearing bigger and spikier might be useful if your adversary is trying to swallow you whole.

Finally, there is horned lizards' best-known defence: blood squirting. They reserve this for two groups of predators: cats and canids, the group that includes dogs, coyotes and wolves.

The process is quite simple. A pouch below the lizard's eyes, the ocular sinus, swells as it fills with blood. With a sudden surge of pressure, blood forcefully squirts out in a stream that can travel up to 6ft (2m).

They shake their heads, they salivate profusely, and try to clear the material out of their mouths

To show that horned lizards can discern dogs from other large predators, Sherbrooke recruited a golden retriever called Dusty. She was trained to bark, paw and gently nibble at a horned lizard. Within a minute of Dusty barking, each lizard squirted her. "She was the perfect dog," says Sherbrooke.

In contrast, the lizards were less concerned when Sherbrooke tried to mimic Dusty's behaviour. "I got down on all fours and I barked, used my front 'paws' to paw her, but I didn't bite," he says.

The lizards were not fooled, and mostly ignored his dog impersonation. "I was somewhat relieved that I wasn't that easily mistaken for a dog," he says.

Canids are not averse to blood, but they dislike horned lizard blood, even though it is not poisonous.

"They have an almost violent reaction," Sherbrooke says. "They shake their heads, they salivate profusely, and try to clear the material out of their mouths." It takes about 15 minutes to recover.

The blood contains a chemical that binds to receptors in the canid's mouth: receptors that humans apparently lack. "I've tasted the blood many, many times," says Sherbrooke. He never detected anything more than a mild acrid aftertaste.

The blood is most effective when delivered directly into the mouth, rather than the eyes or nose. This might explain why horned lizards often wait until the last second, when they are already in their attacker's jaws, before squirting.

If you eat ants, it's like living on M&Ms

The lizards probably get the unpleasant chemical in their blood from their food. They mostly eat ants, and many eat highly venomous harvester ants.

This does have a downside, though: they even need a defence against their own food.

A horned lizard will lap up its food with the tip of its sticky tongue. It does not chew the ants. Instead, before swallowing, it wraps the ants in strands of thick mucus secreted by special cells at the back of its throat. This protects the lizard from the ants' stings, allowing them to exploit a resource that most animals can't.

The lizards' ant-based diet might be what drove them to evolve so many defences.

"If you eat ants, it's like living on M&Ms," says Powell. "Individually they don't contribute a lot."

To make up for it, a horned lizard has to eat a lot of ants, and to do that it needs a big stomach. The desert horned lizard has a stomach that makes up 13% of its mass: akin to a 150-pound person with an almost 20-pound stomach.

They can give birth to up to 48 live offspring at a time

To accommodate such a large stomach, ant-eaters need to be stocky. "If you're going to be stocky you can't run fast," says Powell. "If you can't run fast, you're going to rely on camouflage or horns, and you end up with all these defensive behaviours."

Of course, sometimes camouflage, armour and squirting blood aren't enough, and a horned lizard gets eaten anyway. But even then they sometimes manage one last act of defiance: it's not unheard-of for a horned lizard to become lodged in the throat or stomach of a bird or snake, killing its attacker.

Besides, horned lizards have one last defence against death: they have lots of babies. Their wide abdomens accommodate larger broods than most lizards, so they can give birth to up to 48 live offspring at a time, or lay up to 45 eggs.

So while an individual horned lizard might succumb to a particularly determined coyote or carnivorous mouse, it has a good chance of passing on its genes before it does.