Arrays and animation

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Arrays and animation

Bobber
I'm working on a space invaders copycat and I'm struggling with 2 things. Creating and indexing a 2d array is the first. The second is getting my enemy sprites to move one pixel at a time. I tried with just one sprite and it would move fine left to right,  it would move down when it detected edge of screen too. But when moving down or from right to left I get trash pixels left behind and if I try to animate it with 2 they end up distorting the other.  

What I ultimately want to do is mimic the original space invaders where each enemy gets drawn one at a time instead of all of them moving one frame to the next. This is why the aliens got faster in the original and why I want to use a 2d array and iterate through it

So the question, how would I declare, initialize, and index a 2d array, and then I guess just some general guidelines on animating multiple sprites would be a huge help
Reply | Threaded
Open this post in threaded view
|

Re: Arrays and animation

ivant
You can simulate a 2D array using a 1D array. Let's say that you want to allocate a 2D array with dimensions W columns by H rows. You instead allocate a one dimensional array with W times H elements.

You also need to translate the 2D coordinates (i, j) to an index in the 1D array. To do so, you can use the formula i = j * W + i.

You can implement this as a class which will hide the implementation details.


There are many ways to implement the animation. The easiest one is to first delete the sprite from the old position, then draw it on the new one. This is clearly not very optimal and will result in a lot of flickering.

The most efficient way is to update all the pixels in a 16-bit word simultaneously and to update the same word more than once per frame and to only update words that need changing. For this I'd go with pre-computing all the horizontal movements for each different sprite and on each iteration to directly put the relevant values in the video memory.

The vertical movement should not require any additional pre-computed steps. You just have to make sure you don't override already drawn rows. For example, to move a sprite 1 pixel down, you need to erase the top row (put the background value in the memory location), then you just draw the sprite one row at a time.
Reply | Threaded
Open this post in threaded view
|

Re: Arrays and animation

ivant
For animations there's also the Bit Blit algorithm. You can watch this YouTube video by Computerphile for a good explanation on how it works.

It is a more general algorithm and I think it will be slower for this particular game. Still, it's interesting and historically important. Some graphics and game-oriented systems in the 1980s and 1990s implemented it in hardware. It could be a fun project to implement as an extension to the hack machine.
Reply | Threaded
Open this post in threaded view
|

Re: Arrays and animation

Bobber
Thanks for the advice. I did end up figuring out the arrays. Ended up just making a matrix class to handle the work for me and use a get(x,y) and set(x,y,value) function. Still wish I could initialize it the way I'm used to but oh well. Whole point of this project to me is only working with the tools you can make. I appreciate all the libraries other people have made for me so much more now.  I'll be sure to let you guys see it when I puzzle this out.

As far as animation, if I'm following you right, instead of clearing the screen and redrawing you are recommending, for example if I'm just moving one pixel, multiply the existing word by 2 and handle any carryover by adding it to the next word in memory?
Reply | Threaded
Open this post in threaded view
|

Re: Arrays and animation

Mukhirathinam
In reply to this post by Bobber
if you have the jack file can you upload please