Error Help

I am writing a code for my c++ class that is a very basic form of tetris with only one block. As of now the program generates a block that falls on its own and can be moved with the arrow keys. I am trying to get the block to turn and am getting error that txr, tyr, tx, and ty are all already declared so it will not compile.

[Code]

// Positions of cells relative to each other
int txr[4] = {-1, 0, 1, 1};
int tyr[4] = {0, 0, 0 ,1};

// Position of block
int tx = 5;
int ty = 19;



// Initialization
void initializeScreen() {
initscr(); // Initialization.
cbreak(); // Don't wait for RETURN.
noecho(); // Don't echo key presses on screen.
curs_set(false); // Don't show the cursor.
nodelay(stdscr, true); // Don't wait for key pressed
keypad(stdscr, true); // Special codes for KEY_LEFT, KEY_UP, etc..
}


// Print Block
void showTetris(bool showOrDelete) {
if (showOrDelete == true) printf("\x1b[7m");
if (ty == 1) printf("\x1b[31m");
printf("\x1b[%d;%dH ", 20 - ty - tyr[0], 5 + tx + txr[0]);
printf("\x1b[%d;%dH ", 20 - ty - tyr[1], 5 + tx + txr[1]);
printf("\x1b[%d;%dH ", 20 - ty - tyr[2], 5 + tx + txr[2]);
printf("\x1b[%d;%dH ", 20 - ty - tyr[3], 5 + tx + txr[3]);
printf("\x1b[0m");
}

// Rotate Block
void rotateTetris(int key) {
int txr[4] = {txr[0]*0 + (-1)*tyr[0], txr[1]*0 + (-1)*tyr[1], txr[2]*0 + (-1)*tyr[2],
txr[3]*0 + (-1)*tyr[3]};
int tyr[4] = {txr[0]*1 + 0*tyr[0], txr[1]*1 + 0*tyr[1], txr[2]*1 + 0*tyr[2],
txr[3]*1 + 0*tyr[3]};
}

// Move Block using keys
void moveTetris(int key, bool fall) {
if (fall && ty >1) ty--;
switch (key) {
case KEY_UP: rotateTetris(key);
case KEY_DOWN:
if (!fall && ty > 1) ty--;
break;
case KEY_LEFT: if (tx > 2) tx--;
break;
case KEY_RIGHT: if (tx < 9) tx++;
break;
}
}


// Main Function.

int main(int argc, char** argv) {
initializeScreen();
showTetris(true);
refresh;
int count = 0;
int key;
bool fall;
while (true) {
key = getch();
fall = (++count % 50 == 0);
showTetris(false);
moveTetris(key, fall);
showTetris(true);
refresh();
usleep (10 * 1000);
if (ty == 1) {
ty = 19;
tx = 5;
}
if (key == 'q') {break; }
}
}

[/code]
Last edited on
Could you use [code][/code] tags around your code along with proper indentation?

The error is self explanatory. You have declared some globals, but then inside of your functions you try to declare them again. Just assign to the existing instances instead.

Alternatively, remove the globals and pass the data between functions using parameters.
Could you explain that a little further? I am very new to programming so I am still confused
Topic archived. No new replies allowed.