Tetris clone: How can I do this??

Started by
6 comments, last by adam23 17 years, 9 months ago
Hi guys! I'm making a simple tetris with SDL and i'm having a little problem. I'm blitting surfaces to animate the block but... is there any other way to do this? because when a block is stopped on the ground I have to put a new block on the screen. When I do this I've to blit background again and the block that is stopped on the ground disappears. Is there a way to MOVE an image that has been blitted? I'm a newbie.. I know it :P Thank u in advance for your help :)
Advertisement
You will want to blit the background first. The will go over top of the last frame and pretty much work like a clearing of the screen. Then you will blit (on top of the background) every visible block that is held within the game, which is blocks that are on the ground, partial blocks that have been removed due to making lines, and the currently falling block (and perhaps the next block, if you've allowed for that).

I know only that which I know, but I do not know what I know.
Do I have to save the block status when they are on the ground? I mean save them on a surface. Then I would do this on each frame:

Blit background
Blit blocks on the ground
Blit the block is moving

Am I right? If this is the way I have to do it.. how can I save the status of the blocks on the ground?

Thank u for your answer :)
You need to seperate the object data (eg position) to the object representation (eg the block graphic).

This way, you have a list of blocks all with different positions, and you just blit the same image to those positions on the screen surface after clearing the screen.

Steven Yau
[Blog] [Portfolio]

Thank you! I'm getting the idea. I've added some functions to get the final position of the block. With this I could use that position to blit the fallen blocks.

I have a last question. Suppose I have a pointer of type SDL_Surface to a block image. Is there a way to rotate that surface? if not I have to load images for every move of the block.

Thank you again!
Just like Yaustar said, you will need to create some kind of storage container for all of the blocks. Keeping in mind that you need to keep track of the segments of each block too, since a segment or multiple segments can be removed from a block by sucessfully creating a line.

You could create an array that is the size, in block segments, of the play area. Then store each segment in it's corresponding position in the array. This will allow for easy removal of segments and quick collision detection, since you would only need to check the adjacent blocks to the falling block. This also makes block rotation very easy if you think of the falling block as being in a matrix, and just transpose the matrix (turn it on its side).

For example:

// Measured in block segments#define PLAY_AREA_WIDTH 10#define PLAY_AREA_HEIGHT 20struct BLOCK_SEGMENT{     int X;     int Y;     int Type;     // Specifies the shape of the overall block};BLOCK_SEGMENT Play_Area[20][10];


... and to answer your orignal question, yes, but no. You can copy images around on the screen, however, for this kind of situation you will just want to clear the drawing area by blitting the background and then drawing all the game objects on top of it, since in the worst case scenario of many objects moving at once, it is far simpler and quicker to just redraw the entire scene than to try to move images.

Hope that helps to clear things up and gives you some ideas. :)

I know only that which I know, but I do not know what I know.
Thank you both! now I'm thinking on another way to put blocks on screen. I'm going to rewrite some parts of my program.

If i finish this.. I'll dedicate my tetris clone to both of you hahahah :D

See you on space cowboys!
I just finished my Tetris clone a couple of days ago.

What I did is have two pointers one to the current piece and one to the next piece. I also had a vector<rows> Which contains the information for each space in the puzzle including color, part number, and if the row is complete or not. So what I do is save the current pieces blocks in the vector and the set current piece equal to next piece. Then get a new random piece and save it to next piece. This works really well, because when a row is complete you just delete the row in the vector and add a blank one to the end. I have a few more menu things to add to mine but if you want to check it out you can find it by clicking the link below.

http://www.adamwlarson.com/downloads.html
Adamhttp://www.allgamedevelopment.com

This topic is closed to new replies.

Advertisement