There have been a few moments in his life, Tarell Alvin McCraney tells me, when he has felt like he’s hit the clock in a game of chess, and stopped the world turning.

The first of these moments occurred when he was six or seven years old and had been away for the weekend from his mother’s home in Liberty City, a low-rise housing project in north Miami. Home at the time was not only where his mother lived but also where her boyfriend, Blue, lived. McCraney was small for his age, and bullied at school for being different, for being silent, for not being into sports. He would be beaten, called “faggot” before he knew what that word meant. In the emotional absence of his own father, Blue was the first man in his life who really looked out for him, the first man he could look up to. Blue taught him to ride a bike, took him to the ocean, held him as he learned to swim, made him feel like he might have a place in the world after all. Blue was also a drug dealer, but in Liberty City in 1987 that wasn’t unusual.

When McCraney got home that weekend, though, he knew something was different. His mother, who had by then started on a downward path into crack cocaine addiction, was alone.

“Where’s Blue?” he recalls asking her, as if it were yesterday. “He’s gone,” his mother said.

“Gone?”

“Blue’s been shot and killed.”

McCraney had a dozen other questions about the how, where and why, but as he was asking them, he recalls, he was all the time thinking something else. He was thinking: “This is something you have to remember. This is a very strong lesson for you. The good things in your life are not always. If you go away for the weekend, if you don’t pay proper attention, you will come back and they won’t be here.” As he says this now, he slaps his hand down lightly on the table between us and halts the imaginary chess clock.

We are sitting at a schoolyard bench in the Miami sun a few blocks and three decades away from that childhood memory. The bench is in the grounds of Liberty City’s African Heritage arts centre, a prominent neighbourhood landmark, built with all the civil rights optimism of 1975. As a boy, McCraney came here every day in the summer holidays, and often after school to avoid the harassment and beatings he got if he walked his usual route home through Liberty Square. He took classes here in dance, visual art, music, acting and writing. “You couldn’t sign up to one thing, you had to sign up to everything,” he says, with his easy laugh. These days he comes back to teach on some of those courses himself (he is also, in his more visible public life, newly installed chair of the playwriting programme at Yale University). It was here that he started writing about the chaos of his life in order to begin to make some dramatic sense of it. And it was here, in that sense, that he began his journey to Moonlight, the extraordinary autobiographical film that a couple of days before we meet has been deservedly nominated for eight Oscars, including best picture.

McCraney wrote the script that became the basis for Moonlight at Yale, as part of a postgraduate theatre course in 2003. He had, by then, put some psychological and physical distance between himself and Liberty City. The gift he had shown in his theatre classes at the African Heritage centre had seen him taken under the wing of the black playwright August Wilson, (recipient of a posthumous academy award nomination this year for the adapted screenplay of his play Fences), to college at De Paul University, and then to France where he had come into the orbit of Peter Brook’s Bouffes du Nord theatre. In the summer of 2003, though, the news of the death of his mother – from complications of Aids that followed two years of dementia – pulled McCraney abruptly back into his old life.

He wrote the script not in anger, he says, or only in grief or guilt, though he felt both of those emotions. He wrote it in panic. “The panic came from there being a whole other part of me that wasn’t being accessed in the work I was doing,” he says. “At Yale I was around white people most of the time, people who came mostly from supportive families, people who didn’t grow up with drug addicts. All of the things that were still keeping me awake every night, these feelings that I was still this kid back in eighth grade, were in that other place, a big part of which had just died.”

He was thinking, too, about role models. People told him he had been lucky to have been taken up by August Wilson, to be cast by Peter Brook, and he knew that was true, but he knew also that his role models weren’t only in theatre. One of them, Blue, was a drug dealer, but a good man. “The question kept coming to me: ‘Why didn’t I become a drug dealer?’ It was in many ways the obvious choice, growing up here.”

The script he wrote mapped out some of that territory. He wrote it fast, 60-odd typed pages that confronted his mother’s decline, the brutality of the childhood bullying he experienced – he was hit with bricks, lost several teeth – as well as the moments of grace he achieved with Blue, and the transcendence of a solitary sexual encounter with his only true childhood friend. He titled it: “In Moonlight, Black Boys Look Blue”. The title captured some of the anguish and the escapes from anguish that the script explored.

Once he had finished “In Moonlight…”, he was unsure what to do with it. It felt more like a film than a play, but he had no idea how to get films made, so he put it in a bottom drawer and got on with other things. He wrote a celebrated trilogy of plays that transplanted Nigerian Yoruba myths to contemporary Louisiana; he wrote a play about New York drag acts and a play about the gospel choir of an elite black school; he moved to England as writer at the Royal Court and the RSC; he was awarded a MacArthur “genius” grant for the breadth and scope of his theatrical work. And all that time the script lay there, until – nearly 10 years later, through a series of coincidences – it seems to have insisted on finding the light of day.

McCraney had done some work with a small film-making collective in Miami called Borscht. It happened that Borscht also worked with a director, Barry Jenkins, who had a background that was uncannily similar to McCraney’s. With McCraney’s permission, Borscht sent the decade-old script to Jenkins, who had also grown up in Liberty City, a year above McCraney at elementary school, though the pair had never met.

Jenkins’s single mother was also an addict, but his escape route had not been into theatre but sports. He played American football at Florida State University, where he enrolled on the film programme. Jenkins read the script, saw much of his own life in it, and asked McCraney if he could write and shape it into a movie. McCraney liked the idea of handing some of the decisions over to someone who understood exactly how his world looked, giving him (and Jenkins) an autobiographical escape clause of sorts.

In the event, Moonlight couldn’t have been closer to either of their homes. It is a brutal kind of love story told in three very different acts, each one poignant to the point of tears. Having already seen it in preview in London, I had watched the film again the previous night in Miami in a cinema in an old warehouse a few blocks away from Liberty City, in an audience palpably thrilled at seeing their neighbourhood portrayed so lyrically on screen. In the first act, a version of McCraney’s former self, Chiron, is played by 10-year-old Alex Hibbert, whose eyes do all his talking for him. In the second act a different actor, Ashton Sanders, explores how things unravel in Chiron’s teenage years. And in the third act, substantially written by Jenkins, Chiron has remodelled himself into a gangster and drug dealer named Black in the style of McCraney’s first mentor, Blue. Physically, the three actors are very different, but Jenkins’s concentration on their interior lives means that you don’t for a moment feel the disjunction.

Chiron (Alex Hibbert), a version of the young McCraney, is taught to swim by Jean (Mahershala Ali) in Moonlight. Photograph: Allstar/A24

Though Black in the last act is bulked up, wearing gangster gold grills on his teeth, intimidating the corner boys who deal drugs for him, in private he is curled in a foetal position, summoning the courage to talk on the phone to his mother or to a boy he knew 10 years ago. This final act is obviously a departure from the reality of McCraney’s life, but watching it, he says, he felt it answered many of the questions posed in his decade-old script.

“The astonishing thing about watching Trevante [Rhodes], who plays Black,” he says, “is that every time he opens his mouth, then he just felt like me exactly.”

In person, McCraney is a tall, articulate, graceful man, who seems totally at home in his skin, but he suggests that, much of the time, that still feels like a part he is playing. “Trevante’s performance comes from a deep masculinity that he built up because he thought he needed to, just as my performance in real life is a pseudo smart guy who walks around and says pseudo smart things,” he insists. “On the inside, though, I’m in a constant state of terror and always exhausted because my mind is trying to figure out where am I? Who am I?”

The film confronts the uncomfortable fact of homophobia rooted in black American culture. It links that fact, it seems to me, brilliantly, to the wider risks of revealing vulnerability of any kind.

“It’s true,” McCraney says. “I don’t even necessarily say that [Chiron] is gay. I say he is in love with another man and he is figuring things out…”

That was how McCraney felt about his sexuality in his formative years. “As a boy, just like in the film, I had been called these names and given these attributes at a point when I certainly wasn’t having sexual feelings, male or female,” he says. “And then I started getting really confused because when I started having sexual attraction I was attracted to everybody. I found everybody appealing! I still do.”

He smiles. “But then, as I tried to engage in relationships, if I imagined myself married or in a long-term partnership, which I rarely do, I imagine myself with a man. But that has nothing to do with sex, in my mind, it is all to do with intimacy.”

These nuances are allowed and celebrated in Jenkins’s direction of Moonlight, though of course, not always in the cruder responses to the film.

“I put a promo of the film on my social media,” McCraney says. “I was scrolling through the comments people left. The first ones were nice. Then it was: ‘but they gay!’ And after that it was like: here we go again… We don’t as a community and a culture generally give ourselves enough latitude. Nature is diverse. Most bluebirds are blue but some are nearly black. They are still bluebirds.”

In the film, as in life, McCraney’s teenage self is cruelly betrayed by the first boy he loved, who sides with the bullies. In reality there was no third act to the story; did he ever try to create one, to get back in touch?

“No I think it would be too painful for me,” he says. “Chiron is intimate with [the friend] Kevin that one time in the movie, which is how it happened to me. What happened after had an effect on my attitude to friendship. Even now, every time I get close to a friend, I think: when are they going to turn round and spit in my face? The good thing is that now I have got to the place where I can say the anxiety out loud to them. And then people can go: Oh, is that what you are afraid of?”

McCraney ( on right) pictured with his younger brother Jason Alvin. Photograph: Courtesy of Tarell Alvin McCraney

If McCraney never resolved that relationship, he is grateful to the film for capturing the essence of his love for the murdered Blue. If there were an Oscar for the most beautiful movie scene of the year, Chiron being taught to swim in the ocean should win hands down. As it is, Mahershala Ali, who plays the Blue character, is rightly nominated for a best supporting actor role.

He so accurately matches McCraney’s memory that the two have started to merge in his head. “It’s funny,” he says. “Mahershala was fixing my bow tie backstage [at an awards event] in Toronto the other week, and suddenly I started just weeping in his arms. He must have thought I had lost my mind – which I probably had, momentarily. I was back as a six-year-old kid: I think he found a way to bring out something that I had not seen in so long.”

Had Blue been the first person to tell him he was special?

“The first person to show me that,” he says. “You can tell me a lot of things but I won’t believe you. But when people take actions, I believe that. I remember hugs from him. I think I have hugged my own father maybe three times in my life.”

McCraney’s mother went into rehab when he was 13, and he lived, uneasily, with his father through the worst years of being bullied at school, until he went off to college.

Has his old man seen the film?

“Yes.”

What was the conversation?

“There was no conversation. He said it was good. That was the extent of it.”

Fathers, and their absence, have loomed large in McCraney’s work for theatre. In 2010 he came to London and created a production of Hamlet for the RSC which he toured with around schools in the capital and beyond for half a year.

I ask when he first encountered Shakespeare.

“Well,” he says, “I think Shakespeare encountered me when Blue was killed. I wasn’t aware of it at the time, but I definitely felt like Hamlet. I was wandering around thinking: who did this? And having conversations with him all the time. But I didn’t see a Shakespeare play until I was 19 or 20. That was Peter Brook’s Hamlet, with Adrian Lester.”

He is grateful to his mother in that, even as things were falling so badly apart at home, she never stopped encouraging him to read.

“Even in my mum’s most drug-addled moments, she was kind of adamant about education,” he says. “She would say, ‘Apply yourself, go places I have not been’. That is always at the back of my head.” His middle name, Alvin, is his mother’s maiden name; he insists it is never left out.

There were other influences that made him a playwright, not a drug dealer. On his father’s side, his grandfather was a minister in the church. When McCraney was a young boy they read the whole Bible together. Later he would ask him “lots of theological questions, particularly when I was trying to figure out the whole gay thing”. His grandmother was a schoolteacher. “She is 90 now,” he says, “but when I go over there she will recite stretches of Walt Whitman. I remember once I was practising a speech of Mark Antony when I was in high school, and she was listening in the other room. When I forgot something, she shouted through the whole scene from memory…”

It’s tempting in this sense to view Moonlight as documenting a particular historical moment – the same moment documented in The Wire – when recession and crack cocaine arrived to blight and undermine the family culture and economic progress of urban black communities in the States. In Liberty City, though, that isn’t only a historical fact, it still very much informs the present moment. When Barry Jenkins has been asked how he recreated the look of the shantytown quality of the projects at that time, he suggests it wasn’t hard – it still looks exactly the same now.

“We are in the fallout from a lot of things here,” McCraney says. “The crack epidemic waned but heroin came in. The sub-prime loans disaster actually started here. We are just starting to come out of that – and now we have this new political situation…”

One of the reasons he loves the film is that it has put Liberty City on the map. “People say, you know, ‘I’ve been to Miami – that’s not Miami’. Some have literally said to me ‘but there aren’t that many black people in Miami’.” He laughs. “Florida has the third biggest black population in America by state, though of course you wouldn’t know it from pictures from Orlando.”

The day we meet is the day that Theresa May arrives in Washington, day seven of the executive order craziness. McCraney is shocked by the fact of the administration but not at all surprised. When people would tell him that it couldn’t happen, he would tell them to read the comments on his social media.

“For a long time there has been a systemic problem of oppression. It is directed to us, people of colour, and it is directed to women. You are OK in Miami but if you drive 10 miles north or west of here there are people who will let us know we are not welcome. Ten blocks away from here there are remnants of a race wall from the 1970s. It’s never been completely dismantled.

“You know, some people say to me ‘Moonlight is so great, it is going to change things’, and I think it is maybe helping a few people to find answers to some things. But the attitudes and problems it portrays are not historic ones, they are here and now.”

In this sense it seems fitting that Moonlight – made on a budget of $5m – will go head to head with the inspired escapism of La La Land when it comes to the Oscars. The opposition asks interesting questions about which film presents a true American romance and which deals in alternative facts. I wonder, before I go, if McCraney has been unnerved by the acclaim the film has received, the way it has made his own story so visible.

“No, it needed to be out there,” he says.

It has been good in another way too. He feels he can now write directly about some of these issues in a way he couldn’t find a way to before. “I didn’t know this, but when I actually saw Moonlight it was like the dam broke. Now I have all this energy that was behind it.”

He had never written about Miami in his work before, couldn’t portray his world directly. Now he says there are two or three projects that he wants to get going, based in the three or four blocks around where we sit.

“A community is only as strong as the stories it tells about itself,” he says. In this sense, for Liberty City, he believes Moonlight is just the beginning.

Moonlight is released on 17 February