跳到正文

休闲·娱乐

Flappy_Bird

wangqicong约 15 分钟
原文
文章信息与版本

文章信息

发布
2026年8月19日
收录
2026年8月19日
更新
2026年11月16日
篇幅
约 15 分钟

版本

  1. v1当前
C++
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <conio.h>
#define WIDTH 35
#define HEIGHT 20
#define PIPE_GAP 8
#define PIPE_FREQ 16
HANDLE hOut;
COORD cursor;
float birdY, birdV;
int score, bestScore, pipeX[3], pipeGapY[3], pipeCount;
int frameDelay;
int gameover;
int gameState; // 0:开始界面 1:等待开始 2:游戏中 3:结束界面
void gotoxy(int x, int y){
    cursor.X = x;
    cursor.Y = y;
    SetConsoleCursorPosition(hOut, cursor);
}
void hideCursor(){
    CONSOLE_CURSOR_INFO cci;
    cci.dwSize = 1;
    cci.bVisible = FALSE;
    SetConsoleCursorInfo(hOut, &cci);
}
void setColor(int color){
    SetConsoleTextAttribute(hOut, color);
}

void drawBorder(){
    setColor(7);
    for(int y = 1; y <= HEIGHT; y++){
        gotoxy(0, y);
        printf("|");
        gotoxy(WIDTH + 1, y);
        printf("|");
    }
    gotoxy(0, HEIGHT + 1);
    for(int i = 0; i <= WIDTH + 1; i++) printf("=");
}
void drawBird(){
    setColor(14);
    gotoxy(8, (int)birdY);
    printf("O");
}
void eraseBird(){
    gotoxy(8, (int)birdY);
    printf(" ");
}
void drawPipe(int idx){
    setColor(10);
    for(int y = 1; y < pipeGapY[idx]; y++){
        gotoxy(pipeX[idx], y);
        printf("H");
    }
    for(int y = pipeGapY[idx] + PIPE_GAP; y <= HEIGHT; y++){
        gotoxy(pipeX[idx], y);
        printf("H");
    }
}
void erasePipe(int idx){
    for(int y = 1; y <= HEIGHT; y++){
        gotoxy(pipeX[idx], y);
        printf(" ");
    }
}
void updateBird(){
    eraseBird();
    birdV += 0.15f;
    if(birdV > 2.5f) birdV = 2.5f;
    birdY += birdV;
    if(birdY < 1){
        birdY = 1;
        birdV = 0;
    }
    if(birdY >= HEIGHT){
        gameover = 1;
    }
    drawBird();
}
void processPipes(){
    for(int i = 0; i < pipeCount; i++){
        erasePipe(i);
        pipeX[i]--;
    }
    for(int i = 0; i < pipeCount; i++){
        if(pipeX[i] == 8){
            int birdIntY = (int)birdY;
            if(birdIntY <= pipeGapY[i] || birdIntY >= pipeGapY[i] + PIPE_GAP){
                gameover = 1;
            }
        }
        if(pipeX[i] == 7 && !gameover){
            score++;
            setColor(14);
            gotoxy(WIDTH + 5, 5);
            printf("+1  ");
            Sleep(30);
            gotoxy(WIDTH + 5, 5);
            printf("    ");
        }
    }
    int newCount = 0;
    for(int i = 0; i < pipeCount; i++){
        if(pipeX[i] > 1){
            pipeX[newCount] = pipeX[i];
            pipeGapY[newCount] = pipeGapY[i];
            newCount++;
        }
    }
    pipeCount = newCount;
    static int frameSinceLastPipe = 0;
    frameSinceLastPipe++;
    if(frameSinceLastPipe >= PIPE_FREQ){
        frameSinceLastPipe = 0;
        if(pipeCount < 3){
            pipeX[pipeCount] = WIDTH;
            pipeGapY[pipeCount] = rand() % (HEIGHT - PIPE_GAP - 2) + 2;
            pipeCount++;
        }
    }
    for(int i = 0; i < pipeCount; i++){
        drawPipe(i);
    }
}
void drawUI(){
    setColor(15);
    gotoxy(WIDTH + 3, 2);
    printf("SCORE: %d", score);
    gotoxy(WIDTH + 3, 3);
    printf("BEST : %d", bestScore);
    setColor(8);
    gotoxy(WIDTH + 3, 6);
    printf("SPACE - 跳跃");
    gotoxy(WIDTH + 3, 7);
    printf("R     - 重来");
    gotoxy(WIDTH + 3, 8);
    printf("ESC   - 退出");
}
void drawStartScreen(){
    setColor(13);
    gotoxy(WIDTH/2 - 7, HEIGHT/2 - 2);
    printf("=== FLAPPY BIRD ===");
    setColor(14);
    gotoxy(WIDTH/2 - 5, HEIGHT/2);
    printf("O  <- 小鸟");
    setColor(10);
    gotoxy(WIDTH/2 - 5, HEIGHT/2 + 1);
    printf("H  <- 管道");
    setColor(11);
    gotoxy(WIDTH/2 - 12, HEIGHT/2 + 3);
    printf("按 [SPACE] 开始游戏");
}
void drawWaitingScreen(){
    setColor(13);
    gotoxy(WIDTH/2 - 8, HEIGHT/2 - 1);
    printf("╔══════════════╗");
    gotoxy(WIDTH/2 - 8, HEIGHT/2);
    printf("║  按空格起飞  ║");
    gotoxy(WIDTH/2 - 8, HEIGHT/2 + 1);
    printf("╚══════════════╝");
}
void drawGameoverScreen(){
    setColor(12);
    gotoxy(WIDTH/2 - 5, HEIGHT/2 - 1);
    printf("=== GAME OVER ===");
    setColor(15);
    gotoxy(WIDTH/2 - 4, HEIGHT/2 + 1);
    printf("得分: %d", score);
    if(score > bestScore){
        bestScore = score;
        setColor(14);
        gotoxy(WIDTH/2 - 6, HEIGHT/2 + 2);
        printf("★ 新纪录! ★");
    }
    setColor(8);
    gotoxy(WIDTH/2 - 6, HEIGHT/2 + 4);
    printf("SPACE - 重新开始");
    gotoxy(WIDTH/2 - 6, HEIGHT/2 + 5);
    printf("ESC   - 退出");
}
void resetGame(){
    birdY = HEIGHT / 2;
    birdV = 0;
    score = 0;
    pipeCount = 0;
    gameover = 0;
    system("cls");
    drawBorder();
    drawUI();
    drawBird();
    drawWaitingScreen();
}
int main(){
    srand(time(NULL));
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    hideCursor();
    system("title Flappy Bird");
    system("mode con cols=55 lines=28");
    bestScore = 0;
    gameState = 0;
    while(1){
        if(gameState == 0){
            system("cls");
            drawBorder();
            drawUI();
            drawStartScreen();
            drawBird();   
            while(1){
                if(kbhit()){
                    char ch = getch();
                    if(ch == ' ' || ch == 32){
                        gameState = 1;
                        resetGame();
                        break;
                    }
                    if(ch == 27){
                        return 0;
                    }
                }
                Sleep(50);
            }
        }
        else if(gameState == 1){
            drawWaitingScreen();   
            if(kbhit()){
                char ch = getch();
                if(ch == ' ' || ch == 32){
                    birdV = -1.2f;
                    gameState = 2;
                    for(int y = HEIGHT/2 - 1; y <= HEIGHT/2 + 1; y++){
                        for(int x = WIDTH/2 - 8; x <= WIDTH/2 + 8; x++){
                            gotoxy(x, y);
                            printf(" ");
                        }
                    }
                }
                if(ch == 27){
                    return 0;
                }
            }
            Sleep(50);
        }
        else if(gameState == 2){
            frameDelay = 45;   
            while(!gameover){
                if(kbhit()){
                    char ch = getch();
                    if(ch == ' ' || ch == 32){
                        birdV = -1.6f;
                    }
                    if(ch == 27){
                        return 0;
                    }
                    if(ch == 'r' || ch == 'R'){
                        gameover = 1;
                        break;
                    }
                }        
                updateBird();
                processPipes();         
                setColor(15);
                gotoxy(WIDTH + 3, 2);
                printf("SCORE: %d", score);
                gotoxy(WIDTH + 3, 3);
                printf("BEST : %d", bestScore);          
                if(birdY >= HEIGHT){
                    gameover = 1;
                    break;
                }
                
                Sleep(frameDelay);
            }  
            gameState = 3;
        }
        else if(gameState == 3){
            drawGameoverScreen(); 
            while(1){
                if(kbhit()){
                    char ch = getch();                
                    if(ch == ' ' || ch == 32){
                        gameState = 1;
                        resetGame();
                        break;
                    }
                    if(ch == 27){
                        return 0;
                    }
                }
                Sleep(100);
            }
        }
    }
    return 0;
}
原文