Able Seaman Ryan Donovan, who murdered an officer on a submarine, had been fascinated with violent video games

A sailor fascinated with violent video games and gangsta rap is beginning a life sentence for shooting dead an officer on board a nuclear-powered submarine.

A court heard on Monday that Able Seaman Ryan Donovan was angry over losing the chance of deployment on a surface ship after disobeying an order to help scrub HMS Astute.

He volunteered to take first sentry duty while Astute was docked at Southampton on a goodwill visit in April and, armed with an SA80 assault rifle, shot dead the weapons engineering officer, Lieutenant Commander Ian Molyneux.

Donovan, 23, shot and badly injured a second officer, Lieutenant Commander Christopher Hodge, and his "murderous onslaught" was stopped only when two civilian dignitaries leapt on him in the submarine's control room.

Detectives later discovered that Donovan had spoken of carrying out a "massacre" and discussed taking part in the sort of killing sprees depicted in video games.

It emerged at Winchester crown court that Donovan had been seen drunk in the early hours before the attack, breaking rules that forbid sailors from drinking for 10 hours before handling a firearm. The court also heard that concerns had been raised about Donovan's attitude, but doctors had judged him fit to carry on serving.

He admitted murdering Molyneux and attempting to murder Hodge and two other men who escaped unhurt, Petty Officer Christopher Brown and Chief Petty Officer David McCoy. He was told he will spend at least 25 years in prison.

The court heard that four days before his rampage Donovan had retired to his bunk when he should have been helping scrub part of the submarine. His disobedience led his planned deployment on a surface ship being cancelled.

Astute docked in Southampton on 6 April this year. On shore leave Donovan visited bars, restaurants, clubs and strip joints, drinking heavily. He got back to his hotel at 3.30am on 8 April – the day of the killing – and staggered out of his taxi. Eight hours later, just before going back on board, he told a colleague: "I'm going to kill somebody. I'm not fucking kidding. Watch the news."

Donovan asked if he could take the first sentry duty. He seemed "cheery" as he ate fish and chips in the mess and before he left he squirted vinegar into his mouth, possibly to hide the smell of alcohol, the court was told. At noon, less than nine hours after Donovan had arrived back at his hotel drunk, Brown carried out a "sniff and eyeball" check to judge if he was fit. He passed – though the prosecution said he would have failed a drink-drive test – and was issued with the SA80 rifle.

As he walked off towards the submarine's control room along a narrow corridor, Brown called Donovan to tell him he had not signed for the weapon.

Donovan swivelled and, with the rifle at hip level, fired four times. Brown dived for cover and Chief Petty Officer McCoy, who was standing nearby, ran down stairs and hid in a storage cupboard with eight others. The court was told it was a "miracle" the men were not hit.

Molyneux came from the control room, where he was entertaining dignitaries, lowered his head and tried to tackle Donovan. When Donovan opened fire Molyneux was just a few centimetres from the muzzle. He was shot in the side of the head and died instantly.

Donovan stepped over Molyneux and carried on to the control room, where his sixth shot hit Hodge, leaving him with serious kidney and liver injuries.

The leader of Southampton city council, Royston Smith, and the chief executive, Alistair Neill, seized Donovan. A seventh shot was fired but Donovan was overpowered. The spree lasted 13 seconds. Neill later said he had thought Donovan was a terrorist. His eyes looked "wild but faraway". It was as if he was "in a dream" or "doing this in a video game".

When they investigated his background, police found that Donovan was interested in violent games such as Grand Theft Auto, particularly the "kill frenzy" feature. He wondered out loud about the best way to get the biggest "kill count" on board Astute and discussed carrying out a "massacre" in the control room. He boasted that he would be famous.

Police found out that Donovan had an alter ego, a rap star called Reggie Moondog. Among the lyrics he wrote was: "Tell the quartermaster I caused disaster. SA80 – more palaver."

Defending, Christopher Parker QC said Donovan had no mental illness, disorder or disability but was "radically disordered" at the time of the spree.

Parker said Donovan joined the navy at 18 but never felt comfortable as a submariner. The prospect of joining a surface ship had been a relief and it was a "calamity" when his own disobedience wrecked that. He felt unable to see "a way out" and intended to kill himself that day.

Sentencing Donovan, Mr Justice Field told him that in murdering Molyneux he had "robbed him of a bright future with a loving family and of a most promising career".

Outside the court, Molyneux's wife, Gillian, said: "Nothing can ever replace Ian – my husband and soulmate and the father of our four beautiful children. To Jamie, Arron, Bethany and Charlie – your daddy and I love you very much and our future will always be guided by him."

A navy spokesman said it had no legal right to breath test service personnel but a new law to allow the armed services to do so had been mooted even before the attack.