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

#include <iostream>
#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;

int board[BOARD_HEIGHT][BOARD_WIDTH] = {0};

int shapes[SHAPE_TYPES][SHAPE_SIZE][SHAPE_SIZE] = {
    {
        {0, 0, 0, 0},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
        {0, 0, 0, 0}
    },
    // 其他方块形状
};

struct Point {
    int x, y;
};

void drawBoard() {
    system("cls");
    for (int i = 0; i < BOARD_HEIGHT; i++) {
        for (int j = 0; j < BOARD_WIDTH; j++) {
            if (board[i][j] == 0) {
                cout << ".";
            } else {
                cout << "#";
            }
        }
        cout << endl;
    }
}

bool isValidMove(int shapeId, int x, int y) {
    for (int i = 0; i < SHAPE_SIZE; i++) {
        for (int j = 0; j < SHAPE_SIZE; j++) {
            if (shapes[shapeId][i][j] != 0) {
                int newX = x + j;
                int newY = y + i;
                if (newX < 0 || newX >= BOARD_WIDTH || newY >= BOARD_HEIGHT ||
                    (newY >= 0 && board[newY][newX] != 0)) {
                    return false;
                }
            }
        }
    }
    return true;
}

void placeShape(int shapeId, int x, int y) {
    for (int i = 0; i < SHAPE_SIZE; i++) {
        for (int j = 0; j < SHAPE_SIZE; j++) {
            if (shapes[shapeId][i][j] != 0) {
                int newX = x + j;
                int newY = y + i;
                board[newY][newX] = 1;
            }
        }
    }
}

void removeShape(int shapeId, int x, int y) {
    for (int i = 0; i < SHAPE_SIZE; i++) {
        for (int j = 0; j < SHAPE_SIZE; j++) {
            if (shapes[shapeId][i][j] != 0) {
                int newX = x + j;
                int newY = y + i;
                board[newY][newX] = 0;
            }
        }
    }
}

void clearLines() {
    for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {
        bool isLineFull = true;
        for (int j = 0; j < BOARD_WIDTH; j++) {
            if (board[i][j] == 0) {
                isLineFull = false;
                break;
            }
        }
        if (isLineFull) {
            for (int k = i; k > 0; k--) {
                for (int j = 0; j < BOARD_WIDTH; j++) {
                    board[k][j] = board[k - 1][j];
                }
            }
            for (int j = 0; j < BOARD_WIDTH; j++) {
                board[0][j] = 0;
            }
        }
    }
}

int main() {
    srand(time(NULL));
    
    bool gameOver = false;
    
    int currentShape = rand() % SHAPE_TYPES;
    int currentX = BOARD_WIDTH / 2 - SHAPE_SIZE / 2;
    int currentY = 0;
    
    while (!gameOver) {
        drawBoard();
        
        if (_kbhit()) {
            char key = _getch();
            switch (key) {
                case 'a':
                    if (isValidMove(currentShape, currentX - 1, currentY)) {
                        removeShape(currentShape, currentX, currentY);
                        currentX--;
                    }
                    break;
                case 'd':
                    if (isValidMove(currentShape, currentX + 1, currentY)) {
                        removeShape(currentShape, currentX, currentY);
                        currentX++;
                    }
                    break;
                case 's':
                    if (isValidMove(currentShape, currentX, currentY + 1)) {
                        removeShape(currentShape, currentX, currentY);
                        currentY++;
                    }
                    break;
                case 'q':
                    removeShape(currentShape, currentX, currentY);
                    currentShape = (currentShape + 1) % SHAPE_TYPES;
                    break;
                case 'e':
                    removeShape(currentShape, currentX, currentY);
                    currentShape = (currentShape + SHAPE_TYPES - 1) % SHAPE_TYPES;
                    break;
                case ' ':
                    while (isValidMove(currentShape, currentX, currentY + 1)) {
                        removeShape(currentShape, currentX, currentY);
                        currentY++;
                    }
                    break;
                case 'x':
                    gameOver = true;
                    break;
            }
        }
        
        if (isValidMove(currentShape, currentX, currentY + 1)) {
            removeShape(currentShape, currentX, currentY);
            currentY++;
        } else {
            placeShape(currentShape, currentX, currentY);
            clearLines();
            
            currentShape = rand() % SHAPE_TYPES;
            currentX = BOARD_WIDTH / 2 - SHAPE_SIZE / 2;
            currentY = 0;
            
            if (!isValidMove(currentShape, currentX, currentY)) {
                gameOver = true;
            }
        }
        
        Sleep(100);
    }
    
    cout << "Game Over!" << endl;
    
    return 0;
}

此代码是一个简单的俄罗斯方块游戏,使用了命令行界面。玩家可以使用键盘上的'a'、'd'、's'、'q'、'e'和空格键来控制方块的移动和旋转。游戏会不断生成新的方块,直到游戏结束。游戏结束的条件是方块无法继续移动到新的位置。在游戏中,已经填满的一行将会消除,得分会根据消除的行数进行计算

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

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

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