使用c++编写一个俄罗斯方块游戏
时间: 2023-12-05 17:51:17 浏览: 29
好的,下面是一个简单的俄罗斯方块游戏的C++代码实现,供您参考:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
const int boardWidth = 12; // 游戏板宽度
const int boardHeight = 22; // 游戏板高度
int score = 0; // 分数
int level = 1; // 等级
int board[boardHeight][boardWidth] = {0}; // 游戏板
// 方块类
class Block {
public:
int x, y; // 方块的位置
int type, dir; // 方块的类型和方向
int color; // 方块颜色
// 初始化方块
void init() {
type = rand() % 7;
dir = rand() % 4;
x = boardWidth / 2 - 2;
y = 0;
color = rand() % 7 + 1;
}
// 判断方块是否可以移动
bool canMove(int dx, int dy, int newDir) {
int nx = x + dx;
int ny = y + dy;
int nd = (newDir == -1 ? dir : newDir);
for (int i = 0; i < 4; i++) {
int tx = nx + blocks[type][nd][i][0];
int ty = ny + blocks[type][nd][i][1];
if (tx < 0 || tx >= boardWidth || ty < 0 || ty >= boardHeight || board[ty][tx] != 0) {
return false;
}
}
return true;
}
// 移动方块
void move(int dx, int dy, int newDir) {
if (canMove(dx, dy, newDir)) {
x += dx;
y += dy;
if (newDir != -1) {
dir = newDir;
}
}
}
// 将方块放到游戏板中
void place() {
for (int i = 0; i < 4; i++) {
int tx = x + blocks[type][dir][i][0];
int ty = y + blocks[type][dir][i][1];
board[ty][tx] = color;
}
}
// 绘制方块
void draw() {
for (int i = 0; i < 4; i++) {
int tx = x + blocks[type][dir][i][0];
int ty = y + blocks[type][dir][i][1];
gotoxy(tx * 2 + 2, ty + 2);
cout << "■";
}
}
// 消除方块
int clear() {
int lines = 0;
for (int i = boardHeight - 1; i >= 0; i--) {
bool full = true;
for (int j = 0; j < boardWidth; j++) {
if (board[i][j] == 0) {
full = false;
break;
}
}
if (full) {
lines++;
for (int k = i - 1; k >= 0; k--) {
for (int j = 0; j < boardWidth; j++) {
board[k + 1][j] = board[k][j];
}
}
for (int j = 0; j < boardWidth; j++) {
board[0][j] = 0;
}
i++;
}
}
if (lines > 0) {
score += lines * level;
if (score >= level * 100) {
level++;
}
}
return lines;
}
private:
// 方块的形状
const int blocks[7][4][4][2] = {
{
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }
},
{
{ { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 } },
{ { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 } },
{ { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 } },
{ { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 } }
},
{
{ { 1, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 } },
{ { 1, 0 }, { 1, 1 }, { 2, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 }, { 1, 2 } },
{ { 1, 0 }, { 0, 1 }, { 1, 1 }, { 1, 2 } }
},
{
{ { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 } },
{ { 1, 0 }, { 2, 0 }, { 1, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 }, { 2, 2 } },
{ { 1, 0 }, { 1, 1 }, { 0, 2 }, { 1, 2 } }
},
{
{ { 0, 1 }, { 1, 1 }, { 1, 0 }, { 2, 0 } },
{ { 1, 0 }, { 1, 1 }, { 0, 1 }, { 0, 2 } },
{ { 0, 1 }, { 1, 1 }, { 1, 0 }, { 2, 0 } },
{ { 1, 0 }, { 1, 1 }, { 0, 1 }, { 0, 2 } }
},
{
{ { 0, 1 }, { 1, 1 }, { 2, 1 }, { 2, 0 } },
{ { 1, 0 }, { 1, 1 }, { 1, 2 }, { 2, 2 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 } },
{ { 1, 0 }, { 1, 1 }, { 1, 2 }, { 0, 0 } }
},
{
{ { 0, 1 }, { 1, 1 }, { 1, 0 }, { 2, 0 } },
{ { 1, 0 }, { 1, 1 }, { 2, 1 }, { 2, 2 } },
{ { 0, 1 }, { 1, 1 }, { 1, 0 }, { 2, 0 } },
{ { 1, 0 }, { 1, 1 }, { 0, 1 }, { 0, 2 } }
}
};
};
// 绘制游戏界面
void drawBoard() {
system("cls");
gotoxy(0, 0);
cout << "Score: " << score << " Level: " << level << endl;
for (int i = 0; i < boardHeight; i++) {
for (int j = 0; j < boardWidth; j++) {
gotoxy(j * 2 + 2, i + 2);
if (board[i][j] == 0) {
cout << " ";
} else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), board[i][j]);
cout << "■";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
}
cout << endl;
}
}
// 检测是否有满行
int checkFullLines() {
int lines = 0;
for (int i = 0; i < boardHeight; i++) {
bool full = true;
for (int j = 0; j < boardWidth; j++) {
if (board[i][j] == 0) {
full = false;
break;
}
}
if (full) {
lines++;
}
}
return lines;
}
// 游戏主循环
void gameLoop() {
Block block;
block.init();
drawBoard();
while (true) {
if (kbhit()) {
char ch = getch();
if (ch == 'a' || ch == 'A') {
block.move(-1, 0, -1);
} else if (ch == 'd' || ch == 'D') {
block.move(1, 0, -1);
} else if (ch == 's' || ch == 'S') {
block.move(0, 1, -1);
} else if (ch == 'w' || ch == 'W') {
block.move(0, 0, (block.dir + 1) % 4);
} else if (ch == ' ') {
while (block.canMove(0, 1, -1)) {
block.move(0, 1, -1);
}
}
}
if (block.canMove(0, 1, -1)) {
block.move(0, 1, -1);
} else {
block.place();
int lines = block.clear();
if (lines > 0) {
drawBoard();
Sleep(500);
drawBoard();
}
block.init();
if (!block.canMove(0, 0, -1)) {
break;
}
}
drawBoard();
Sleep(1000 / level);
}
gotoxy(0, boardHeight + 3);
cout << "Game Over!" << endl;
}
int main() {
srand(time(NULL));
drawBoard();
gameLoop();
return 0;
}
```
这个代码可能还有一些不足之处,欢迎您进行改进和完善。