SANTA FE, Tex. — Just outside the ceramics storeroom where Trenton Beazley huddled on the floor, his classmates and substitute teacher from first-period art lay dead. A gunman stalked back and forth between two adjoining classrooms inside Santa Fe High School, firing blast after blast.

This was really happening. Again. This time, to them.

Mr. Beazley, 15, a catcher on the high school baseball team, had woken up on Friday morning excited about that evening’s quarterfinal game against Kingwood Park. When Mr. Beazley slipped into art class, the substitute, Ann Perkins, had been telling another student to go get a tardy pass. Everything about that muggy late-spring morning seemed so routine.

Then, at about 7:30 a.m., Mr. Beazley heard the sound that has become too routine in schools across America: Bang. Bang. Bang.

On Saturday, through their shock, grief and anger over a massacre that left 10 people dead and 13 wounded, this broken, stunned community of 13,000 struggled to find any reason behind it all. The authorities have not announced any motive but said that Dimitrios Pagourtzis, a 17-year-old student at Santa Fe High, had confessed to the rampage and told investigators he had spared students he liked so that “he could have his story told.”

Like millions of their peers, the students here grew up in the shadow of school shootings. They had done active-shooter drills since grade school. A school resource officer, who was critically wounded on Friday, patrolled their sprawling red brick school, which is an hour southeast of Houston.

In February, they had been spooked by a lockdown ordered after someone reported a pop-pop-pop sound outside. Devin Maier, 17, remembers not being able to go back to class that Monday and Tuesday. “I was just scared,” she said.

The school massacre that same month in Parkland, Fla., struck a response here, too, as a small group of students marched and waved signs that declared, “Never Again.”

But for many, the protests and preparations only hardened the shard of dread that occupied their thoughts — that one day, the television scenes of tears and police tape, memorial flowers and hands-up fleeing children, would arrive for them. Their turn.

“In the back of my mind, I knew it was going to happen,” said Madilyn Williams, 18.

Other students would later say they thought they heard a garbage can slamming or metal being hammered, but Mr. Beazley said he and his classmates knew almost immediately what was happening.

They bolted for the storeroom where the pottery kilns were kept while Ms. Perkins went to shut the classroom door. She was one of the two teachers killed.

The last thing Mr. Beazley saw as they tried to slam the door was the gunman, clad in a black trench coat, heading toward the closet, a strap of shotgun shells slung across his chest.

“I realized then, wow, he’s not playing around,” Mr. Beazley said.

The students tried desperately to barricade the closet door with a heavy pottery kiln. He smashed his fists against a heat vent to dislodge the kiln from the wall, but as he shoved it across the floor, the gunman jabbed his .38 pistol through a broken window of the closet door. Mr. Beazley heard a taunt that is now burned onto his memory: “Surprise,” the gunman said, followed by an expletive. Then:

Bang.

He killed a student who lay near the door, and as Mr. Beazley pushed and pushed on the kiln, he aimed at Mr. Beazley.

Bang.

“He saw me,” Mr. Beazley said. He was grazed in the side, and then struck again by the bullet’s ricochet. “I just dropped to the floor.”

While students puzzled over the gunman’s motives, the mother of Shana Fisher, a 16-year-old who was among those killed, said on Saturday that Mr. Pagourtzis had made advances toward her daughter for four months, which she consistently turned down.

“He had been getting more aggressive, more aggressive,” Ms. Fisher’s mother, Sadie Rodriguez, said. “Finally, she stood up to him, she stood up to him in class last week.”

Ms. Rodriguez said she did not discuss the events directly with her daughter. But she said that Ms. Fisher had told her sister and Ms. Rodriguez’s brother of her problems with Mr. Pagourtzis, and of the most recent confrontation in class.

Rome Shubert was in the first classroom to be attacked on Friday morning. He had been drawing 3-D shapes in art class when his teacher left to drop off something in another classroom. The gunman walked in through the open door “guns blazing,” Mr. Shubert said.