Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

俄罗斯方块c++代码

作者:若此生已赞。   发布日期:2025-04-29   浏览:131

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h> // for _kbhit() and _getch()

using namespace std;

const int width = 12;
const int height = 20;

char board[height][width];
vector<vector<int>> shapes = {
    {{0, 1, 0}, {1, 1, 1}}, // I
    {{1, 1}, {1, 1}},       // O
    {{1, 1, 0}, {0, 1, 1}}, // S
    {{0, 1, 1}, {1, 1, 0}}, // Z
    {{1, 1, 1}, {0, 1, 0}}, // T
    {{1, 1, 1}, {0, 0, 1}}, // L
    {{1, 1, 1}, {1, 0, 0}}  // J
};

int currentShapeIndex;
vector<int> currentShape;
int currentX, currentY;

void initBoard() {
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if (x == 0 || x == width - 1 || y == height - 1)
                board[y][x] = '#';
            else
                board[y][x] = ' ';
        }
    }
}

void printBoard() {
    system("cls"); // Use "clear" on Linux/Mac
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            cout << board[y][x];
        }
        cout << endl;
    }
}

void newShape() {
    currentShapeIndex = rand() % shapes.size();
    currentShape = shapes[currentShapeIndex];
    currentX = width / 2 - currentShape[0].size() / 2;
    currentY = 0;
}

bool canMove(int dx, int dy) {
    for (int y = 0; y < currentShape.size(); ++y) {
        for (int x = 0; x < currentShape[y].size(); ++x) {
            if (currentShape[y][x]) {
                int newX = currentX + x + dx;
                int newY = currentY + y + dy;
                if (board[newY][newX] != ' ')
                    return false;
            }
        }
    }
    return true;
}

void placeShape() {
    for (int y = 0; y < currentShape.size(); ++y) {
        for (int x = 0; x < currentShape[y].size(); ++x) {
            if (currentShape[y][x]) {
                board[currentY + y][currentX + x] = '*';
            }
        }
    }
}

void clearLines() {
    for (int y = 0; y < height - 1; ++y) {
        bool fullLine = true;
        for (int x = 1; x < width - 1; ++x) {
            if (board[y][x] == ' ') {
                fullLine = false;
                break;
            }
        }
        if (fullLine) {
            for (int yy = y; yy > 0; --yy) {
                for (int xx = 1; xx < width - 1; ++xx) {
                    board[yy][xx] = board[yy - 1][xx];
                }
            }
            y--;
        }
    }
}

void rotateShape() {
    vector<int> rotatedShape(currentShape[0].size(), 0);
    for (int y = 0; y < currentShape.size(); ++y) {
        for (int x = 0; x < currentShape[y].size(); ++x) {
            rotatedShape[x] |= (currentShape[y][x] << (currentShape.size() - y - 1));
        }
    }
    if (canMove(0, 0)) {
        currentShape = rotatedShape;
    }
}

void moveShape(int dx, int dy) {
    if (canMove(dx, dy)) {
        for (int y = 0; y < currentShape.size(); ++y) {
            for (int x = 0; x < currentShape[y].size(); ++x) {
                if (currentShape[y][x]) {
                    board[currentY + y][currentX + x] = ' ';
                }
            }
        }
        currentX += dx;
        currentY += dy;
        placeShape();
    } else if (dy == 1) {
        placeShape();
        clearLines();
        newShape();
        if (!canMove(0, 0)) {
            cout << "Game Over!" << endl;
            exit(0);
        }
    }
}

void gameLoop() {
    srand(time(0));
    initBoard();
    newShape();

    while (true) {
        if (_kbhit()) {
            char key = _getch();
            switch (key) {
                case 'a': moveShape(-1, 0); break;
                case 'd': moveShape(1, 0); break;
                case 's': moveShape(0, 1); break;
                case 'w': rotateShape(); break;
                case 'q': exit(0);
            }
        }

        if (canMove(0, 1)) {
            moveShape(0, 1);
        } else {
            placeShape();
            clearLines();
            newShape();
            if (!canMove(0, 0)) {
                cout << "Game Over!" << endl;
                exit(0);
            }
        }

        printBoard();
        this_thread::sleep_for(chrono::milliseconds(300));
    }
}

int main() {
    gameLoop();
    return 0;
}

解释说明

  1. 初始化游戏板 (initBoard):

    • 创建一个二维字符数组 board,表示游戏区域。边缘用 # 表示墙壁和底部。
  2. 打印游戏板 (printBoard):

    • 清屏后逐行输出游戏板内容。
  3. 生成新方块 (newShape):

    • 随机选择一个方块形状,并将其放置在游戏板顶部中央位置。
  4. 移动检测 (canMove):

    • 检查方块是否可以移动到指定位置,避免碰撞墙壁或已固定的方块。
  5. 放置方块 (placeShape):

    • 将当前方块放置在游戏板上。
  6. 清除满行 (clearLines):

    • 检查是否有满行,并将满行清除,上方的行下移。
  7. 旋转方块 (rotateShape):

    • 计算并应用方块的旋转操作。
  8. 移动方块 (moveShape):

    • 根据输入方向移动方块,如果无法向下移动则固定方块并生成新方块。
  9. 主游戏循环 (gameLoop):

    • 主循环处理用户输入、方块移动、放置和清除满行等逻辑。
  10. 主函数 (main):

    • 启动游戏循环。

这个代码实现了一个简单的俄罗斯方块游戏,使用了基本的控制台输入输出和图形显示。

上一篇:c++中::是什么

下一篇:c++ 优先队列

大家都在看

c++闭包

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++freopen怎么用

c++ 获取系统时间

c++进制转换函数

c++ tcp

c++ gcd函数

c++ cli

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站