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..
}
// 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; }
}
}
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.