🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

stuck in tetris game

Started by
7 comments, last by da_cobra 22 years, 6 months ago
i'm good stuck in this game I already have a random block that I can move in my playfield now when I hit the floor (or another block) it should be placed on my playfield at that place My playfield is a 20x10 array Playfield [20] [10] and my blocks are also put in an array like this blocks = 6 ; // 7 blocks rotation = 3 ; // 4 rotations a block block [Blocks] [rotation] [4] [4] those [4] [4] are my blocks, like this by example 0000 0100 0110 0010 0100 0110 0111 0100 0110 0000 0100 0000 a 1 is for a red block, 2 purple, ..... now when I place that block in my playfield (array) I go to my function BlockInArray() and now it places that block on 3 positions in my array?!? everytime 10 blocks between and my function is only runned once (I used a timer to check) here's my function : void BlockInArray() { x = ((Pos_X/16)-19) ; // calculate x/y y = ((Pos_Y/16)-9) ; // position in playfield for (i=0; i<4; i++) { for (j=0; j<4; j++) { if (blocks [block] [rotation] [j]) // check if there is a cube in that array { // if so position it beginning at x & y PlayField [x+i] [y+j] = blocks [block] [rotation] [j] ; } ; // end of if } ; // end of for j } ; // end of for i Pos_X = PlayfieldX+48 ; // position new block in the Pos_Y = PlayfieldY ; // middle top of playfield RandomPiece() ; } // end of BlockInArray() I'm really desperade, I've been breaking my head over this problem now for 2 weeks :/ Big thanx in advance if any one can help me (if you want me to send the complete code to you then send an email to s_noppe@hotmail.com or contact me on MSN or icq 3347831) </i> Edited by - da_cobra on December 1, 2001 1:43:25 PM
Advertisement
My C is a little rusty...


if (blocks [block] [rotation] [j]) // check if there is a cube in that array


added the reference to ''i'' above...sense blocks is a 4D array you would need it to refrence the correct memory cell....also why do you have all the x=, y= math above? You can just as easily clamp Pos_X and Pos_Y within limits of the playfield array..then when updateing the game screen multiply them by the tile size to blit them correctly

an example:

Pos_x is clamped within 0 and 17 (playfield horizontal array - 4)
Pos_y is clamped within 0 and 11 (playfield vertical array - 4)

so Pos_X and Pos_y refer to the upper left corner of a given block... if the tiles are 20 pixels wide then you just need to find the starting location (draw_X = Pos_x * 20 etc..) and place a tile there...this seperates the screen drawing from the actual gameplay mechanics and can help when it comes to debugging and it will help a lot when you start codeing the tetris line clearing routines...

just some advice
void BlockInArray() {
x = ((Pos_X/16)-19); // calculate x/y
y = ((Pos_Y/16)-9); // position in playfield
for (i=0; i<4; i++)
for (j=0; j<4; j++)
if (blocks[block][rotation][j] != 0)
PlayField [x+i][y+j] = blocks [block][rotation][j];<br><br>Pos_X = PlayfieldX+48; // position new block in the <br>Pos_Y = PlayfieldY; // middle top of playfield<br>RandomPiece();<br>} // end of BlockInArray() <br><br>I cant actually see anything wrong with this section of code. (especially if all of your other code works.) I rewrote it so<br>it was a bit more readable. <img src="wink.gif" width=15 height=15 align=middle> I don''t think the != operater in the if statement is necessary, but I threw it in just in case.<br>Also, does your New block show up at the top of the screen like its supposed to. (I notice you simply referenced Pos_X and Pos_Y, but their values would still be the same outside the function as they were when the function was called.)<br><br><br> </i>
for (i=0; i<4; i++)
for (j=0; j<4; j++)
if (blocks[block][rotation][j] != 0)

actually it should be:

for (i=0; i<4; i++)
for (j=0; j<4; j++)
if (blocks[block][rotation] ----> <---- [j] != 0)

if "blocks" is a 4 dimentional array (added the ''arrows'' to show the change)...

Also as dark_solar noted...are Pos_X, Pos_Y, blocks and playfield arrays global variables?...nothing really wrong if they are (should be for that function to work) altho computer science classes look down on them...for a game as basic as Tetris, heavy use of pointers could be overkill
?????
what do you mean by

if (blocks[block][rotation] ----> <---- [j] != 0)

I tried this line => if (blocks[block][rotation] [j] != 0)

and as I thought this didn''t work

can I pls send my complete code to one of ya, because I really don''t see my fault
Hello,

Not really an answer to your question, but it''s useful if you are ever going to post something with code again. You could use the [ source ] tags (without the spaces of course) which will make your code a little bit more structured. It will also avoid the problem with the smileys that can occur and the italic text. See the example:

int numers[10];
for (int i=0; i<10; i++) {
numbers = i;
}
AClass:Method();

  int numbers[10];for (int i=0; i<10; i++) {  numbers[i] = i;}AClass::PMethod();  


Much better that way, don''t you think . btw, you can read all this in the forum FAQ. There''s a link at the top of the page.
Sorry folks....I now see what is happening...the web board is translateing the code as if I intended italics to be on...

the line should have read:

if (blocks[block][rotation] ----> {i} <---- [j] != 0)

take out the ''arrows'' and replace the ''{,}'' around the ''i'' with ''[,]''

also are the variables ''block'' and ''rotation'' global?
Yeah sure, send me your source.
My address is nightsolar@hotmail.com
Ill see if I can figure out whats wrong, and try to help.
I know how frustrating it is to try and fix a bug you can''t find.
Happened to me quite a few times in fact.
it works !!!!!!!!!!!!!!!!!
finally!!!!!

so I don''t have to send it to ya anymore Dark_Solar
but still thanx for asking
and also thanx to every1 else that (tried to) helped me

This topic is closed to new replies.

Advertisement