I have been learning a little bit of C++ and decided to recreate a game ofTetris. The problem here is when I tried to display the tetris block within the game map itself. I'm not sure what was wrong and would appreciate if someone could help point out the error for me thanks.
void newPiece()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (Piece[NewType][Rotate][i][j] != 0)
std::cout << "#";
}std::cout << std::endl;
}
}
void start()
{
x = width / 2;
y = 1;
}
void draw()
{
for (int i = 0; i < width + 3; i++) //top wall
std::cout << " ";
std::cout << std::endl;
for (int i = 0; i < height; i++)
{
for (int j = 0;j < width + 2; j++)
{
if (j == 0)
std::cout << "\xDB"; // left wall within height
if (i == y && j == x)
for (int k = 0; k < 4; k++)
{
for (int l = 0; l < 4; l++)
{
if (i == k + y && j == l + x && Piece[NewType][Rotate][i][j] == 0)
std::cout << " ";
else if (i == k + y && j == l + x && Piece[NewType][Rotate][i][j] != 0)
std::cout << "#";
}
}
if (j == width + 1)
std::cout << "\xDB"; // right wall within height
else
std::cout << " ";
}std::cout << std::endl;
}
for (int i = 0; i < width + 3; i++) // bottom wall
std::cout << "\xDB";
std::cout << std::endl;
}
void input()
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
direction = ROTATE;
break;
case 'a':
direction = LEFT;
break;
case 's':
direction = SDOWN;
break;
case 'd':
direction = RIGHT;
break;
case ' ':
direction = HDOWN;
break;
case 'q':
Quit = true;
break;
}
}
}
void logic()
{
//key logic for eDirection
switch (direction)
{
case ROTATE:
if (Rotate != 3)
Rotate++;
else
Rotate = 0;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case SDOWN:
y += 2;
Score += 1;
break;
case HDOWN:
// calculate space between bottom and piece
// Place piece at bottom
// Score += 1*calculated
break;
}
}
You cannot print the elements like so. I suggest that you have an screen array that you fill with all the elements you want to see and then always print the whole array.