c++俄罗斯方块代码

cpp
#include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> #include <Windows.h> using namespace std; const int WIDTH = 10; const int HEIGHT = 20; int field[WIDTH][HEIGHT] = {0}; class Tetris { private: int x, y; // Current position of the shape int shape, rotation; // Shape and rotation of the current piece // Define the shapes of the pieces int shapes[7][4][4] = { // I { {0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0} }, // J { {1,0,0,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, // L { {0,0,1,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, // O { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, // S { {0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, // T { {0,1,0,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, // Z { {1,1,0,0}, {0,1,1,0}, {0,0,0,0}, {0,0,0,0} } }; public: Tetris() { // Initialize the game x = WIDTH / 2 - 2; y = 0; shape = rand() % 7; rotation = 0; } // Draw the shape on the field void draw() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (shapes[shape][rotation][i*4 + j] == 1) { field[x + j][y + i] = 1; } } } } // Check if the piece can move down bool canMoveDown() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (shapes[shape][rotation][i*4 + j] == 1) { if (field[x + j][y + i + 1] == 1 || y + i + 1 >= HEIGHT) return false; } } } return true; } // Move the piece down void moveDown() { if (canMoveDown()) { ++y; } else { draw(); clearLines(); x = WIDTH / 2 - 2; y = 0; shape = rand() % 7; rotation = 0; } } // Check if the piece can move left bool canMoveLeft() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (shapes[shape][rotation][i*4 + j] == 1) { if (field[x + j - 1][y + i] == 1 || x + j - 1 < 0) return false; } } } return true; } // Move the piece left void moveLeft() { if (canMoveLeft()) { --x; } } // Check if the piece can move right bool canMoveRight() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (shapes[shape][rotation][i*4 + j] == 1) { if (field[x + j + 1][y + i] == 1 || x + j + 1 >= WIDTH) return false; } } } return true; } // Move the piece right void moveRight() { if (canMoveRight()) { ++x; } } // Rotate the piece clockwise void rotate() { int nextRotation = (rotation + 1) % 4; if (checkCollision(x, y, shape, nextRotation)) rotation = nextRotation; } // Check if a rotation results in a collision bool checkCollision(int nx, int ny, int nshape, int nrotation) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (shapes[nshape][nrotation][i*4 + j] == 1) { if (field[nx + j][ny + i] == 1 || nx + j < 0 || nx + j >= WIDTH || ny + i >= HEIGHT) return false; } } } return true; } // Clear completed lines void clearLines() { for (int i = HEIGHT - 1; i >= 0; --i) { bool isComplete = true; for (int j = 0; j < WIDTH; ++j) { if (field[j][i] == 0) { isComplete = false; break; } } if (isComplete) { // Shift lines down for (int k = i; k > 0; --k) { for (int j = 0; j < WIDTH; ++j) { field[j][k] = field[j][k - 1]; } } // Clear the top line for (int j = 0; j < WIDTH; ++j) { field[j][0] = 0; } ++i; // Recheck the current line } } } // Draw the field void drawField() { system("cls"); for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j
cpp
) { if (field[j][i] == 0) { cout << "."; } else { cout << "#"; } } cout << endl; } } // Run the game void run() { while (true) { drawField(); // Handle user input if (_kbhit()) { char key = _getch(); switch (key) { case 'a': moveLeft(); break; case 'd': moveRight(); break; case 's': moveDown(); break; case 'w': rotate(); break; case 'q': exit(0); break; } } moveDown(); Sleep(500); // Adjust the speed of falling pieces } } }; int main() { srand(time(NULL)); Tetris game; game.run(); return 0; }

这个示例演示了一个基本的俄罗斯方块游戏,使用C++编写。玩家可以通过控制台输入来控制方块的移动和旋转。游戏通过drawField()函数在控制台上绘制游戏区域。玩家可以按下 'a', 'd', 's', 'w' 键分别控制左移、右移、下移和旋转,按下 'q' 键退出游戏。