0
\$\begingroup\$

I am making a game where i have items or blocks stacked on top of each other.

What i would like to do is, when the first item(or the item at the bottom of the stack) is removed, i would like to shift all the blocks above, down into the next position below it and add another block to the top of the stack.

Sort of like in a match 3 game.

Could someone provide instructions for doing this? or some type of psuedocode?

Thanks guys

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Is something like this what you're asking for?

Block blocks[][];

void removeBlock(int x, int y)
{
  while (y < blocks[x].length - 1)
    blocks[x][y] = blocks[x][y+1];
  if (y < blocks[x].length)
    blocks[x][y] = generateNewBlock();
}

If you're asking about the animations for moving the blocks down, then you probably will want to keep both a logic position (the x,y coordinates above) and a visual position, that interpolates smoothly as the block falls.

You can use simple linear interpolation pos = pos1 * t + pos0 * (1-t) where t will go from 0 to 1, to move from pos0 to pos1. Preferably though is to have some kind of animation, for instance, you could use acceleration to speed up the blocks as they fall, then have them bounce once:

gravity = 10
velocity.y -= gravity * dt
position.y += velocity.y * dt
if (position.y < destination.y)
{
 if (!bounced)
 {
   velocity.y *= -0.1;
   bounced = true;
 }
 else
 {
   position.y = destination.y;
 }
}
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.