由于代码较长,无法一次性贴出,请参考以下代码(注释中有详细说明):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

#define WIDTH 12 // 游戏区域宽度
#define HEIGHT 24 // 游戏区域高度

#define BLOCK_NUM 7 // 方块种类数
#define BLOCK_SIZE 4 // 方块大小

// 方块形状
const int blocks[BLOCK_NUM][BLOCK_SIZE][BLOCK_SIZE] = {
    // I型
    {
        {0, 0, 0, 0},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
    // J型
    {
        {1, 0, 0, 0},
        {1, 1, 1, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
    // L型
    {
        {0, 0, 1, 0},
        {1, 1, 1, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
    // O型
    {
        {1, 1, 0, 0},
        {1, 1, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
    // S型
    {
        {0, 1, 1, 0},
        {1, 1, 0, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
    // T型
    {
        {0, 1, 0, 0},
        {1, 1, 1, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
    // Z型
    {
        {1, 1, 0, 0},
        {0, 1, 1, 0},
        {0, 0, 0, 0},
        {0, 0, 0, 0},
    },
};

// 方块颜色
const int colors[BLOCK_NUM] = {
    FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY, // I型
    FOREGROUND_BLUE | FOREGROUND_INTENSITY, // J型
    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, // L型
    FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY, // O型
    FOREGROUND_GREEN | FOREGROUND_INTENSITY, // S型
    FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY, // T型
    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, // Z型
};

// 游戏区域
int area[HEIGHT][WIDTH] = {0};

// 当前方块坐标和类型
int block_x, block_y, block_type;

// 下一个方块类型
int next_block_type;

// 得分和等级
int score = 0, level = 1;

// 游戏是否结束
int game_over = 0;

// 生成随机方块
int rand_block() {
    return rand() % BLOCK_NUM;
}

// 检查当前方块是否可以放置
int check_block(int x, int y, int type) {
    int i, j;
    for (i = 0; i < BLOCK_SIZE; i++) {
        for (j = 0; j < BLOCK_SIZE; j++) {
            if (blocks[type][i][j] && (x + j < 0 || x + j >= WIDTH || y + i >= HEIGHT || (y + i >= 0 && area[y + i][x + j]))) {
                return 0;
            }
        }
    }
    return 1;
}

// 将当前方块放置到游戏区域中
void put_block(int x, int y, int type) {
    int i, j;
    for (i = 0; i < BLOCK_SIZE; i++) {
        for (j = 0; j < BLOCK_SIZE; j++) {
            if (blocks[type][i][j]) {
                area[y + i][x + j] = type + 1;
            }
        }
    }
}

// 消除满行并得分
void clear_lines() {
    int i, j, k;
    int lines = 0;
    for (i = HEIGHT - 1; i >= 0; i--) {
        int flag = 1;
        for (j = 0; j < WIDTH; j++) {
            if (!area[i][j]) {
                flag = 0;
                break;
            }
        }
        if (flag) {
            lines++;
            for (k = i - 1; k >= 0; k--) {
                for (j = 0; j < WIDTH; j++) {
                    area[k + 1][j] = area[k][j];
                }
            }
            for (j = 0; j < WIDTH; j++) {
                area[0][j] = 0;
            }
            i++;
        }
    }
    score += 100 * lines * lines; // 得分
    level = score / 1000 + 1; // 等级
}

// 显示游戏区域
void draw_area() {
    int i, j;
    HANDLE hOut;
    COORD pos = {0, 0};
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos); // 光标移动到左上角
    for (i = 0; i < HEIGHT; i++) {
        for (j = 0; j < WIDTH; j++) {
            if (area[i][j]) {
                SetConsoleTextAttribute(hOut, colors[area[i][j] - 1]);
                printf("[]");
            } else {
                printf("  ");
            }
        }
        printf("\n");
    }
    printf("Score: %d  Level: %d\n", score, level);
}

// 显示下一个方块
void draw_next_block() {
    int i, j;
    HANDLE hOut;
    COORD pos = {WIDTH * 2 + 5, 3};
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
    printf("Next:");
    pos.Y += 2;
    SetConsoleCursorPosition(hOut, pos);
    for (i = 0; i < BLOCK_SIZE; i++) {
        for (j = 0; j < BLOCK_SIZE; j++) {
            if (blocks[next_block_type][i][j]) {
                SetConsoleTextAttribute(hOut, colors[next_block_type]);
                printf("[]");
            } else {
                printf("  ");
            }
        }
        printf("\n");
        pos.Y++;
        SetConsoleCursorPosition(hOut, pos);
    }
}

// 初始化游戏
void init_game() {
    srand(time(NULL));
    block_x = WIDTH / 2 - BLOCK_SIZE / 2;
    block_y = 0;
    block_type = rand_block();
    next_block_type = rand_block();
}

// 处理游戏逻辑
void update_game() {
    int i, j;
    // 检查是否可以向下移动
    if (check_block(block_x, block_y + 1, block_type)) {
        block_y++;
    } else {
        put_block(block_x, block_y, block_type); // 将当前方块放置到游戏区域中
        clear_lines(); // 消除满行并得分
        block_x = WIDTH / 2 - BLOCK_SIZE / 2;
        block_y = 0;
        block_type = next_block_type;
        next_block_type = rand_block(); // 生成下一个方块
        if (!check_block(block_x, block_y, block_type)) { // 检查新的方块是否可以放置
            game_over = 1;
        }
    }
}

// 处理用户输入
void handle_input() {
    if (_kbhit()) { // 检查是否有按键按下
        switch (_getch()) {
            case 'a': // 左移
                if (check_block(block_x - 1, block_y, block_type)) {
                    block_x--;
                }
                break;
            case 'd': // 右移
                if (check_block(block_x + 1, block_y, block_type)) {
                    block_x++;
                }
                break;
            case 's': // 下移
                if (check_block(block_x, block_y + 1, block_type)) {
                    block_y++;
                }
                break;
            case 'w': // 旋转
                {
                    int temp_type = block_type;
                    block_type = (block_type + 1) % BLOCK_NUM;
                    if (!check_block(block_x, block_y, block_type)) {
                        block_type = temp_type;
                    }
                    break;
                }
            case 'p': // 暂停
                system("pause");
                break;
            case 'q': // 退出
                game_over = 1;
                break;
        }
    }
}

// 主函数
int main() {
    init_game(); // 初始化游戏
    while (!game_over) {
        update_game(); // 处理游戏逻辑
        draw_area(); // 显示游戏区域
        draw_next_block(); // 显示下一个方块
        Sleep(300 - level * 30); // 根据等级控制下落速度
        handle_input(); // 处理用户输入
    }
    printf("Game over!\n");
    return 0;
}
``
用c语言编写俄罗斯方块代码

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

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