EAST LANSING, Mich. — Maryland football had its chances.

The Terps found themselves inside Michigan State’s 25-yard line three times in the second half. And three times, they came away with no points.

That gave No. 8 Michigan State ample opportunity to pull away from Maryland, which the Spartans eventually did for a 40-21 win. It was a frustrating loss for the Terps, their second straight to a ranked opponent.

It didn’t have to be.

“It’s a sick feeling in your stomach knowing that you probably should have won the game,” tight end Chig Okonkwo said.

Maryland (5-5, 2-5 Big Ten) came out firing in the second half, marching 61 yards downfield in just under two minutes. The Terps were in prime position to cut their deficit to one score.

But quarterback Taulia Tagovailoa threw behind Marcus Fleming on an end zone route, and his pass was picked off by Noah Harvey at the goal line.

[Maryland football has struggled against star players. Kenneth Walker may be the best yet.]

That started the series of late-drive failures, the next coming after Maryland forced a turnover and set up deep in Michigan State territory. Again attempting to close it to one score, Tagovailoa brought the Terps inside the Spartans’ five-yard line.

On third down, he was flushed out and threw the ball away. But his pass did not get back to the line of scrimmage, and Tagovailoa was flagged for an intentional grounding, setting his side back 19 yards. Then, Joseph Petrino missed a 40-yard field goal, and the score remained the same.

The next possession was just as fruitless. Aided by an unsportsmanlike conduct penalty, Maryland set up at Michigan State’s 24-yard line needing to convert a critical fourth down. But Tagovailoa’s pass to Carlos Carriere was broken up by a swarm of green jerseys, and the Spartans held their lead at two scores yet again.

“We made plays from the 20 to the 20, but it’s going to be really important, if we want to win against quality, ranked opponents like Michigan State, that when we get it down in there, we’ve got to come away with touchdowns,” coach Mike Locksley said.

This time, they capitalized, adding an insurance touchdown from Kenneth Walker III. The Heisman contender turned in a below-average day by his lofty standards — being held under 100 rushing yards until the fourth quarter — but Michigan State did not need him to be exceptional to win.

Instead, the Spartans turned to a surprise aerial assault to hit the Terps early.

Michigan State (9-1, 6-1 Big Ten) drew Maryland in on a flea flicker, and Montorie Foster burned Lavonte Gater for his first career touchdown, an easy 52-yard reception. Two drives later, Jayden Reed sprinted past Tarheeb Still and hauled in a 29-yard score.

A pair of long drives — one 14 plays, the other 13 — also ended in touchdowns, the latter a one-yard push by Walker. But the Spartans slowed in the second half, and the Terps’ inability to capitalize cost them.

“We felt like we could win the game if we forced them to pass the ball,” defensive lineman Sam Okuayinonu said. “We’ve got to make big plays.”

[Inconsistent defense cost Maryland football in loss to Penn State]

There were ominous signs of inconsistency from the start. Maryland, ever lethal through the air, struggled to break down Michigan State’s typically paltry pass defense.

Tagovailoa misfired on a handful of throws, while his receivers continued to drop passes that hit them in the hands. Until the final series of the first half, Tagovailoa had a measly 50 passing yards on eight completions, and the Terps’ only score came on a four-yard touchdown rumble by Peny Boone.

Maryland picked it up at the end of the half, as Tagovailoa found Okonkwo for a 32-yard touchdown strike. The pair connected for another 32-yard score early in the third quarter.

But between those marks came the frustrating turnovers, the penalties, the drops. The Terps played well enough at times, particularly in the second half, to pressure a lagging Spartans side into an uncomfortable position.

Yet, a potential upset never came to be thanks to those missed chances.

“We really feel like we beat ourselves at the end there,” Okonkwo said.