Talking about Lumines game, Actionscript 2, Flash and Game development.
In the first part we managed to move and rotate a 2×2 square made (obviously) by 4 bricks.
Now we’ll see how to make them fall when we press SPACE
key.
Notice that you move and rotate blocks by tapping arrow keys, but you make them fall by pressing SPACE
.
This because timing is essential, and we want to make blocks fall as the player touches the spacebar.
The code does not have anything difficult, I just have to check if there are empty spaces under falling bricks.
No “game over” check at the moment, it will be covered later.
// declaring some setup variables
// number of horizontal cells
grid_width = 16;
// number of vertical cells
grid_height = 10;
// size of the cell
tile_size = 30;
// offset in pixels fron the left side of the stage
x_offset = 10;
// offset in pixels from the top side of the stage
y_offset = 10;
// number of different colors that can be displayed in a brick
different_colors = 3;
// boolean values saying if I should wait for the left (or right, up...) key to be released
// this is used to make the player move bricks tapping arrow keys instead of just pressing them
wait_left = false;
wait_right = false;
wait_up = false;
wait_down = false;
// flag indicating if blocks are falling
falling = false;
// array containing the game field data
field = new Array();
// array containing the bricks I can move
moveable_bricks = new Array();
// initializing and drawing the play field
for (x=0; x variable
if (Key.isDown(Key.LEFT)) {
wait_left = true;
} else {
if (wait_left) {
// if the left key has been tapped, move the four bricks on the left
for (x=0; x<4; x++) {
// this "if" is used to determine if the bricks are still inside the game field
if ((_root["brick_"+moveable_bricks[x]]._x-x_offset)/tile_size-_root["brick_"+moveable_bricks[x]].pos%2>0) {
_root["brick_"+moveable_bricks[x]]._x -= tile_size;
}
}
// reset variable, now I must wait again for a key to be pressed
wait_left = false;
}
}
// same routine for the right key
if (Key.isDown(Key.RIGHT)) {
wait_right = true;
} else {
if (wait_right) {
for (x=0; x<4; x++) {
if ((_root["brick_"+moveable_bricks[x]]._x-x_offset)/tile_size-_root["brick_"+moveable_bricks[x]].pos%2<14) {
_root["brick_"+moveable_bricks[x]]._x += tile_size;
}
}
wait_right = false;
}
}
// when the DOWN arrow is pressed, I must rotate the bricks clockwise
// block 0: moves to the right and becomes block 1
// block 1: moves down and becomes block 3
// block 2: moves up and becomes block 0
// block 3: moves to the left and becomes block 2
if (Key.isDown(Key.DOWN)) {
wait_down = true;
} else {
if (wait_down) {
for (x=0; x<4; x++) {
switch (_root["brick_"+moveable_bricks[x]].pos) {
case 0 :
_root["brick_"+moveable_bricks[x]].pos = 1;
_root["brick_"+moveable_bricks[x]]._x += tile_size;
break;
case 1 :
_root["brick_"+moveable_bricks[x]].pos = 3;
_root["brick_"+moveable_bricks[x]]._y += tile_size;
break;
case 2 :
_root["brick_"+moveable_bricks[x]].pos = 0;
_root["brick_"+moveable_bricks[x]]._y -= tile_size;
break;
case 3 :
_root["brick_"+moveable_bricks[x]].pos = 2;
_root["brick_"+moveable_bricks[x]]._x -= tile_size;
break;
}
}
wait_down = false;
}
}
// when the UP arrow is pressed, I must rotate the bricks counter-clockwise
// block 0: moves down the right and becomes block 2
// block 1: moves to the left and becomes block 0
// block 2: moves to the right and becomes block 3
// block 3: moves up and becomes block 1
if (Key.isDown(Key.UP)) {
wait_up = true;
} else {
if (wait_up) {
for (x=0; x<4; x++) {
switch (_root["brick_"+moveable_bricks[x]].pos) {
case 0 :
_root["brick_"+moveable_bricks[x]].pos = 2;
_root["brick_"+moveable_bricks[x]]._y += tile_size;
break;
case 1 :
_root["brick_"+moveable_bricks[x]].pos = 0;
_root["brick_"+moveable_bricks[x]]._x -= tile_size;
break;
case 2 :
_root["brick_"+moveable_bricks[x]].pos = 3;
_root["brick_"+moveable_bricks[x]]._x += tile_size;
break;
case 3 :
_root["brick_"+moveable_bricks[x]].pos = 1;
_root["brick_"+moveable_bricks[x]]._y -= tile_size;
break;
}
}
wait_up = false;
}
}
// when SPACE key is pressed, I don't wait for its release but I detect the key at once
if (Key.isDown(Key.SPACE)) {
// letìs made bricks fall...
falling = true;
}
} else {
//if the blocks are falling
// blocks_landed variable will count the number of bricks that already touched the ground
// or that cannot fall anymore because they are over other bricks
blocks_landed = 0;
for (x=0; x<4; x++) {
// calculating x and y position of the brick in the array
x_pos = (_root["brick_"+moveable_bricks[x]]._x-x_offset)/tile_size;
y_pos = (_root["brick_"+moveable_bricks[x]]._y-y_offset)/tile_size-1;
// determining if the block can fall. There are two conditions:
// 1: its position is less than zero. It means the block is outside the grid, in its starting position
// 2: its position is lower than the grid height and the tile under the block is empty
if ((y_pos<0) or ((y_pos
moveable_bricks[x] = brk.getDepth();
}
}
And this is the result… arrow keys to move/rotate and SPACE
to drop
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.