C++ 贪吃蛇基础算法2.0

将基础贪吃蛇代码进一步简化改进,可以自定义棋盘大小,可以自定义棋盘与控制台边界的距离,可以自定义墙面,蛇身,蛇头,食物的颜色。

#include <iostream>
#include <windows.h>
#include <cstring>
#include <time.h>
#include <conio.h>
#include <cstdio>
using namespace std;

const long Length=18;                                   //The length of Map
const long Width=15;                                    //The Width  of Map
const long BLength=3;                                   //The Length of Blank
const long BWidth=20;                                   //The Width  of Blank


bool Error(char x,char y)
{
    switch(x-y)
    {
        case 3:case -3:case 4:case -4:case 0: return true;
    }
    return false;
}
//avoid abnormal self-eating and accelerating

void Pos(long x,long y)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos={x,y};
    SetConsoleCursorPosition(direct,pos);
}
//Change the indicator's position 

void Color(long clr)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(direct,clr);
}
//Change the indicator's color

void Show(long judge)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(direct,&cci);
    cci.bVisible=judge;
    SetConsoleCursorInfo(direct,&cci);
}
//Hide the indicator

int main()
{

    char Key,Keys;
    long xh,yh,xt,yt,xm,ym;
    long start,timeover,level;
    long X,Y;
    char map[Width][Length];
    memset(map,' ',sizeof(map));

    for(long i=0;i<Width;i++)
    {
        if((i==0)||(i==Width-1))
            for(long j=0;j<Length;j++)
                map[i][j]='b';
        else
            map[i][0]=map[i][Length-1]='b';
    }
    //Initialize the Map
    xh=7,yh=9;
    xt=7,yt=8;
    map[xt][yt]='d';
    //Initialize the Map

    srand(time(0));
    do{
        xm=rand()%(Width-1)+1;
        ym=rand()%(Length-1)+1;
      }while(map[xm][ym]!=' '||(xm!=xh&&ym!=yh));
    map[xm][ym]='m';
    //Creat the food
    Pos(22,8);
    cout<<"Please input the level (1-8): ";
    cin>>level;
    //Input the level

    Show(0);
    X=BWidth;Y=BLength;
    Pos(X,Y);
    for(long i=0;i<Width;i++)
    {
        for(long j=0;j<Length;j++)
        {
            switch(map[i][j])
            {
                case 'b':
                    Color(0x02);
                    cout<<"■";
                    break;
                case 'w':
                case 'a':
                case 's':
                case 'd':
                    Color(0x0F);
                    cout<<"■";
                    break;
                case 'm':
                    Color(0x03);
                    cout<<"■";
                    break;
                case ' ':
                    Color(0x0F);
                    cout<<"  ";
                    break;
            }
        }
        Y++;
        Pos(X,Y);
    }
    X=BWidth+yh*2;Y=BLength+xh;
    Pos(X,Y);
    Color(0x01);
    cout<<"■";
    //Output the Map        
    //'b' refers to the wall
    //'w' 'a' 's' 'd' refers to the body of snake
    //'m' refers to the food

    Key='d';
    while(1)
    {
        timeover=1;
        start=clock();
        while((timeover=(clock()-start<=(900-level*100)))&&!kbhit());
        {
            Keys=Key;
            if(timeover)
            {
                Key=getch();
                if(Error(Key,Keys))
                {
                    Key=Keys;
                    continue;
                }
            }
            //avoid abnormal self-eating and accelerating
            X=BWidth+yh*2;Y=BLength+xh;
            Pos(X,Y);
            Color(0x0F);
            cout<<"■";
            switch(Key)
            {
                case 'W':case 'w':map[xh][yh]='w';xh--;break;
                case 'S':case 's':map[xh][yh]='s';xh++;break;
                case 'A':case 'a':map[xh][yh]='a';yh--;break;
                case 'D':case 'd':map[xh][yh]='d';yh++;break;
            }
            if(map[xh][yh]!=' '&&map[xh][yh]!='m')
            {
                Pos(30,18);
                cout<<"GameOver";
                getch();
                return 0;
            }
            //Game over

            X=BWidth+yh*2;Y=BLength+xh;
            Pos(X,Y);
            Color(0x01);
            cout<<"■";
            //Move the head
            if(map[xh][yh]=='m')
            {
                srand(time(0));
                do{
                    xm=rand()%(Width-1)+1;
                    ym=rand()%(Length-1)+1;
                  }while(map[xm][ym]!=' '||(xm!=xh&&ym!=yh));
                map[xm][ym]='m';
                X=BWidth+ym*2;Y=BLength+xm;
                Pos(X,Y);
                Color(0x03);
                cout<<"■";
                //Creat the food
            }
            else
            {
                X=BWidth+yt*2;Y=BLength+xt;
                Pos(X,Y);
                Color(0x0F);                
                cout<<"  ";
                switch(map[xt][yt])
                {
                    case 'w':map[xt][yt]=' ';xt--;break;
                    case 's':map[xt][yt]=' ';xt++;break;
                    case 'a':map[xt][yt]=' ';yt--;break;
                    case 'd':map[xt][yt]=' ';yt++;break;
                }
            }
            //Move the tail
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值