Now how to move them sideways to fill empty columns? easy!
6 variables are needed;
xset - actor integer
set[] - array, size of total columns in game (vertical rows). for this example, we'll say there's 12 columns.
xmove[] - array, same size as set.
int i;
int j;
piece>draw actor>
- Code: Select all
xset=round((x-offset)/32);
/*this breaks the screen into columns, here our puzzle pieces are 32x32 pixels.
'offset' refers to the distance from 0 that the playfield starts at; (the farthest left location a piece can have. by subtracting the offset, we make sure that the farthest left column will be column 0. so if your playfield starts at x128, you'd say
xset=round((x-128)/32);*/
if(set[xset]==0){ //if the row is currently empty, the piece tells the program
set[xset]=1; //that the row is occupied. rowvalue can either be 0 or 1, empty or not.
}
then run a loop
- Code: Select all
for(i=0; i<11; i++){
/*we used <11 here, because row 11 is the last row (remember we started at 0, so 0-11 = 12 total. we don't want to calculate empty spaces beyond that.*/
xmove[i]=0; //reset this for move storage
for(j=i+1; j<=11; j++){
/*start at current row+1, and move right. We start at i+1, to calculate before would just be a waste, as columns don't care whats on the left side of them. This time we go all the way to 11, because though the last column doesn't move (so its not in the i loop) it can still be empty, and so it needs to be in the j loop.*/
if(set[j]==0 ){ //if empty column found to the right of current column
xmove[i]+=1; /*for each empty column found, add one to the move pile. remember that we're still in the i loop, and here i refers to the current column value. remember, we capped i at 10, so the last column (11) won't get any move value.*/
set[i]=0; /*we've found an empty space, we know that this row will soon be empty(after move).
since we're moving right, we know nothing will be using this space in the calculation loop, so its safe to set this column to
0. If any new pieces move in here after the move, they will set the column to one on their own. (via their draw actor).*/
} //end if set[j]==0
}//end j loop
}//end i loop
Then move the pieces!
trigger any event for the pieces (you can use a timer,
piece>timer>
- Code: Select all
x+=xmove[set]*32;
Now i reset xmove[] at the beginning of i loop, depending on the timing of your game, you can reset it with another loop at any time.
In the end, you just told the computer to do exactly what you would have done....count the empty spaces, and move pieces accordingly. All you need to know is how the computer wants to be told....but most of the logic computers use is based off of human logic, and often acts very similiar.