Police and government officials in charge of black affairs here have finally won a three-month war of attrition against homeless black people who have defied South Africa's racial laws by erecting primitive plastic shelters for their families on sandy wasteland outside this beautiful coastal city.

The campaign to remove what is officially regarded as an illegal "squatter camp" has included repeated tearing down of the people's shelters and confiscating of their clothes, food, cooking utensils and even children's medicines.

It ended with a military-style assault in the early hours Tuesday when police threw a barbed-wire cordon around the camp and, after giving the people five minutes to disperse, bombarded it with tear-gas canisters.

Reporters who were present said there was pandemonium as the trapped people tried to scatter, some stumbling into the barbed wire as they were blinded by the tear gas. Several were injured, and some children were taken for treatment to a hospital here, the reporters said.

Later, 76 of the squatters who do not have official permits to be in this area were arrested. They are being held, together with 34 children, in official buildings, jails and police stations.

Two hundred more who do have official permits are being accommodated in two large beer halls in nearby black townships. Pieter G. Koornhof, the minister in charge of black affairs, has announced a special concession--allowing them to build houses on a new site, called Driftsands, 25 miles outside the city. The squatter camp where the people have been trying to establish rudimentary homes is 10 miles from Cape Town, where most of them work.

Noel Robb, a member of the Black Sash rights organization helping the squatters, said today that two children were reported lost during the assault. There was another child whose parents could not be traced, Robb said.

The official campaign against the black families has its roots in a 20-year-old government policy to try to limit the number of blacks allowed to live and work in the western half of Cape Province.

In one precedent case, at the nearby squatter camp called Crossroads in 1977, the government relented to international outcry and let the 10,000 blacks living there remain. But the government froze the building of houses for blacks. Nevertheless, economic growth in the area continued to atttract black workers despite the controls, and there is now a chronic housing shortage.

Timo Bezuidenhout, the official in charge of black affairs in the region, estimates 80,000 blacks are without homes here. The government now recognizes the need to build more housing but is emphatic in refusing to allow blacks without homes to build shelters in the meantime.

It argues that squatter camps cannot be allowed to develop. Bezuidenhout has appealed repeatedly to the homeless to be patient and lodge with other families until the government builds more houses.

However, the pressures of overcrowding keep forcing groups of people to the sandy Cape Flats. They put up rudimentary shelters and then clashed with the police who came to dismantle them. The latest clash was one of the most prolonged, beginning in February when about 600 families put up plastic shelters on the site of what used to be called the Kakaza Trading Center. Police repeatedly tore down the shelters at KTC Camp, as it came to be known, only to see the determined squatters put them up again.

Eventually the squatters dismantled the shelters themselves before dawn each day, putting them up again after dark. The police then raided at night, confiscating the families' goods.

The campaign reached a climax last week as the cold winter rains began. For four wet days and nights the police set up spotlights at the camp and, operating from the shelter of their trucks, refused to let the squatters cover themselves. "The people dug holes in the sand," Robb said today. "If they wrapped blankets around themselves the police left them, but as soon as anyone pulled anything over his head the police removed it. I saw one policeman stop a woman who tried to cover her baby's head with a petticoat."

Monday, Koornhof announced that KTC squatters with permits to be in the area could live in the two beer halls until the Driftsands site was ready. He also warned that those without permits would be prosecuted. That night, 200 whites held a meeting in Cape Town to protest the treatment of the squatters. Some went to the KTC Camp to join the squatters in singing and dancing.

It was then that the police put up the barbed-wire cordon, warned the people to disperse because they were constituting "an illegal gathering," and finally launched their tear-gas bombardment.