Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Tetris Clone Algorithim

This thread is locked; no one can reply to it. rss feed Print
Tetris Clone Algorithim
shangshu
Member #8,168
December 2006

Hi all,
I'm out to make a tetris clone and need some thoughts on the rotating of the blocks as they fall down the playing area. I would like the keys to work like this: KEY_DOWN would accelerate the rate of fall, KEY_RIGHT would move the block right, KEY_LEFT would move the block left, and KEY_UP would rotate the block clockwise in 90 degree increments. Nothing fancy.

I'm not familiar with the function rotate_sprite(bmp, spr, x, y, itofix(64)).
If I use the rotate_function above it will rotate the sprite 90 degrees to the right?

In the game the user needs to have the ability to rotate the sprites as many times as they want before it reaches the bottom. What is a good method of doing this?

switch(state){

state 1: rotate_sprite(bmp, spr, x, y, itofix(64));
break;

state 2: rotate_sprite(bmp, spr, x, y, itofix(128));
break;

state 3: rotate_sprite(bmp, spr, x, y, itofix(270));
break;

state 4: rotate_sprite(bmp, spr, x, y, itofix(0));
default:break;
}

Any ideas or suggestion are greatly appreciated

Audric
Member #907
January 2001

Seems good to me, for the displaying part. (type 'case 1', not 'state 1' of course)
It can be shortened to one line, but it would be less readble IMHO.

Hovever, you'll need something else to detect collisions:
- test if the piece can go left or right, before allowing the move
- test if the piece can go down
- test if rotation is possible without overlapping the playfield edges or the old pieces.

For collision testing, you can use for example an array of 4x4 booleans.
And the playfield would also have an array of booleans.

edit: I don't know if that was the question, but pressing left or right would set
state = (state % 4) + 1; // increment
and
state = ((state + 3) % 4) + 1; // decrement

raccoon
Member #7,478
July 2006

well, if you keep everything in an array, I think it would be useful not to draw the tetrominoes as sprites, but just draw the blocks as represented in the array

Simon Parzer
Member #3,330
March 2003
avatar

Yeah, make a 4x4 block array and rotate it by swapping columns with rows (first column is first row of the "transformed" matrix). If you do that upside-down you rotate clockwise.
Then just draw the array to the screen.

---- ---- -XX- ---- 
-X-- XXX- --X- ---X
-X-- X--- --X- -XXX
-XX- ---- ---- ----

Ricardo Santos
Member #6,609
November 2005
avatar

I'm also on a tetris clone and used Simon's approach but using 0 and 1 as it's easier to deal with. :P

You must think in blocks not in complete tetris pieces. Good luck!

kentl
Member #2,905
November 2002

I think Simon meant to use an array of integers as well. His ASCII drawing is just a visualization of how it will look when you draw the array to the screen. :)

Thomas Harte
Member #33
April 2000
avatar

shangshu said:

rotate_sprite(bmp, spr, x, y, itofix(270));

cough
rotate_sprite(bmp, spr, x, y, itofix(192));

Go to: