使用C++写一个俄罗斯方块
时间: 2023-08-31 11:11:21 浏览: 92
好的,下面是一个简单的C++俄罗斯方块游戏示例代码:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WIDTH = 12;
const int HEIGHT = 20;
int board[HEIGHT][WIDTH] = {0};
int shapes[7][4][4] = {
{
{0, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 3, 3, 0},
{3, 3, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{4, 4, 0, 0},
{0, 4, 4, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 5, 0, 0},
{5, 5, 5, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{6, 0, 0, 0},
{6, 6, 6, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 7, 0},
{7, 7, 7, 0},
{0, 0, 0, 0}
}
};
int curX, curY;
int curShape[4][4];
int curShapeID;
int curColor;
bool check(int x, int y, int shape[4][4])
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (shape[i][j] && (x + j < 0 || x + j >= WIDTH || y + i >= HEIGHT || board[y + i][x + j]))
{
return false;
}
}
}
return true;
}
void draw(int x, int y, int shape[4][4], int color)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (shape[i][j])
{
COORD pos = {x + j, y + i};
SetConsoleCursorPosition(hConsole, pos);
SetConsoleTextAttribute(hConsole, color);
cout << "█";
}
}
}
}
void erase(int x, int y, int shape[4][4])
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (shape[i][j])
{
COORD pos = {x + j, y + i};
SetConsoleCursorPosition(hConsole, pos);
cout << " ";
}
}
}
}
void clear()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {0, 0};
SetConsoleCursorPosition(hConsole, pos);
}
void init()
{
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
board[i][j] = 0;
}
}
srand(time(NULL));
curX = WIDTH / 2 - 2;
curY = 0;
curShapeID = rand() % 7;
memcpy(curShape, shapes[curShapeID], sizeof(curShape));
curColor = rand() % 7 + 1;
draw(curX, curY, curShape, curColor);
}
void update()
{
erase(curX, curY, curShape);
if (check(curX, curY + 1, curShape))
{
curY++;
}
else
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (curShape[i][j])
{
board[curY + i][curX + j] = curColor;
}
}
}
int lines = 0;
for (int i = HEIGHT - 1; i >= 0; --i)
{
int j;
for (j = 0; j < WIDTH; ++j)
{
if (!board[i][j])
{
break;
}
}
if (j == WIDTH)
{
lines++;
for (int k = i; k > 0; --k)
{
memcpy(board[k], board[k - 1], sizeof(board[k]));
}
memset(board[0], 0, sizeof(board[0]));
i++;
}
}
clear();
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
if (board[i][j])
{
draw(j, i, shapes[0], board[i][j]);
}
}
}
curX = WIDTH / 2 - 2;
curY = 0;
curShapeID = rand() % 7;
memcpy(curShape, shapes[curShapeID], sizeof(curShape));
curColor = rand() % 7 + 1;
if (!check(curX, curY, curShape))
{
clear();
cout << "Game Over!" << endl;
exit(0);
}
}
draw(curX, curY, curShape, curColor);
}
int main()
{
init();
while (true)
{
if (_kbhit())
{
int c = _getch();
if (c == 'a' || c == 'A')
{
if (check(curX - 1, curY, curShape))
{
erase(curX, curY, curShape);
curX--;
draw(curX, curY, curShape, curColor);
}
}
else if (c == 'd' || c == 'D')
{
if (check(curX + 1, curY, curShape))
{
erase(curX, curY, curShape);
curX++;
draw(curX, curY, curShape, curColor);
}
}
else if (c == 's' || c == 'S')
{
while (check(curX, curY + 1, curShape))
{
erase(curX, curY, curShape);
curY++;
draw(curX, curY, curShape, curColor);
}
}
else if (c == ' ')
{
int temp[4][4];
memcpy(temp, curShape, sizeof(temp));
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
curShape[j][3 - i] = temp[i][j];
}
}
if (!check(curX, curY, curShape))
{
memcpy(curShape, temp, sizeof(temp));
}
else
{
erase(curX, curY, temp);
draw(curX, curY, curShape, curColor);
}
}
}
update();
Sleep(50);
}
return 0;
}
```
这个游戏使用了Windows API函数来进行控制台屏幕输出和输入,所以只能在Windows系统下运行。
阅读全文