贪吃蛇

· · 休闲·娱乐

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<bits/stdc++.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
#define int long long
#define KEY_DOWN(vKey) ((GetAsyncKeyState(vKey) & 0x8000) ? 1:0)
#define KEY_UP(vKey) ((GetAsyncKeyState(vKey) & 0x8000) ? 0:1)
using namespace std;
struct node{
    int x;
    int y;
};
int a[25][25];
int lv=1,jy,fang=0,n=23,si,miao=0;
double deng=12;
bool tong,jia;
char cai=0;
node guozi[5];
deque<node> q;
string die;
inline void SetColorAndBackground(int ForgC, int BackC) {
    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

inline void Slowsay(string a){
    int l=a.size();
    for(int i=0;i<l;i++){
        cout<<a[i];
        Sleep(10);
    }
    printf("\n");
}

inline void gotoxy(int x,int y){
    COORD pos;//COORD是一种自带结构体,表示一个字符在控制台屏幕上的坐标
    HANDLE han=GetStdHandle(STD_OUTPUT_HANDLE); //从标准输出设备里取出一个句柄
    pos.X=y,pos.Y=x;
    SetConsoleCursorPosition(han,pos);//定位光标的函数
}

inline void print(int x){
    for(int i=x;i<=n-x+1;i++){
        gotoxy(i,x*2);
        for(int j=x;j<=n-x+1;j++){
            if(i==1||j==1||i==n||j==n)
            SetColorAndBackground(4,4);
            else
            SetColorAndBackground(15,0);
            cout<<"  ";
        }
        SetColorAndBackground(15,0);
        cout<<"\n";
    }
}

inline void popsn(){
    for(int i=0;i<q.size();i++){
        node p=q.front();
        q.pop_front();
        gotoxy(p.x,p.y*2);
        q.push_back(p);
        SetColorAndBackground(15,0);
        cout<<"  ";
    }
}

inline void printsn(){
    popsn();
    for(int i=0;i<q.size();i++){
        node p=q.front();
        q.pop_front();
        gotoxy(p.x,p.y*2);
        q.push_back(p);
        SetColorAndBackground(3,3);
        cout<<"  ";
    }
}

inline void printgo(){
    SetColorAndBackground(10,10);
    gotoxy(guozi[0].x,guozi[0].y*2);
    cout<<"  ";
    if(lv<2)return;
    gotoxy(guozi[1].x,guozi[1].y*2);
    cout<<"  ";
    if(lv<3)return;
    SetColorAndBackground(12,12);
    gotoxy(guozi[2].x,guozi[2].y*2);
    cout<<"  ";
    if(lv<4)return;
    gotoxy(guozi[3].x,guozi[3].y*2);
    cout<<"  ";
    if(lv<5)return;
    SetColorAndBackground(13,13);
    gotoxy(guozi[4].x,guozi[4].y*2);
    cout<<"  ";
}

int wiget(char x){
    if(x==72)return 1;//上
    if(x==80)return -1;//下
    if(x==75)return 2;//左
    if(x==77)return -2;//右
    else return 33;
}

bool chi1(int x,int y){
    if(x==guozi[0].x&&y==guozi[0].y)return 1;
    return 0;
}

bool chi2(int x,int y){
    if(x==guozi[1].x&&y==guozi[1].y)return 1;
    return 0;
}

bool chi3(int x,int y){
    if(x==guozi[2].x&&y==guozi[2].y)return 1;
    return 0;
}

bool chi4(int x,int y){
    if(x==guozi[3].x&&y==guozi[3].y)return 1;
    return 0;
}

bool chi5(int x,int y){
    if(x==guozi[4].x&&y==guozi[4].y)return 1;
    return 0;
}

int sudu;

LARGE_INTEGER  large_interger;
double dff;
__int64  c1, c2;
inline void kbh(){
    if (KEY_DOWN(32)) {
        for(int i=1;i<=sudu;i++){
            if(_kbhit()){
                char xy=_getch();
                if(wiget(xy)==33)continue;
                if(wiget(xy)==0-fang&&q.size()!=1){
                QueryPerformanceCounter(&large_interger);
                c2 = large_interger.QuadPart;
                SetColorAndBackground(15,0);
                system("cls");
                Slowsay(die);
                Slowsay("得 分  :   ");
                cout<<si<<"\n";
                Slowsay("时 间  :   ");
                cout<<(c2 - c1) / dff<<"秒 ("<<(c2 - c1) * 1000 / dff<<"毫秒)\n";
                Slowsay("重 启 中 · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · \n");
                Sleep(1000);
                system("cls");
                if(cai=='2'){
                    system("shutdown -s");
                }
                system("贪吃蛇.exe");
                }else fang=wiget(xy);
            }
        }
    }
    if (KEY_UP(32)) {
        for(int i=1;i<=sudu;i++){
            if(_kbhit()){
                char xy=_getch();
                if(wiget(xy)==33)continue;
                if(wiget(xy)==0-fang&&q.size()!=1){
                QueryPerformanceCounter(&large_interger);
                c2 = large_interger.QuadPart;
                SetColorAndBackground(15,0);
                system("cls");
                Slowsay(die);
                Slowsay("得 分  :   ");
                cout<<si<<"\n";
                Slowsay("时 间  :   ");
                cout<<(c2 - c1) / dff<<"秒 ("<<(c2 - c1) * 1000 / dff<<"毫秒)\n";
                Slowsay("重 启 中 · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · \n");
                Sleep(1000);
                system("cls");
                if(cai=='2'){
                    system("shutdown -s");
                }
                system("贪吃蛇.exe");
                }else fang=wiget(xy);
            }
        }
    }
}

inline void qiang(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            a[i][j]=0;
            if(i==1||j==1||i==n||j==n)a[i][j]=1;
            if(i==n/2&&tong==0)a[i][j]=7;
        }
    }
}

bool siwang(){
    qiang();
    bool s=0;
    node xp=q.front();
    q.pop_front();
    q.push_back(xp);
    for(int i=0;i<q.size()-1;i++){
        node p=q.front();
        q.pop_front();
        if(p.x==xp.x&&p.y==xp.y){
            s=1;
            break;
        }
        q.push_back(p);
    }
    if(a[q.front().x][q.front().y]==1){
        die="      死因:撞墙··";
        return 1;
    }
    else if(s){
        die="      死因:咬到自己··········";
        return 1;
    }
    else if(a[q.front().x][q.front().y]==7){
        die="      死因:不守交通规则(乱闯红绿灯)··········";
        return 1;
    }
    else return 0;
}

bool siw(int g){
    qiang();
    if(a[guozi[g].x][guozi[g].y]==1||a[guozi[g].x][guozi[g].y]>=2||guozi[g].x==n/2)return 1;
    return 0;
}

inline void shuaxin(int k){
    while(1){
        guozi[k].x=rand()%23+1;
        guozi[k].y=rand()%23+1;
        if(!siw(k)){
            return;
        }
    }
}

inline void yidong(){
    node p=q.front();
    if(fang==1){
        q.push_front({p.x-1,p.y});
        if(!chi1(q.front().x,q.front().y)&&!chi2(q.front().x,q.front().y)&&!chi3(q.front().x,q.front().y)&&!chi4(q.front().x,q.front().y)&&!chi5(q.front().x,q.front().y)){
            q.pop_back();
        }
        if(chi1(q.front().x,q.front().y)&&lv>=1)shuaxin(0),jy++,si++;
        if(chi2(q.front().x,q.front().y)&&lv>=2)shuaxin(1),jy++,si++;
        if(chi3(q.front().x,q.front().y)&&lv>=3)shuaxin(2),jy+=2,si++;
        if(chi4(q.front().x,q.front().y)&&lv>=4)shuaxin(3),jy+=4,si++;
        if(chi5(q.front().x,q.front().y)&&lv>=5)shuaxin(4),jy+=7,si++;
    }
    else if(fang==-1){
        q.push_front({p.x+1,p.y});
        if(!chi1(q.front().x,q.front().y)&&!chi2(q.front().x,q.front().y)&&!chi3(q.front().x,q.front().y)&&!chi4(q.front().x,q.front().y)&&!chi5(q.front().x,q.front().y)){
            q.pop_back();
        }
        if(chi1(q.front().x,q.front().y)&&lv>=1)shuaxin(0),jy++,si++;
        if(chi2(q.front().x,q.front().y)&&lv>=2)shuaxin(1),jy++,si++;
        if(chi3(q.front().x,q.front().y)&&lv>=3)shuaxin(2),jy+=2,si++;
        if(chi4(q.front().x,q.front().y)&&lv>=4)shuaxin(3),jy+=4,si++;
        if(chi5(q.front().x,q.front().y)&&lv>=5)shuaxin(4),jy+=7,si++;
    }
    else if(fang==2){
        q.push_front({p.x,p.y-1});
        if(!chi1(q.front().x,q.front().y)&&!chi2(q.front().x,q.front().y)&&!chi3(q.front().x,q.front().y)&&!chi4(q.front().x,q.front().y)&&!chi5(q.front().x,q.front().y)){
            q.pop_back();
        }
        if(chi1(q.front().x,q.front().y)&&lv>=1)shuaxin(0),jy++,si++;
        if(chi2(q.front().x,q.front().y)&&lv>=2)shuaxin(1),jy++,si++;
        if(chi3(q.front().x,q.front().y)&&lv>=3)shuaxin(2),jy+=2,si++;
        if(chi4(q.front().x,q.front().y)&&lv>=4)shuaxin(3),jy+=4,si++;
        if(chi5(q.front().x,q.front().y)&&lv>=5)shuaxin(4),jy+=7,si++;
    }
    else if(fang==-2){
        q.push_front({p.x,p.y+1});
        if(!chi1(q.front().x,q.front().y)&&!chi2(q.front().x,q.front().y)&&!chi3(q.front().x,q.front().y)&&!chi4(q.front().x,q.front().y)&&!chi5(q.front().x,q.front().y)){
            q.pop_back();
        }
        if(chi1(q.front().x,q.front().y)&&lv>=1)shuaxin(0),jy++,si++;
        if(chi2(q.front().x,q.front().y)&&lv>=2)shuaxin(1),jy++,si++;
        if(chi3(q.front().x,q.front().y)&&lv>=3)shuaxin(2),jy+=2,si++;
        if(chi4(q.front().x,q.front().y)&&lv>=4)shuaxin(3),jy+=4,si++;
        if(chi5(q.front().x,q.front().y)&&lv>=5)shuaxin(4),jy+=7,si++;
    }
}

inline void printd(){
    gotoxy(n/2,4);for(int i=1;i<n-1;i++){
        if(tong==0)
            SetColorAndBackground(12,12);
        else
            SetColorAndBackground(2,2);
        cout<<"  ";
    }
}

signed main(){
    Sleep(1000);
    srand(time(0));
    Slowsay("1.正常模式\n2.彩蛋模式");
    cai=_getch();
    Slowsay("欢迎来到原创贪吃蛇(C++14版单人)\n作者::a_middle\n上下左右键控制,经验满自动升级\n升级可以刷更多果子\n请输入速度:(1~4)4为慢,1为最快\n作者推荐3,新手先用4,玩多了再用2或1");
    sudu=_getch()-48;
    if(sudu!=1&&sudu!=2&&sudu!=3&&sudu!=4){
        Slowsay("默认为3\n");
        sudu=3;
    }
    sudu*=3000;
    system("cls");
    //
    Slowsay("  \n贪 吃 蛇    启动!!!!!!!!!!!!\n\n");
    Sleep(300);
    system("cls");
    cout<<"\n";
    print(1);
    //
    q.push_front({rand()%19+2,rand()%19+2});
    printsn();
    printd();
    fang=_getch();
    shuaxin(0);
    //
    QueryPerformanceFrequency(&large_interger);
    dff = large_interger.QuadPart;
    QueryPerformanceCounter(&large_interger);
    c1 = large_interger.QuadPart;
    //
    while(1){
        miao++;
        kbh();
        node p=q.front();
        popsn();
        qiang();
        printgo();
        printd();
        if(KEY_DOWN(32)) deng-=0.5;
        else deng--;
        if(deng<=0){
            deng=12+8*!tong;
            tong=!tong;
        }
        SetColorAndBackground(15,0);
        gotoxy(n/2,n*2+3);printf("%.0f",deng);
        qiang();
        yidong();
        if(siwang()){
            QueryPerformanceCounter(&large_interger);
            c2 = large_interger.QuadPart;
            SetColorAndBackground(15,0);
            system("cls");
            Slowsay(die);
            Slowsay("得 分  :   ");
            cout<<si<<"\n";
            Slowsay("时 间  :   ");
            cout<<(c2 - c1) / dff<<"秒 ("<<(c2 - c1) * 1000 / dff<<"毫秒)\n";
            Slowsay("重 启 中 · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · \n");
            Sleep(1000);
            system("cls");
            if(cai=='2'){
                system("shutdown -s");
            }
            system("贪吃蛇.exe");
        }
        while(q.size()>12){
            popsn();
            q.pop_back();
            printsn();
        }
        popsn();
        printsn();
        if(jy>=lv*lv*2){
            jy-=lv*lv*2;
            lv++;
            shuaxin(lv-1);
            sudu--;
            sudu=max(signed(sudu),9);
            while(q.size()>4){
                popsn();
                q.pop_back();
                printsn();
            }
            popsn();
            printsn();
        }
        SetColorAndBackground(15,0);
        gotoxy(n+2,n*2+2);
        cout<<"得 分  :   ";
        cout<<si<<"\n";
        cout<<"等 级  :   ";
        cout<<lv<<"\n";
        cout<<"经 验  :   ";
        cout<<jy<<"\n";
    }
    return 0;
}