用c++编写俄罗斯方块游戏
时间: 2023-08-12 20:08:49 浏览: 158
好的,下面是一个简单的俄罗斯方块游戏的 C++ 代码,你可以参考一下:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 12; // 游戏区宽度
const int height = 20; // 游戏区高度
const int blockWidth = 4; // 方块宽度
const int blockHeight = 4; // 方块高度
const int startX = width / 2 - blockWidth / 2; // 方块初始位置
const int startY = 0;
char blockShapes[7][4][4] = { // 七种方块的形状
{ "....", "....", ".00.", ".00." },
{ "....", "..0.", ".00.", ".0.." },
{ "....", ".0..", ".00.", "..0." },
{ "....", "..0.", "..0.", ".00." },
{ "....", "..0.", ".00.", ".0.." },
{ "....", ".0..", ".00.", ".0.." },
{ "....", ".00.", ".00.", "...." }
};
int gameArea[height][width] = {}; // 游戏区
void drawGameArea() { // 绘制游戏区
system("cls"); // 清屏
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (gameArea[i][j] == 0) {
cout << ".";
} else {
cout << "0";
}
}
cout << endl;
}
}
bool checkCollision(int blockX, int blockY, int blockType) { // 检查方块是否与已有方块冲突
for (int i = 0; i < blockHeight; i++) {
for (int j = 0; j < blockWidth; j++) {
if (blockShapes[blockType][i][j] == '0') {
int x = blockX + j;
int y = blockY + i;
if (x < 0 || x >= width || y >= height) {
return true;
}
if (y >= 0 && gameArea[y][x] != 0) {
return true;
}
}
}
}
return false;
}
void placeBlock(int blockX, int blockY, int blockType) { // 放置方块
for (int i = 0; i < blockHeight; i++) {
for (int j = 0; j < blockWidth; j++) {
if (blockShapes[blockType][i][j] == '0') {
int x = blockX + j;
int y = blockY + i;
gameArea[y][x] = blockType + 1;
}
}
}
}
void removeFullLines() { // 移除已满行
int fullLineCount = 0;
for (int i = height - 1; i >= 0; i--) {
bool fullLine = true;
for (int j = 0; j < width; j++) {
if (gameArea[i][j] == 0) {
fullLine = false;
break;
}
}
if (fullLine) {
fullLineCount++;
for (int j = i - 1; j >= 0; j--) {
for (int k = 0; k < width; k++) {
gameArea[j + 1][k] = gameArea[j][k];
}
}
for (int j = 0; j < width; j++) {
gameArea[0][j] = 0;
}
i++;
}
}
if (fullLineCount > 0) {
Sleep(100); // 等待一段时间,让玩家看清楚哪些行被移除了
}
}
int main() {
srand(time(NULL)); // 初始化随机数生成器
int currentBlockX = startX;
int currentBlockY = startY;
int currentBlockType = rand() % 7;
int nextBlockType = rand() % 7;
int score = 0;
while (true) {
drawGameArea();
// 绘制下一个方块
cout << "Next block:" << endl;
for (int i = 0; i < blockHeight; i++) {
for (int j = 0; j < blockWidth; j++) {
if (blockShapes[nextBlockType][i][j] == '0') {
cout << "0";
} else {
cout << ".";
}
}
cout << endl;
}
// 移动方块
if (_kbhit()) { // 如果有按键输入
char ch = _getch();
if (ch == 'a' && !checkCollision(currentBlockX - 1, currentBlockY, currentBlockType)) {
currentBlockX--;
}
if (ch == 'd' && !checkCollision(currentBlockX + 1, currentBlockY, currentBlockType)) {
currentBlockX++;
}
if (ch == 's' && !checkCollision(currentBlockX, currentBlockY + 1, currentBlockType)) {
currentBlockY++;
}
if (ch == 'w') { // 旋转方块
int newBlockType = (currentBlockType + 1) % 4;
if (!checkCollision(currentBlockX, currentBlockY, newBlockType)) {
currentBlockType = newBlockType;
}
}
} else { // 如果没有按键输入,则让方块自动下落
if (!checkCollision(currentBlockX, currentBlockY + 1, currentBlockType)) {
currentBlockY++;
} else {
placeBlock(currentBlockX, currentBlockY, currentBlockType);
removeFullLines();
score += 10;
currentBlockX = startX;
currentBlockY = startY;
currentBlockType = nextBlockType;
nextBlockType = rand() % 7;
if (checkCollision(currentBlockX, currentBlockY, currentBlockType)) { // 如果新方块无法放置,则游戏结束
cout << "Game over!" << endl;
cout << "Your score: " << score << endl;
system("pause");
return 0;
}
}
}
}
return 0;
}
```
这个代码使用了 Windows API 中的 `Sleep()` 函数来控制游戏速度,如果你在 Linux 或 macOS 上编译运行这个代码,可能需要使用其他方式来控制游戏速度。
阅读全文