Shazam! takes place in the same universe as other films adapted from DC Comics, but writer Henry Gayden (Earth to Echo) and director David F. Sandberg (Lights Out) seem determined to turn that universe upside down. Or at least, they want to blow a raspberry at the glum-and-glummer world established by the Zack Snyder trilogy of Man of Steel, Batman V. Superman, and Justice League, plus their neck-tattoo-sporting companion piece Suicide Squad. The first big-screen starring vehicle for one of the oldest superheroes in existence, a kid who can turn into a superpowered grown-up with the help of a magic word, Shazam! would be tough to turn into a grim-and-gritty DC story.

And that’s because it’s too deeply based in childhood fantasy. One moment, Billy Batson (played as a teenager by Asher Angel, and in his superhero form by Chuck star Zachary Levi) is an ordinary kid with a difficult history. The next, he’s a beefy, cape-wearing hero capable of flying through the air and shooting bolts of electricity from his finger. He’s barely able to convey the joy he takes in his newfound abilities. It’s almost as if superhero stories were at heart about wish fulfillment. It’s almost as if they’re allowed to be fun.

It’s certainly easier for some superhero stories to tap into this kind of gleeful power trip than others. Created by artist C.C. Beck and writer Bill Parker, Batson first appeared in the second issue of Whiz Comics, which hit newsstands in late 1939 as part of the flood of comic books inspired by Superman’s success. In the original comic, a wizard grants Batson the ability to turn himself into the hero Captain Marvel by saying the word “Shazam,” an acronym of “Samson, Hercules, Atlas, Zeus and Mercury,” whose powers contribute to his might. Over time, Captain Marvel picked up a supporting cast that included other kid heroes and a talking tiger, as well as nemeses like the fiendish Dr. Sivana and Mister Mind, an alien worm who headed the Monster Society of Evil.

Fawcett Comics aimed Captain Marvel’s adventures squarely at even younger readers than those devouring rival superhero stories, and he became a hit, outselling even Superman for a good stretch of the 1940s. But interest in superheroes waned at the end of the decade, and a copyright-infringement lawsuit launched by the company now known as DC Comics proved an enemy even Captain Marvel couldn’t defeat. His adventures temporarily ended. But by the early 1970s, Captain Marvel and his extended family had been absorbed into the DC Comics universe. He’s stayed there ever since, though he’s been retrofitted as “Shazam” to avoid confusion with that other Captain Marvel, who’s also just gotten a big-screen debut.

Photo: Warner Bros. Pictures

In Gayden and Sandberg’s film, though, Billy’s superhero alter ego remains nameless, even by the end of the story. Billy and his pal Freddy Freeman (Jack Dylan Grazer) keep cycling through name possibilities, which are mostly awful (“Thundercrack,” for one, is quickly rejected), which serves as a thematically appropriate running gag. Shazam! is the story of a boy trying to figure out what kind of hero he wants to be — and, by extension, what kind of man he should become. He screws up a lot in the process.

With or without the name, the spirit of the old Captain Marvel adventures is very much at the heart of Shazam!, even amid a lot of just-barely PG-13 violence and a couple of gags about a strip club. That’s part of what makes it such a gleeful alternative both to the grimness of past DC films — a tone the company seems eager to shed — and the cosmos-in-the-balance stakes of the Marvel Cinematic Universe. Whether played by Angel or Levi, Billy is just a kid. It’s fun to watch him take delight in his new powers, and a little frightening to realize how little control he has over them. And where Batson’s earliest comics adventures gave him a big city to treat as a playground, Shazam! does the same with Philadelphia. His pleasure at bouncing around the city proves infectious, even though he always seems to be on the verge of accidentally leveling a city block.

His joy is all the more exciting to watch because joy doesn’t come naturally to Billy, who’s had more stacked against him than most teenage boys. He’s spent much of his childhood running away from one foster parent after another in search of the mother he hasn’t seen since he drifted away from her at a carnival at age three. Shortly after the film opens, he lands in what he expects to be another temporary living situation: a Philadelphia group home overseen by a married couple (Marta Milans and Cooper Andrews) who used to be foster kids themselves.

They’re also looking after Freddy (who’s developed a gift for wisecracks as a defense mechanism against those who bully him for using a crutch), college-bound overachiever Mary (Grace Fulton), hug-enthusiast Darla (Faithe Herman), and a handful of other kids. It’s a chaotic but loving environment that instantly embraces Billy — literally, in Darla’s case. Billy can’t wait to flee it. He’s been searching for a home so long, he can’t recognize it when he sees it, with or without superpowers.

That feeling starts to change after his fateful encounter with a wizard named Shazam (Djimon Hounsou, under a lot of facial hair). Confused by the new superheroic abilities Shazam grants him, Billy recruits Freddy to help him explore his own possibilities. After a poky start, Shazam! kicks into gear as the two try to figure out what he can and cannot do with his new powers, whether that’s flying, or buying beer without an ID.

Photo: Steve Wilkie / Warner Bros. Pictures

Gayden and Sandberg attempt a difficult balancing act with Shazam! They have to fulfill a lot of superhero-movie obligations, from introducing an evil arch-nemesis to designing a climactic showdown. Mark Strong — a frequent screen heavy easing back into superhero films for the first time since playing Sinestro in 2011’s misbegotten Green Lantern — makes for an unsettling Dr. Sivana, a man given powers by the Seven Deadly Sins. He’s never as clownish as the Sivana of the comics, but his unbending malevolence makes him a fine foil for the big-screen version of Batson, whose goofiness plays nicely off his nemesis’ scowls. But even when the filmmakers let their project come across as a little frightening, they also have to find a way to stay true to the original comics’ fun, kid-friendly spirit.

It wouldn’t be out of the question for the filmmakers to put a dark spin on this material. Alan Moore’s Miracleman found a definitive way to make the Billy Batson idea nightmarish and haunting. If Gayden and Sandberg truly wanted a film more in line with the Snyderverse entries, they could have made it. But Shazam! super speeds in the opposite direction while nodding at the other films in its franchise. Billy’s world is packed with Batman and Superman merchandise, but their adventures seem to take place far from the world where he lives. Gotham and Metropolis get superhero icons who rarely smile. Philly gets a goofball, and that turns out to be a lot more fun.

Sandberg draws on the horror skills he developed through films like Annabelle: Creation. Sivana’s allies include manifestations of the Seven Deadly Sins that wouldn’t look out of place in a much more graphic movie. And though Sandberg retains the shadowy imagery of previous DCEU films, he uses that dark palette to make Billy’s shiny red suit and glowing lightning-bolt chest insignia stand out even more. If Batman branding criminals in Batman V. Superman has a polar opposite moment, it’s Batson’s unnamed hero identity smiling and dancing to “Eye of the Tiger” at the top of the Philadelphia Museum of Art’s steps while shooting lightning from his hands, to the delight of the tourists around him. This is the rare superhero film that gets more whimsical as it goes along, up to and including the final fight, a battle royale that mostly unfolds at a Philadelphia Christmas carnival.

But whimsical isn’t the same as frivolous. Both Angel and Levi play Billy as a boy who’s never had the support he’s needed, and the film suggests there’s no easy fix for his traumas, even if he’s both dropped into a supportive environment, and suddenly able to leap tall buildings in a single bound. (Or in Billy’s case, almost leap a tall building in a single bound.)

That’s the subtext resting beneath Shazam!’s broad humor, fun spirit, and scary monsters. The film suggests that wish fulfillment will only get people so far, and power alone can’t change what’s damaged inside. Captain Marvel (or Shazam, or Thundercrack, or whatever you call him) might be one of the simplest superheroes ever created, but Shazam! both gets what makes that simplicity so appealing, and understands the complications stirred by the common wish to grow up too fast and assume powers you don’t know how to control.