Archived

This topic is now archived and is closed to further replies.

hammerstein_02

Prolly stupid but...

Recommended Posts

hammerstein_02    134
Hey, I posted in here the other day, because I am writing a Tetris clone and needed some information on how to go about programming it. Thank-you for the replies btw, they were much appreciated and got me on my way nicely. Now as far as I can tell, I have chosen a slightly different method of going about this, and so far my code is prolly a few times larger than what it should be at this stage (like 800 lines). i have got the blocks falling, and moving.. but I don''t know how to check for collisions, both efficiently and correctly. You see my problems are these: I store the shape in a 4*4 UCHAR array.. and each position in the array represents a block.. I have a board array and am going to move the shape independantly, but each each time check for collisions with the bottom or sides etc.. Now this.. If I have say the line shape, and it is on the right hand side of my array (due to rotation) and the user wants to move it to the left hand side of the screen. I defined the line shape as.. 0 0 0 1 1 1 1 1 0 0 0 1 Rotates 90 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 But if it is on the right hand side, how do I move it to the left? Any suggestions, or do I need to rethink my design. I looked at the other code and stuff, and from what I read it looks like it is done in a different way to my method. Please correct me if I am wrong, and sorry if I am being stupid.. thanks for your time and your replies (this is one of my longest ever postings) Cheers Hammer =*= If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore =*=

Share this post


Link to post
Share on other sites
Tybal    122
I''m kind of doing something similar at the moment actually, the way I''m planning to do it is to keep the piece always on the bottom-left of the 4*4 array. Then you''d have to keep the piece''s bottom left position in relation to the game play grid. So if you have the grid full of random blocks, to check collision with pieces below, you only have to check the bottom of your grid with the game grid. So say you have the line piece

0 0 0 0
0 0 0 0
0 0 0 0
1 1 1 1

and are trying to check to see if it will collide with the grid, you check the 4 squares right below that line up with the 4 squares of your piece. For checking with the sides, you can just use the bottom-left point to check with the left side, because you always keep your piece in the bottom left of your array. To check with the right side, just check to see if there are any rows where row + bottom-left >= right bound (or however) with pieces in them.

Hope this helps

Tybal
ehlertjd@uwec.edu

Share this post


Link to post
Share on other sites
hammerstein_02    134
Sorry for the crappy diagram...

I rotate the shape within the array, meaning I move it, so need to decide where it is.. I presume you hold each of the rotated shapes in individual arrays, and when they rotate move to the next one?

Anyway, I think I have a solution in that I will move the shape through itself if they move it, if they rotate it will rotate from its last known good position (if this makes sense)

Add more detail later, if required.. Thanks..

Share this post


Link to post
Share on other sites
Tybal    122
Actually if you write a function to remove dead space from the bottom and from the left, you can 'shift' your piece down and to the left.

Some valueable functions to have for this:

bool isColumnEmpty(int index);
bool isRowEmpty(int index);
void shiftLeft();
void shiftDown();

they're pretty easy to write, so then you just have to say:

while( isRowEmpty(3) )
{
shiftDown();
}
while( isColumnEmpty(0) )
{
shiftLeft();
}

do this every time you rotate, and it'll always be in the bottome left.


void Block::shiftLeft()
{
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 3; x++)
{
squares[y][x] = squares[y][x+1];
}
squares[y][3] = 0;
}
}

void Block::isColumnEmpty(int index)
{
return(squares[0][index] == 0 && squares[1][index] == 0 && squares[2][index] == 0 && squares[3][index] == 0);
}


or something like that.


[edited by - Tybal on October 22, 2002 8:57:54 AM]

Share this post


Link to post
Share on other sites