Dramatic footage has revealed the moment a man attempted to murder two armed police officers with a shotgun, a court has heard.
Daniel Hannam, 32, had allegedly unlawfully obtained the firearm and a large number of cartridges.
He took it to the streets of a small Somerset town, Castle Cary, before going to his girlfriend's house.
Armed police officers surrounded the property at 2.45am on January 23, following 999 calls from members of the public.
Body-worn video footage, viewed by jurors on Tuesday, shows two officers, known only as IC and IX, making their way towards the front door.
A police helicopter can be heard overhead as IC and IX shine lights on the end-of-terrace house on Park Street.
One says "Window, movement" before they shout "Armed police, armed police, put your hands up to the window now".
Shots are then heard, with video showing one of the officers shooting back towards the window and shouting "shots fired".
Hannam is on trial at Bristol Crown Court, where he denies two charges of attempting to murder a police officer, relating to IC and IX.
The prosecution case was opened on Tuesday by David Scutt, with jurors visiting Castle Cary on Wednesday.
Mr Scutt told them: "Realising he was surrounded and desperate, he deliberately fired at armed police officers intending to kill them as they tried to gain entry to that house.
"They were at the front door of the house and the defendant went to the front bedroom.
"He opened that window and he fired down onto the officers outside."
The jury was played bodycam footage of IX and IC twice approaching the front door.
In the footage, the officers shout "armed police" and "put that down now" before "shots fired" and "get back, get back".
One of the officers shouts: "Yes I fired".
Mr Scutt described how the police "scrambled for cover" behind an armed response vehicle parked by the house.
The side of this vehicle was allegedly hit with pellets from Hannam's shotgun.
In a phone call to police during the incident, he is alleged to have said: "I'm down for a shoot out, I'm not going to lie.
"I've got unlimited cartridges. Someone is going to die tonight. One of you lot is going to die."
The jury heard Hannam had gone to see his on-off girlfriend at her home in Park Street on the evening of January 22.
He then took his shotgun and cartridges into town to meet a "geezer" he had previously argued with.
Hannam later told police he fired the gun into the air while waiting for the man, then returned to Park Street.
The defendant left the house to buy cocaine but became "very cross" when he could not get hold of his dealer, Mr Scutt said.
He went to McColls newsagents for alcohol and cigarettes, shooting twice at the door, before walking to a car park at 1.44am.
At 1.47am he approached a BMW driven by Oliver Walter-Taylor, who had driven there to meet a female friend.
Hannam is accused of attempting to kidnap Mr Walter-Taylor by getting into his BMW and pointing the shotgun at him.
After a struggle, both men got out and Hannam allegedly fired two shots in the air, with pellets hitting the car.
In a 999 call to police moments later, Mr Walter-Taylor said: "Someone has just pointed a shotgun at me.
"He has just fired another shot. I've just legged it."
Mr Scutt told the jury that Hannam's actions could be described as a "rampage", as by that point he had "loaded and fired" three times.
Police attended Park Street at 2.10am and a decision was made to "contain" the property, with armed officers surrounding it by 2.45am.
Hannam later told police that his girlfriend had woken him up to tell him that police were outside.
"He got out of bed and he went downstairs when he was aware of the police to get the shotgun," Mr Scutt said.
"You have heard one or two shots coming from above those police officers.
"He fired more shots out of the back window of the house where there were officers.
"He carried on firing until he ran out of cartridges. After that, he surrendered to the police."
At 4.25am, Hannam threw the shotgun out of the back window of the house and was arrested.
Neither IX or IC were hit by the shot or shots fired out the front window.
Hannam denies two counts of attempting to murder a police officer, possessing a firearm with intent to endanger life and attempting to kidnap Mr Walter-Taylor.
He has admitted possessing a firearm with intent to cause fear of violence, making use of a firearm with intent to resist arrest and attempting to burgle the McColls shop.
The trial continues.