C++#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
const int WIDTH = 10;
const int HEIGHT = 20;
// 方块形状,每个方块有 4 种旋转状态
const int SHAPES[7][4][4][4] = {
// I 型
{
{
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
},
{
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
}
},
// O 型
{
{
{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}
},
// J 型
{
{
{1, 1, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
}
},
// L 型
{
{
{1, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 0, 0},
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
},
{
{1, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{0, 0, 0, 0}
}
},
// S 型
{
{
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
}
},
// Z 型
{
{
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
}
},
// T 型
{
{
{1, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{1, 0, 0, 0},
{1, 1, 0, 0},
{1, 0, 0, 0},
{0, 0, 0, 0}
}
}
};
class Tetris {
private:
vector<vector<int> > board;
int currentShape;
int currentRotation;
int currentX;
int currentY;
// 检查方块是否可以移动到指定位置
bool canMove(int newX, int newY, int newRotation) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int newBlockX = newX + j;
int newBlockY = newY + i;
if (SHAPES[currentShape][newRotation][i][j]) {
if (newBlockX < 0 || newBlockX >= WIDTH || newBlockY >= HEIGHT) {
return false;
}
if (newBlockY >= 0 && board[newBlockY][newBlockX]) {
return false;
}
}
}
}
return true;
}
// 将方块固定到棋盘上
void fixBlock() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int blockX = currentX + j;
int blockY = currentY + i;
if (SHAPES[currentShape][currentRotation][i][j]) {
board[blockY][blockX] = 1;
}
}
}
}
// 检查并消除满行
void checkLines() {
for (int y = HEIGHT - 1; y >= 0; --y) {
bool isFull = true;
for (int x = 0; x < WIDTH; ++x) {
if (!board[y][x]) {
isFull = false;
break;
}
}
if (isFull) {
for (int yy = y; yy > 0; --yy) {
for (int x = 0; x < WIDTH; ++x) {
board[yy][x] = board[yy - 1][x];
}
}
}
}
}
public:
Tetris() {
board.resize(HEIGHT, vector<int>(WIDTH, 0));
newBlock();
}
// 生成新方块
void newBlock() {
currentShape = rand() % 7;
currentRotation = 0;
currentX = WIDTH / 2 - 2;
currentY = 0;
if (!canMove(currentX, currentY, currentRotation)) {
// 游戏结束
cout << "Game Over!" << endl;
exit(0);
}
}
// 方块下落
void moveDown() {
if (canMove(currentX, currentY + 1, currentRotation)) {
++currentY;
} else {
fixBlock();
checkLines();
newBlock();
}
}
// 方块左移
void moveLeft() {
if (canMove(currentX - 1, currentY, currentRotation)) {
--currentX;
}
}
// 方块右移
void moveRight() {
if (canMove(currentX + 1, currentY, currentRotation)) {
++currentX;
}
}
// 方块旋转
void rotate() {
int newRotation = (currentRotation + 1) % 4;
if (canMove(currentX, currentY, newRotation)) {
currentRotation = newRotation;
}
}
// 绘制棋盘
void draw(HANDLE hOut) {
COORD coord = {0, 0};
SetConsoleCursorPosition(hOut, coord);
// 绘制顶部边框
cout << "+";
for (int i = 0; i < WIDTH; ++i) {
cout << "--";
}
cout << "+" << endl;
for (int y = 0; y < HEIGHT; ++y) {
cout << "|";
for (int x = 0; x < WIDTH; ++x) {
bool isPartOfShape = false;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int blockX = currentX + j;
int blockY = currentY + i;
if (SHAPES[currentShape][currentRotation][i][j] && blockX == x && blockY == y) {
isPartOfShape = true;
break;
}
}
if (isPartOfShape) break;
}
if (isPartOfShape || board[y][x]) {
cout << "[]";
} else {
cout << " ";
}
}
cout << "|" << endl;
}
// 绘制底部边框
cout << "+";
for (int i = 0; i < WIDTH; ++i) {
cout << "--";
}
cout << "+" << endl;
// 显示按键功能说明
cout << "控制说明:" << endl;
cout << "A: 左移" << endl;
cout << "D: 右移" << endl;
cout << "W: 旋转" << endl;
cout << "S: 下落" << endl;
}
};
int main() {
srand(time(0));
Tetris game;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hOut, &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(hOut, &cursorInfo);
while (true) {
game.draw(hOut);
if (kbhit()) {
char key = getch();
switch (key) {
case 'a':
game.moveLeft();
break;
case 'd':
game.moveRight();
break;
case 'w':
game.rotate();
break;
case 's':
game.moveDown();
break;
}
}
Sleep(300);
game.moveDown();
}
return 0;
}