/ Vijos / 讨论 / 游戏 /

Flappy_bird AI

要是想自己玩,可以去掉函数 ai()

#include <cstdio>
#include <ctime>            //srand(time(NULL));    rand()%(n)
#include <conio.h>
#include <windows.h>        //停顿:Sleep(); 
#include <cstdlib>          //清屏:system("cls");
#include <cstring>          //◎ █※
using namespace std;
//==================================================================
#define hest 100
#define mo 2600
#define hei 20
#define wid 13  
#define gap 6
#define len 3
#define T 25
#define TT 20
#define det (v*2-1)
//hest  跳的速度(点击到最高点有 (250/hest) 秒) 
//mo    跳的高度(每次点击跳到最高多 (((1+hest)*hest+hest)/mo) 格) 
//hei   屏幕高度 
//wid   屏幕宽度 
//gap   柱子间上下的空隙长度 
//len   柱子的宽度 
//T     柱子移动时间单位        T t移动一格
//TT    柱子出现频率          TT T出现一个            
//==================================================================
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int x,int y)//位置函数(列为x 行为y)
{
    COORD pos;
    pos.X=2*x;
    pos.Y=hei-y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int du(){
    int duru=0;
    char cc=getchar();
    while (cc<'0' || cc>'9') cc=getchar();
    while (cc>='0' && cc<='9') duru=duru*10+cc-'0', cc=getchar();
    return duru;
}

int v,h,hx,hy,t,tt,gg,quit,score,plused,wall[100][100];
/*
    t:  时间基本单位          1t=4ms
    tt: 辅助作用                柱子每 移动T格tt++ 
    quit                        记录是否退出游戏 
    plused                      辅助用作计分 
    wall[i][j]  第 i 行第 j 列的柱子情况(0为空 1为有但还没打印 2为打印过了) 
*/ 

void reset(){
    system("cls");
    color(4);
    for (int i=0; i<=wid; i++){gotoxy(i,-1); printf("※");}
    gg=0;
    hx=5;
    hy=hei/2;
    h=hy*mo;
    t=0;
    tt=-5; 
    v=hest;
    score=0;
    memset(wall,0,sizeof(wall));
    color(14); gotoxy(hx,hy); printf("◎");
}

void ai(){
    int f=0,zx=hx,y1,y2;
    while (!f && ++zx<3+wid) for (y1=0; y1<hei; y1++) if (wall[y1][zx]) {f=1; break;}
    while (wall[y1][zx]) y1++; y1--;
    for (y2=y1+1; y2<hei; y2++) if (wall[y2][zx]) break;
    if (!f) zx=hx,y1=1,y2=5;
    //gotoxy(10,10); printf("%d  ",y2);
    //gotoxy(10,9); printf("%d  ",y1);
    int vv=v,yy=h;
    for (int i=0; i<zx-hx; i++) for (int j=1; j<=T; j++){
        yy+=v*2-1;
        vv--;
    }
    //gotoxy(10,8); printf("%d  ",yy/mo);
    if (yy/mo<=y1){
        vv=hest,yy=h,f=1;
        for (int i=1; vv>=-hest/4; i++) for (int j=1; j<=T; j++){
            yy+=vv*2-1;
            vv--;
            //gotoxy(10,8); printf("%d  ",yy/mo);
            if (wall[yy/mo][i+hx] && yy/mo>=y2) {f=0;}
        }
        if (f || yy/mo<=y1) v=hest;
    }
}

void move_bird(){
    h+=(v*2-1);
    if (h>hei*mo) h=hei*mo;
    if (h/mo!=hy){
        gotoxy(hx,hy); printf("  ");
        hy=h/mo;
        if (hy<0) hy=h=0;//gg=1;
        if (wall[hy][hx]) gg=1;
        gotoxy(hx,hy);
        color(14); printf("◎");
    }
}

void move_wall(){
    for (int i=0; i<=hei; i++){
        int k=0,pp=0;
        while (k<wid+5){
            color(2); 
            for (; k<=wid+5 && !wall[i][k]; k++);
            if (k+len/2==hx){
                if (!plused) plused=1, score++;
                if (!pp) color(15),pp=1;
            }
            if (k>=0 && wall[i][k]) wall[i][k-1]=1;
            k-=2; if (k<-1) k=-1;
            while (wall[i][++k]){
                if (hx==k && hy==i) gg=1;
                if (k<=wid && wall[i][k]==1){wall[i][k]=2; gotoxy(k,i); printf("█");}
            }
            wall[i][k-1]=0; gotoxy(k-1,i); printf("  ");
        }
    }
}

void new_wall(){
    srand(time(NULL));
    int dd=(rand()%(hei-gap-1)+1),uu=dd+gap+1;
    for (int i=1; i<=len; i++) wall[dd][i+wid]=1;
    for (int i=dd-1; i>=0; i--) for (int j=2; j<len; j++) wall[i][j+wid]=1;
    for (int i=1; i<=len; i++) wall[uu][i+wid]=1;
    for (int i=uu+1; i<=hei; i++) for (int j=2; j<len; j++) wall[i][j+wid]=1;
    plused=0;
}

void game(){
    while (!gg){
        v--;
        if(kbhit()) {char ch=getch(); v=hest;}
        Sleep(4); t=(t+1)%T;
        if (tt==TT) tt=0, new_wall();
        if (t==0) tt++, move_wall();
        ai();
        move_bird();
        color(8);
        gotoxy(wid/2,2*hei/3);
        printf("%d",score);
    }
    gotoxy(wid/2,-3); color(15); printf("GG~");
    gotoxy(wid/2-3,-5); printf("final score: %d",score);
    gotoxy(hx,hy); color(12); printf("◎");
}

int main(){
    char ch;
    while (ch!='*'){
        reset();
        ch=getch();
        game();
        while (kbhit()) ch=getch();
        Sleep(1000);
        ch=getch();
    }
}

2 条评论

  • @ 2017-08-11 14:53:09

    666

  • @ 2017-04-26 13:26:46

    good!
    简单改了改.

    #include <cstdio>
    #include <ctime>            //srand(time(NULL));    rand()%(n)
    #include <conio.h>
    #include <windows.h>        //停顿:Sleep(); 
    #include <cstdlib>          //清屏:system("cls");
    #include <cstring>          //◎ █※
    #include <iostream>
    using namespace std;
    
    int hei, wid;
    #define hest 100
    #define mo 2600
    #define gap 6
    #define len 3
    #define T 25
    #define TT 20
    #define det (v*2-1)
    void color(int a)//颜色函数
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
    }
    void gotoxy(int x,int y)//位置函数(列为x 行为y)
    {
        COORD pos;
        pos.X=2*x;
        pos.Y=hei-y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
    }
    int du(){
        int duru=0;
        char cc=getchar();
        while (cc<'0' || cc>'9') cc=getchar();
        while (cc>='0' && cc<='9') duru=duru*10+cc-'0', cc=getchar();
        return duru;
    }
    
    int v,h,hx,hy,t,tt,gg,quit,score,plused,wall[100][100];
    /*
        t:  时间基本单位          1t=4ms
        tt: 辅助作用                柱子每 移动T格tt++ 
        quit                        记录是否退出游戏 
        plused                      辅助用作计分 
        wall[i][j]  第 i 行第 j 列的柱子情况(0为空 1为有但还没打印 2为打印过了) 
    */ 
    
    void reset(){
        system("cls");
        color(4);
        for (int i=0; i<=wid; i++){gotoxy(i,-1); printf("※");}
        gg=0;
        hx=5;
        hy=hei/2;
        h=hy*mo;
        t=0;
        tt=-5; 
        v=hest;
        score=0;
        memset(wall,0,sizeof(wall));
        color(14); gotoxy(hx,hy); printf("◎");
    }
    
    void move_bird(){
        h+=(v*2-1);
        if (h>hei*mo) h=hei*mo;
        if (h/mo!=hy){
            gotoxy(hx,hy); printf("  ");
            hy=h/mo;
            if (hy<0) hy=h=0;//gg=1;
            if (wall[hy][hx]) gg=1;
            gotoxy(hx,hy);
            color(14); printf("◎");
        }
    }
    
    void move_wall(){
        for (int i=0; i<=hei; i++){
            int k=0,pp=0;
            while (k<wid+5){
                color(2); 
                for (; k<=wid+5 && !wall[i][k]; k++);
                if (k+len/2==hx){
                    if (!plused) plused=1, score++;
                    if (!pp) color(15),pp=1;
                }
                if (k>=0 && wall[i][k]) wall[i][k-1]=1;
                k-=2; if (k<-1) k=-1;
                while (wall[i][++k]){
                    if (hx==k && hy==i) gg=1;
                    if (k<=wid && wall[i][k]==1){wall[i][k]=2; gotoxy(k,i); printf("█");}
                }
                wall[i][k-1]=0; gotoxy(k-1,i); printf("  ");
            }
        }
    }
    
    void new_wall(){
        srand(time(NULL));
        int dd=(rand()%(hei-gap-1)+1),uu=dd+gap+1;
        for (int i=1; i<=len; i++) wall[dd][i+wid]=1;
        for (int i=dd-1; i>=0; i--) for (int j=2; j<len; j++) wall[i][j+wid]=1;
        for (int i=1; i<=len; i++) wall[uu][i+wid]=1;
        for (int i=uu+1; i<=hei; i++) for (int j=2; j<len; j++) wall[i][j+wid]=1;
        plused=0;
    }
    
    void game(){
        while (!gg){
            v--;
            if(kbhit()) {char ch=getch(); v=hest;}
            Sleep(4); t=(t+1)%T;
            if (tt==TT) tt=0, new_wall();
            if (t==0) tt++, move_wall();
            move_bird();
            color(8);
            gotoxy(wid/2,2*hei/3);
            printf("您目前的游戏得分:%d",score);
        }
        gotoxy(wid/2,-3); color(15); printf("您撞到柱子了!");
        gotoxy(wid/2-3,-5); printf("您的最终游戏得分: %d,输入回车再次开始",score);
        gotoxy(hx,hy); color(12); printf("◎");
    }
    
    int main(){
        cout << "您好,欢迎您使用 Flappy_bird 小游戏。\n您的屏幕高度和宽度:" << endl; 
        cin >> hei >> wid;
        if (hei == 0) hei = 20;
        if (wid == 0) wid = 30;
        char ch;
        while (ch!='*'){
            cout << "输入任意键开始游戏。" << endl; 
            Sleep(1000);
            reset();
            ch=getch();
            game();
            while (kbhit()) ch=getch();
            Sleep(1000);
            ch=getch();
        }
    }
    
  • 1