#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h> // for _kbhit() and _getch()
using namespace std;
const int width = 12;
const int height = 20;
char board[height][width];
vector<vector<int>> shapes = {
{{0, 1, 0}, {1, 1, 1}}, // I
{{1, 1}, {1, 1}}, // O
{{1, 1, 0}, {0, 1, 1}}, // S
{{0, 1, 1}, {1, 1, 0}}, // Z
{{1, 1, 1}, {0, 1, 0}}, // T
{{1, 1, 1}, {0, 0, 1}}, // L
{{1, 1, 1}, {1, 0, 0}} // J
};
int currentShapeIndex;
vector<int> currentShape;
int currentX, currentY;
void initBoard() {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (x == 0 || x == width - 1 || y == height - 1)
board[y][x] = '#';
else
board[y][x] = ' ';
}
}
}
void printBoard() {
system("cls"); // Use "clear" on Linux/Mac
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
cout << board[y][x];
}
cout << endl;
}
}
void newShape() {
currentShapeIndex = rand() % shapes.size();
currentShape = shapes[currentShapeIndex];
currentX = width / 2 - currentShape[0].size() / 2;
currentY = 0;
}
bool canMove(int dx, int dy) {
for (int y = 0; y < currentShape.size(); ++y) {
for (int x = 0; x < currentShape[y].size(); ++x) {
if (currentShape[y][x]) {
int newX = currentX + x + dx;
int newY = currentY + y + dy;
if (board[newY][newX] != ' ')
return false;
}
}
}
return true;
}
void placeShape() {
for (int y = 0; y < currentShape.size(); ++y) {
for (int x = 0; x < currentShape[y].size(); ++x) {
if (currentShape[y][x]) {
board[currentY + y][currentX + x] = '*';
}
}
}
}
void clearLines() {
for (int y = 0; y < height - 1; ++y) {
bool fullLine = true;
for (int x = 1; x < width - 1; ++x) {
if (board[y][x] == ' ') {
fullLine = false;
break;
}
}
if (fullLine) {
for (int yy = y; yy > 0; --yy) {
for (int xx = 1; xx < width - 1; ++xx) {
board[yy][xx] = board[yy - 1][xx];
}
}
y--;
}
}
}
void rotateShape() {
vector<int> rotatedShape(currentShape[0].size(), 0);
for (int y = 0; y < currentShape.size(); ++y) {
for (int x = 0; x < currentShape[y].size(); ++x) {
rotatedShape[x] |= (currentShape[y][x] << (currentShape.size() - y - 1));
}
}
if (canMove(0, 0)) {
currentShape = rotatedShape;
}
}
void moveShape(int dx, int dy) {
if (canMove(dx, dy)) {
for (int y = 0; y < currentShape.size(); ++y) {
for (int x = 0; x < currentShape[y].size(); ++x) {
if (currentShape[y][x]) {
board[currentY + y][currentX + x] = ' ';
}
}
}
currentX += dx;
currentY += dy;
placeShape();
} else if (dy == 1) {
placeShape();
clearLines();
newShape();
if (!canMove(0, 0)) {
cout << "Game Over!" << endl;
exit(0);
}
}
}
void gameLoop() {
srand(time(0));
initBoard();
newShape();
while (true) {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'a': moveShape(-1, 0); break;
case 'd': moveShape(1, 0); break;
case 's': moveShape(0, 1); break;
case 'w': rotateShape(); break;
case 'q': exit(0);
}
}
if (canMove(0, 1)) {
moveShape(0, 1);
} else {
placeShape();
clearLines();
newShape();
if (!canMove(0, 0)) {
cout << "Game Over!" << endl;
exit(0);
}
}
printBoard();
this_thread::sleep_for(chrono::milliseconds(300));
}
}
int main() {
gameLoop();
return 0;
}
初始化游戏板 (initBoard
):
board
,表示游戏区域。边缘用 #
表示墙壁和底部。打印游戏板 (printBoard
):
生成新方块 (newShape
):
移动检测 (canMove
):
放置方块 (placeShape
):
清除满行 (clearLines
):
旋转方块 (rotateShape
):
移动方块 (moveShape
):
主游戏循环 (gameLoop
):
主函数 (main
):
这个代码实现了一个简单的俄罗斯方块游戏,使用了基本的控制台输入输出和图形显示。
上一篇:c++中::是什么
下一篇:c++ 优先队列
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站