俄罗斯方块c++代码
时间: 2023-11-05 21:06:05 浏览: 136
俄罗斯方块是一款经典的游戏,以下是一个简单的C++代码实现:
```
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WIDTH = 10;
const int HEIGHT = 20;
const int BLOCK_SIZE = 4;
int board[HEIGHT][WIDTH] = {0};
int block[BLOCK_SIZE][BLOCK_SIZE] = {0};
int blockType, blockRotation;
int blockX = WIDTH / 2 - 2, blockY = 0;
int score = 0;
void initBlock() {
int blocks[7][4][4][4] = {
{
{
{0, 0, 0, 0},
{1, 1, 1, 1},
{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, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0}
},
{
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
}
},
{
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 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, 2, 2, 0},
{0, 2, 2, 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, 0, 3, 0},
{0, 3, 3, 3},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 3, 0, 0},
{0, 3, 3, 0},
{0, 3, 0, 0}
},
{
{0, 0, 0, 0},
{0, 3, 3, 3},
{0, 3, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 3, 0},
{0, 3, 3, 0},
{0, 0, 3, 0}
}
},
{
{
{0, 0, 0, 0},
{0, 4, 4, 0},
{0, 0, 4, 4},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 4, 0},
{0, 4, 4, 0},
{0, 4, 0, 0}
},
{
{0, 0, 0, 0},
{0, 4, 4, 0},
{0, 0, 4, 4},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 4, 0},
{0, 4, 4, 0},
{0, 4, 0, 0}
}
},
{
{
{0, 0, 0, 0},
{0, 5, 5, 0},
{0, 5, 0, 0},
{0, 5, 0, 0}
},
{
{0, 0, 0, 0},
{0, 5, 5, 5},
{0, 0, 0, 5},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 5, 0},
{0, 0, 5, 0},
{0, 5, 5, 0}
},
{
{0, 0, 0, 0},
{0, 5, 0, 0},
{0, 5, 5, 5},
{0, 0, 0, 0}
}
},
{
{
{0, 0, 0, 0},
{0, 6, 6, 0},
{0, 0, 6, 6},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 6, 0},
{0, 6, 6, 0},
{0, 6, 0, 0}
},
{
{0, 0, 0, 0},
{0, 6, 6, 0},
{0, 0, 6, 6},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 6, 0},
{0, 6, 6, 0},
{0, 6, 0, 0}
}
},
{
{
{0, 0, 0, 0},
{0, 7, 0, 0},
{0, 7, 7, 7},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 7, 7, 0},
{0, 7, 0, 0},
{0, 7, 0, 0}
},
{
{0, 0, 0, 0},
{0, 7, 7, 7},
{0, 0, 0, 7},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 7, 0},
{0, 0, 7, 0},
{0, 7, 7, 0}
}
}
};
blockType = rand() % 7;
blockRotation = rand() % 4;
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
block[i][j] = blocks[blockType][blockRotation][i][j];
}
}
}
bool isCollision(int x, int y, int rotation) {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] != 0 && (y + i >= HEIGHT || x + j < 0 || x + j >= WIDTH || board[y + i][x + j] != 0)) {
return true;
}
}
}
return false;
}
void drawBoard() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
cout << " ";
} else {
cout << "*";
}
}
cout << endl;
}
cout << "Score: " << score << endl;
}
void mergeBlock() {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] != 0) {
board[blockY + i][blockX + j] = blockType + 1;
}
}
}
}
void clearLines() {
for (int i = HEIGHT - 1; i >= 0; i--) {
bool full = true;
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
full = false;
break;
}
}
if (full) {
for (int k = i; k > 0; k--) {
for (int j = 0; j < WIDTH; j++) {
board[k][j] = board[k - 1][j];
}
}
score += 10;
i++;
}
}
}
void gameLoop() {
while (true) {
initBlock();
if (isCollision(blockX, blockY, blockRotation)) {
break;
}
while (true) {
drawBoard();
Sleep(100);
if (_kbhit()) {
char ch = _getch();
if (ch == 'a' && !isCollision(blockX - 1, blockY, blockRotation)) {
blockX--;
} else if (ch == 'd' && !isCollision(blockX + 1, blockY, blockRotation)) {
blockX++;
} else if (ch == 'w') {
int nextRotation = (blockRotation + 1) % 4;
if (!isCollision(blockX, blockY, nextRotation)) {
blockRotation = nextRotation;
}
} else if (ch == 's') {
while (!isCollision(blockX, blockY + 1, blockRotation)) {
blockY++;
}
break;
} else if (ch == 'q') {
return;
}
}
if (isCollision(blockX, blockY + 1, blockRotation)) {
break;
}
blockY++;
}
mergeBlock();
clearLines();
}
drawBoard();
cout << "Game Over!" << endl;
}
int main() {
srand(time(NULL));
gameLoop();
return 0;
}
```
这个代码实现了俄罗斯方块的基本功能,包括方块的生成、移动、旋转、碰撞检测、合并、消除等。你可以根据自己的需要进行修改和扩展。
阅读全文