Kosie
Kosie

Reputation: 441

Space invaders base deterioration

In space invaders, when an alien bullet hits your defense bases, they deteriorate slowly. Can anybody explain how games do that?

I guess they are not using images for the bases and also not bounding rectangle collision detection?

Upvotes: 2

Views: 850

Answers (4)

Stiles Stillinski
Stiles Stillinski

Reputation: 1

I am not sure how the og space invaders guys have done it but, My extrapolation from the pygame knowledge that I have is, create a 2d array with 255 color value as base and 0s as transparent, make it as an pixel array object.

whenever you want to blit this array, you can convert it to surface using make_surface() function. create a mask out of this surface and use overlap method to detect which index in the 2d array the collision happens, with a radius r make the cells 0s inside the circle with point of collision as center. Now in the next iteration, use this modified 2d array to create surface and blit it.

Upvotes: 0

Griggy
Griggy

Reputation: 123

In the original space invaders, there were no more than 3 invader bombs on screen at any one time so simple rectangle collision can be okay.

The way I did it was to create the bases from individual blocks/pixels with an id. When a bomb hits a base, I get the id of the 1st pixel hit via a reversed loop which breaks at that point.

From that pixel, I counted a number of pixels up, down, left and right roughly in the shape of an explosion and removed those base pixels leaving a nice dent in the base.

The same method is used for the lazer cannon shooting up at the bases.

Unfortunately, I can't post the game code as it exceeds the max character posting limit. I have the game on Codepen but I'm not allowed to post a link without some code which is too large as stated above!

However, if you go to Codepen and do a search for space invaders it appears on the 1st page as 'Space Invaders Taito 1978'

Upvotes: 0

Blindman67
Blindman67

Reputation: 54128

Dialing in the wayback machine. The original space invaders used a simple bit mask to add damage to the bases. The images were just basic 1 bit images, from memory 1 byte wide and 8 bytes down (8 by 8 pixels)

To apply the damage there is a mask [0b11111001,0b11110000] that is shifted left (with carry placed in the lowest bit) then masked with the base bytes. pixelByte &= maskByte. Then cut out the pattern of damage. Bombs would continue hitting the base if any pixels were remaining and in the path of the bomb.

To detect a base hit a mask was also used. bombMask = 0b00000011 shifted left (or could have been looked up) to match x pos. To see if there is a hit you just AND the mask with the base bits pixelByte & bombMask if the result is not zero then the base has been hit.

It was all written in assembly, and for its time was a programing master piece. I still remember thinking, "How can they move so many pixels so quickly."

Today you can use the same method, but you seldom see that much effort put into detail like that. I find that modern games tend to take short cuts and just have a set lot of images to show damage.

So just take the damage count, based on a hit box, or circle, show image to illustrate damage state, healthy, minor damage, major damage, and destroyed. Easy to implement! good for the game? well that is another matter altogether.

Upvotes: 1

Matt
Matt

Reputation: 987

I'm not exactly sure how it was originally done, but one way to make it work would be to have each barrier made up of a number of separate blocks, each with their own health value. If a block takes enough hits, remove it.

This should allow for the barriers to deteriorate gradually.

Edit: To clarify, you would have each barrier made up of a number of separate images (I believe they were just a flat colour in the original), and each image having a rectangle that projectiles could collide with, damaging that section of the barrier.

Upvotes: 1

Related Questions