新版贪吃蛇

· · 个人记录

这是浩哥做的第一个小游戏--贪吃蛇

有两种模式,为无尽模式和通关模式(2000分通关)

w上s下a左d右q减速e加速

默认速度为最快

你的初始血量为50

当你碰到墙的时候,你不会扣血,会穿越到对面的墙

当你碰到自己的时候,你会扣一滴血

有6种食物:

1.蓝色的○,出现几率:84% 可获得积分:4

2.金色的●,出现几率:5% 可获得积分:100

3.绿色的血,出现几率:3% 可获得积分:10 可获得血量:10

4.紫色的缩,出现几率:3% 可获得积分:10 可以将蛇缩为一节,避免因过长而碰到自己而扣血

5.白色的数字倒计时,出现几率:3% 可获得积分:当你吃掉它时倒计时上的数字

6:会变色的屎,出现几率:1% 可获得积分:400 可获得血量:5 我是老八吗

代码奉上

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
#include<cstdlib> 
using namespace std;
short mapp[32][32]={
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int tx[300]={1,1},ty[300]={2,1},longg=2,jf=0,ming=50; 
char fodf='d',fbug='d';
int slep;
void color(int x) 
{
    if(x>=0 && x<=15)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
    else
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void food()
{
    srand(time(0));
    int x,y,r=rand()%100;
    do{
        x=rand()%20+1,y=rand()%20+1;
    }while(mapp[x][y]);
    if(r<84)mapp[x][y]+=10;
    else if(r<89)mapp[x][y]+=20; 
    else if(r<92) mapp[x][y]+=30;
    else if(r<95) mapp[x][y]+=40;
    else if(r<98){
        int t=rand()%30+170;
        mapp[x][y]=t;
    }
    else mapp[x][y]+=500;
}
void draw()
{
    for(int i=0;i<22;i++){
        for(int j=0;j<22;j++){  
            switch(mapp[i][j]){
                case 0:color(111),cout<<"  ";break;
                case 1:color(111),cout<<"■";break;
                case 32: ming+=5,jf-=490;
                case 502:jf+=400,ming+=5;
                case 22:jf+=96;
                case 12:mapp[i][j]=2,jf+=4,food();
                tx[longg]=tx[longg-1],ty[longg]=tx[longg-1],longg++;
                case 2:color(4),cout<<"●";break;
                case 10:color(9),cout<<"○";break;
                case 20:color(6),cout<<"●";break;
                case 500:color(rand()%15+1),cout<<"屎";break;
                case 30:color(11),cout<<"血";break;
                case 40:color(5),cout<<"缩";break;
                case 42:color(4),cout<<"●";
                for(int i=0;i<longg;i++) mapp[tx[i]][ty[i]]=0;
                longg=1;
                mapp[tx[0]][ty[0]]=2;
                jf+=10;
                food();
                break;
                case 4:color(6),cout<<"◆",ming--,mapp[i][j]=2;break;
                case 100:cout<<"  ";mapp[i][j]=0;food();
                default:
                if(mapp[i][j]<100) mapp[i][j]=0;
                else if(i==tx[0]&&j==ty[0]) jf+=(mapp[i][j]-102),color(4),cout<<"●",food(),mapp[i][j]=2;
                else{
                    color(15),cout<<mapp[i][j]/10%10<<mapp[i][j]%10,mapp[i][j]--;
                } 
            }
        }
        cout<<endl;
    }
    cout<<"长度:"<<longg<<"积分:"<<jf<<"命数:"<<ming;
}
void moving()
{
    if(_kbhit()) fodf=_getch();
    mapp[tx[longg-1]][ty[longg-1]]=0;
    for(int i=longg-1;i;i--){
        tx[i]=tx[i-1],ty[i]=ty[i-1];
    }
    switch(fodf)
    {
        case 'w': tx[0]--;
        if(tx[0]==0) tx[0]=20;
        fbug=fodf;
        break;
        case 's':tx[0]++;
        if(tx[0]==21) tx[0]=1;
        fbug=fodf;
        break;
        case 'a':ty[0]--;
        if(ty[0]==0) ty[0]=20;
        fbug=fodf;
        break;
        case 'd':ty[0]++;
        if(ty[0]==21) ty[0]=1;
        fbug=fodf;
        break;
        case ' ':_getch();fodf=fbug;
        break;
        case 'q': slep+=10;
        fodf=fbug;
        break;
        case 'e': slep-=10;
        fodf=fbug;
        break;
        default:fodf=fbug;
    }
    mapp[tx[0]][ty[0]]+=2;
}
char hahaha()
{
    int a=rand()%16;
    if(a<10) return (char)(a+'0');
    else return (char)(a-10+'a');
}
int main()
{
    char tt;
    cout<<"你要开什么模式? 0键为无尽模式 其他键为通关模式(2000分通关)";
    tt=_getch();
    if(tt=='0'){
        draw();
        while(ming>0&&jf<2000)
        {
            moving();
            Sleep(slep);
            system("cls");
            draw();
        }
        srand(time(0));
        system("cls");
        if(jf>=2000) cout<<"你赢了!!!";
        else cout<<"你输了。。。";
    }
    else {
        draw();
        while(ming>0)
        {
            moving();
            Sleep(slep);
            system("cls");
            draw();
        }
        srand(time(0));
        system("cls");
        cout<<"你死了。。。";
    }
    return 0;
}