Residents of Tucson might not appreciate the choice of their fair city as the place “where dreams go to die,” but even they will probably find plenty of pleasure in Hamlet 2. Directed by Andrew Fleming (Dick) from a screenplay he wrote with Pam Brady (South Park: Bigger Longer & Uncut), Hamlet 2 mines the rich comedy in failure. The film employs an act structure with titles such as “Act 4—Hope is a demon bitch,” and a theatrically inflated voiceover to narrate the story of Dana Marschz (Steve Coogan), a failed actor with an impossible to pronounce surname, turned high school drama teacher in Tucson, Arizona, who is fighting to save his department and his pride.

His solution, suggested to him by his nemesis, the acerbic ninth-grade drama critic, is to write an original play. Typing half-naked while crying to Puccini beneath a tragedy mask, Marschz creates a musical called Hamlet 2, in which a time machine cheers up Shakespeare’s denouement—an idea so bad, the baby-faced critic agrees, it just might work.

Marschz has a tough row to hoe, with a group of unruly students, a hostile principal, a dismal track record of school productions (Erin Brockovich—the play!), and a paucity of personal talent. But like many inspired comic characters (Steve Carell’s Michael Scott in “The Office” comes to mind), Marschz’s cluelessness is his best defense, and Coogan plays cluelessness with the best of them.

Long famous in England for the series “I’m Alan Partridge,” and for the film 24 Hour Party People, Coogan recently appeared as Larry David’s psychiatrist on “Curb Your Enthusiasm.” He brings a fearless lack of vanity to his roles, by turns deadpan, over-the-top and just plain goofy. While his portrayal here is occasionally too broad, mostly he nails it. Coogan is also a master of physical comedy, roller-skating through Tucson (actually Albuquerque, where the movie was shot) as a means of transportation, as often down as up, muttering, “Gravel is the bane of my existence.” Coogan’s Marschz is preposterously self-inflated, mildly delusional and uncannily appealing.

Until late in the movie, when fate, to use one of Marschz’s favorite phrases, makes him feel like he’s “been raped in the face,” Marschz soldiers on, breaking up a class fight (in both senses of the phrase) by shouting, “Nachos or pizza?” The movie acknowledges the tensions (sexual and otherwise) between the white-bread drama nerds (Skylar Astin and Phoebe Strole of Broadway’s Spring Awakening) and the streetwise Latino students who are only taking the class as a last resort. As Strole’s character, Epiphany, says, “I try, but I still get anxious around ethnics!” In a satisfying twist, the most intimidating youth (who tells Marschz he’s “Haywood Jablowme,” but is actually Octavio) turns out to be the Ivy League-bound son of a prosperous, highbrow Latino family. Octavio (Joseph Julian Soria) also turns out to be a passionate Hamlet.

Catherine Keener as Marschz’s dissatisfied wife, Brie, steals her few scenes, especially when she drunkenly castigates both her husband and their boarder, Gary (David Arquette), while sipping from a margarita glass the size of her head. Keener’s almost scary intensity contrasts nicely with Elisabeth Shue’s gently comic portrayal of herself as a Hollywood actress turned Tucson nurse. Speaking to Marschz’s students, she confides that what she most misses about acting is kissing all her cute co-stars. “In nursing you don’t get to make out with your patients,” she says with a sigh.

Amy Poehler adds her characteristic sparkle as Cricket Feldstein, a testy ACLU lawyer who shows up late in the movie to make sure the show goes on. Considering that the musical numbers include “Rock Me Sexy Jesus” and that the principal and a growing number of parents hate Marschz’s guts, Cricket helps save the day.

Fleming and Brady have fun spoofing Inspirational Teacher movies like Mr. Holland’s Opus, but in its own way, Hamlet 2 shares the genre’s optimism. Would it be giving anything away to say that theatre really does bring people together? While not all the jokes in Hamlet 2 work, even the failures earn a smile.