以下是一个简单的用C++代码实现的俄罗斯方块游戏:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <Windows.h>

using namespace std;

const int BOARD_WIDTH = 10;
const int BOARD_HEIGHT = 20;
const int SHAPE_TYPES = 7;
const int SHAPE_SIZE = 4;

const char SHAPES[SHAPE_TYPES][SHAPE_SIZE][SHAPE_SIZE + 1] = {
    {
        "    ",
        "****",
        "    ",
        "    "
    },
    {
        " *  ",
        " *  ",
        " *  ",
        " *  "
    },
    {
        "    ",
        " ** ",
        " ** ",
        "    "
    },
    {
        "    ",
        " ** ",
        "**  ",
        "    "
    },
    {
        "    ",
        "*   ",
        "*** ",
        "    "
    },
    {
        "    ",
        " *  ",
        "*** ",
        "    "
    },
    {
        "    ",
        "*** ",
        " *  ",
        "    "
    }
};

struct Point {
    int x;
    int y;
};

class Tetris {
private:
    char board[BOARD_HEIGHT][BOARD_WIDTH];
    Point currentShapePos;
    int currentShapeIndex;
    int score;

public:
    Tetris() {
        resetBoard();
        currentShapePos.x = BOARD_WIDTH / 2 - SHAPE_SIZE / 2;
        currentShapePos.y = 0;
        currentShapeIndex = rand() % SHAPE_TYPES;
        score = 0;
    }

    void start() {
        while (!isGameOver()) {
            drawBoard();
            processInput();
            update();
            Sleep(500);
        }

        drawBoard();
        cout << "Game Over! Score: " << score << endl;
    }

private:
    void resetBoard() {
        for (int i = 0; i < BOARD_HEIGHT; i++) {
            for (int j = 0; j < BOARD_WIDTH; j++) {
                if (i == BOARD_HEIGHT - 1 || j == 0 || j == BOARD_WIDTH - 1) {
                    board[i][j] = '#';
                } else {
                    board[i][j] = ' ';
                }
            }
        }
    }

    void drawBoard() {
        system("cls");

        for (int i = 0; i < BOARD_HEIGHT; i++) {
            for (int j = 0; j < BOARD_WIDTH; j++) {
                cout << board[i][j];
            }
            cout << endl;
        }
    }

    bool canMove(int dx, int dy) {
        for (int i = 0; i < SHAPE_SIZE; i++) {
            for (int j = 0; j < SHAPE_SIZE; j++) {
                if (SHAPES[currentShapeIndex][i][j] != ' ') {
                    int newX = currentShapePos.x + j + dx;
                    int newY = currentShapePos.y + i + dy;

                    if (newX < 0 || newX >= BOARD_WIDTH || newY >= BOARD_HEIGHT) {
                        return false;
                    }

                    if (newY >= 0 && board[newY][newX] != ' ') {
                        return false;
                    }
                }
            }
        }

        return true;
    }

    void placeShape() {
        for (int i = 0; i < SHAPE_SIZE; i++) {
            for (int j = 0; j < SHAPE_SIZE; j++) {
                if (SHAPES[currentShapeIndex][i][j] != ' ') {
                    int x = currentShapePos.x + j;
                    int y = currentShapePos.y + i;
                    board[y][x] = '#';
                }
            }
        }

        clearFullRows();
        currentShapePos.x = BOARD_WIDTH / 2 - SHAPE_SIZE / 2;
        currentShapePos.y = 0;
        currentShapeIndex = rand() % SHAPE_TYPES;
    }

    void clearFullRows() {
        for (int i = BOARD_HEIGHT - 2; i >= 0; i--) {
            bool isFullRow = true;

            for (int j = 1; j < BOARD_WIDTH - 1; j++) {
                if (board[i][j] == ' ') {
                    isFullRow = false;
                    break;
                }
            }

            if (isFullRow) {
                for (int k = i; k > 0; k--) {
                    for (int j = 1; j < BOARD_WIDTH - 1; j++) {
                        board[k][j] = board[k - 1][j];
                    }
                }

                score += 10;
                i++;
            }
        }
    }

    void processInput() {
        if (_kbhit()) {
            char key = _getch();

            switch (key) {
                case 'a':
                    if (canMove(-1, 0)) {
                        currentShapePos.x--;
                    }
                    break;
                case 'd':
                    if (canMove(1, 0)) {
                        currentShapePos.x++;
                    }
                    break;
                case 's':
                    if (canMove(0, 1)) {
                        currentShapePos.y++;
                    }
                    break;
                case 'w':
                    rotateShape();
                    break;
                case 'q':
                    exit(0);
            }
        }
    }

    void rotateShape() {
        char tempShape[SHAPE_SIZE][SHAPE_SIZE];

        for (int i = 0; i < SHAPE_SIZE; i++) {
            for (int j = 0; j < SHAPE_SIZE; j++) {
                tempShape[i][j] = SHAPES[currentShapeIndex][i][j];
            }
        }

        for (int i = 0; i < SHAPE_SIZE; i++) {
            for (int j = 0; j < SHAPE_SIZE; j++) {
                SHAPES[currentShapeIndex][i][j] = tempShape[SHAPE_SIZE - j - 1][i];
            }
        }

        if (!canMove(0, 0)) {
            for (int i = 0; i < SHAPE_SIZE; i++) {
                for (int j = 0; j < SHAPE_SIZE; j++) {
                    SHAPES[currentShapeIndex][i][j] = tempShape[i][j];
                }
            }
        }
    }

    void update() {
        if (canMove(0, 1)) {
            currentShapePos.y++;
        } else {
            placeShape();
        }
    }

    bool isGameOver() {
        for (int j = 1; j < BOARD_WIDTH - 1; j++) {
            if (board[0][j] != ' ') {
                return true;
            }
        }

        return false;
    }
};

int main() {
    srand(time(NULL));

    Tetris game;
    game.start();

    return 0;
}

此代码实现了一个简单的俄罗斯方块游戏,使用了字符界面进行显示。游戏中使用方向键控制方块的移动和旋转,按下Q键可以退出游戏。游戏结束后会显示得分

用C++代码写一个俄罗斯方块

原文地址: https://www.cveoy.top/t/topic/hSwt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录