Space invaders base degradation?

Hi, I’m working on a clone of the classic space invaders from 1978. I’m quite new to games and I have been trying to work out how I’m going to get the bases to be destroyed gradually as they get hit be invaders bullets. In the arcade it seems the damage is randomly distributed as the bullets travel into the bases, depending on the type of missile? I don’t know how I’m going to randomly knock out pixels at the point of impact. Can anyone offer any help or suggestions. Thanks. Katie.

Since the game is relatively simple in terms of graphics requirements you could just create a bunch of empty sprites (which defaults to 2x2 white texture, or use your own 1x1 or NxN image) used as the pixels that make up the shield full image.

https://dl.dropboxusercontent.com/u/168438/1gam/invasion/002/index.html - example prototype testing out Cocos2D-Js. Not sure it’s current state as it was a few years back since I tested it out.

There are other options:

  • render texture drawing black into a black and white texture and use as a mask for the shield image.
  • keep a data buffer of pixel data and send to the gpu when hit
  • use a drawnode and clear each frame redrawing the shield
  • use a custom drawnode that you add method to remove specific subset of verts
  • use a set of frames with multiple sets that go from full to destroyed and randomly select the frame for a given damage level
  • custom shader with shields state in uniforms
  • etc

wow, thanks for the tips. Could you tell me more about the render texture idea? is this essentially drawing a line into the base with a specific type of brush?

Well the only major issue with using a RenderTexture (at least the way you’d use it in cocos2d-x) is that I don’t think you can easily “erase” pixels unless you use it as a mask or setup a custom blend or shader.

Essentially you can render any node (Sprite, etc) into a RenderTexture instead of to the screen buffer. You can also clear it. Because space invaders has, again, minimum requirements for CPU/GPU you could actually clear every frame and just render all the still visible (undamaged) pieces of the shield.

You can read its inception: http://discuss.cocos2d-x.org/t/planning-some-features-for-v3-3/16663/12 .
Commonly used for screenshot: Screen Capture -> scale -> save file .
And notes about rendering same sprite multiple times: Using RenderTexture to render one sprite multiple times .

See:

// RenderTexture (RT)
auto rt1 = RenderTexture::create(200, 200);
// create some node(s)
auto sprite = Sprite::create("Test/1.png");
sprite->setPosition(100, 100);
auto sprite2 = Sprite::create("Test/2.png");
sprite2->setPosition(100, 100);
// draw sprite(s) in the RT
rt1->beginWithClear(1, 0, 0, 1);
sprite->visit();
sprite2->visit();
rt1->end();

Hope that helps.

1 Like

Thanks Steve, I will have a go at building a shield later today. I’m not sure now about render texture, though I think the shields need to be made up of different sprites that I can change the texture on when I get a hit. Do you think this will work? Also say 4 bases with 40 sprites per base will this kill the physics engine?

It shouldn’t kill the physics engine, mostly because there’s little else that’s onscreen and you should be able to add them all together such that they’ll auto-batch.

The easiest thing to do is test it out. While it’s some amount of work you’ll learn things and it shouldn’t take you too long. Feel free to view source on the demo I linked above for one naive method for doing this.

There are usually optimizations you can do if you find issues. For example, you might put all the shield pixel sprites into a batch layer in order to not have to rely on the auto-batching to work well, etc.

Hi Steve, I gave this a try using sprites in each shield (80 per shield) and it’s remarkably quick. I still have some work to do but here is a link if you want to take a look. Please note though the sound on the video is really bad so you might want to turn it down if you watch it! (https://www.facebook.com/mobileappbuilderz/videos/388524888154608/)

Katie

Nice, yeah looks like the shields are working as you wanted. There’s always something that can be optimized and if you run into performance issues do some profiling to figure out where the bottleneck is and then address that issue.

Unless you’re running on WebGL in a mobile browser your game will probably run fine with up to 1000 sprites on the screen. And if a bunch of sprites have the same texture (and a few other things like vert/frag shader + uniforms, etc) then they can be rendered extremely fast.

In your case it’s good to remember that space invaders ran well back in the days of very limited hardware so you can assume that even if you wrote a renderer entirely in software (manipulating a large array of bits that represent every pixel on the screen and then rendering those bits onto the screen once per frame) you’d probably find that it runs just as fast.

Anyway, good luck on polishing it up to 100% complete :smiley:

1 Like