vvvvvv

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

// 游戏屏幕尺寸
#define WIDTH 10
#define HEIGHT 20

// 定义方块形状
const int TETROMINOS[7][4][4][4] = {
    {
        { {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, 
        { {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0} }, 
        { {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, 
        { {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0} }
    },
    {
        { {1, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, 
        { {1, 0, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0} }, 
        { {0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, 
        { {0, 1, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0} }
    },
    // 其他方块形状省略
};

// 游戏状态
int board[HEIGHT][WIDTH];
int currentPiece[4][4];
int currentX, currentY;
int currentRotation;

void initGame() {
    // 初始化游戏状态
    for (int i = 0; i < HEIGHT; ++i) {
        for (int j = 0; j < WIDTH; ++j) {
            board[i][j] = 0;
        }
    }
    spawnPiece();
}

void drawBoard() {
    system("cls"); // 清屏
    for (int i = 0; i < HEIGHT; ++i) {
        for (int j = 0; j < WIDTH; ++j) {
            if (board[i][j] != 0)
                printf("#");
            else
                printf(".");
        }
        printf("\n");
    }
}

void spawnPiece() {
    // 生成一个新的方块
    int pieceIndex = rand() % 7;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            currentPiece[i][j] = TETROMINOS[pieceIndex][0][i][j];
        }
    }
    currentX = WIDTH / 2 - 2;
    currentY = 0;
    currentRotation = 0;
}

int checkCollision(int x, int y, int rotation) {
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (currentPiece[i][j] != 0) {
                int newX = x + j;
                int newY = y + i;
                if (newX < 0 || newX >= WIDTH || newY >= HEIGHT || (newY >= 0 && board[newY][newX] != 0)) {
                    return 1;
                }
            }
        }
    }
    return 0;
}

void placePiece() {
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (currentPiece[i][j] != 0) {
                board[currentY + i][currentX + j] = 1;
            }
        }
    }
}

void clearLines() {
    for (int i = 0; i < HEIGHT; ++i) {
        int fullLine = 1;
        for (int j = 0; j < WIDTH; ++j) {
            if (board[i][j] == 0) {
                fullLine = 0;
                break;
            }
        }
        if (fullLine) {
            for (int k = i; k > 0; --k) {
                for (int l = 0; l < WIDTH; ++l) {
                    board[k][l] = board[k - 1][l];
                }
            }
            for (int l = 0; l < WIDTH; ++l) {
                board[0][l] = 0;
            }
        }
    }
}

void rotatePiece() {
    int rotatedPiece[4][4];
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            rotatedPiece[i][j] = currentPiece[3 - j][i];
        }
    }
    if (!checkCollision(currentX, currentY, (currentRotation + 1) % 4)) {
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                currentPiece[i][j] = rotatedPiece[i][j];
            }
        }
        currentRotation = (currentRotation + 1) % 4;
    }
}

void update() {
    if (!checkCollision(currentX, currentY + 1, currentRotation)) {
        currentY++;
    } else {
        placePiece();
        clearLines();
        spawnPiece();
        if (checkCollision(currentX, currentY, currentRotation)) {
            printf("Game Over!\n");
            exit(0);
        }
    }
}

void processInput() {
    if (_kbhit()) {
        char input = _getch();
        switch (input) {
            case 'a':
                if (!checkCollision(currentX - 1, currentY, currentRotation)) {
                    currentX--;
                }
                break;
            case 'd':
                if (!checkCollision(currentX + 1, currentY, currentRotation)) {
                    currentX++;
                }
                break;
            case 's':
                if (!checkCollision(currentX, currentY + 1, currentRotation)) {
                    currentY++;
                }
                break;
            case 'w':
                rotatePiece();
                break;
        }
    }
}

int main() {
    srand(time(NULL));
    initGame();
    while (1) {
        drawBoard();
        processInput();
        update();
        Sleep(100); // 控制游戏速度
    }
    return 0;
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值