贪吃蛇

呵呵!!!

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define N 20
using namespace std;
void mianPage();  //主界面
void end();    // 退出界面
void gotxy(int x,int y); //位置函数;
void color(int a); //颜色函数
void play();//主要操作过程
void init();
class snake{};
void LoadData();
void saveData();
int Score;//存储分数

void gotxy(int x,int y)
{
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void mainPage()
{
    system("cls");

    char c;
    cout<<endl<<endl;
    cout<<endl;
    cout<<"            **********************************************************"<<endl;
    cout<<"            *                                                        *"<<endl;
    cout<<"            *               1  开始游戏                              *"<<endl;
    cout<<"            *                                                        *"<<endl;
    cout<<"            *               2  最高记录                              *"<<endl;
    cout<<"            *                                                        *"<<endl;
    cout<<"            *               3  退出游戏                              *"<<endl;
    cout<<"            *                                                        *"<<endl;
    cout<<"            **********************************************************"<<endl;
    cout<<"            请输入:";
    c=getch();
    switch(c)
    {
        case '1':play();break;
        case '2':LoadData();break;
        case '3':end();break;
        default:cout<<"Please input agin";
    }
    mainPage();
}
void init(int food[2])//初始化框架
{
    system("cls");
    char ch;
    int i,j;
    int wall[N+2][N+2]={{0}};
    color(3);
    color(14);
    for(i=1;i<=N;i++)
    for(j=1;j<=N;j++)
    wall[i][j]=1;
    for(i=0;i<N+2;i++)
    {
        for(j=0;j<N+2;j++)
    {
      if(wall[i][j])
    cout<<" ";
    else
    cout<<"#";
    }
    cout<<endl;
    }
    gotxy(N+10,0);
    color(13);
    cout<<"欢迎来到贪吃蛇游戏  ";
    gotxy(N+10,2);
    cout<<"Score: "<<endl;
    gotxy(N+10,3);
    cout<<"你可以按任意键暂停!!!";
    gotxy(N+10,4);
    cout<<"当然! 你可以按 1 返回,按 2 结束";
    food[0]=rand()%N+1;
    food[1]=rand()%N+1;
    gotxy(food[0],food[1]);
    cout<<"*"<<endl;

    //Sleep(INFINITE);
}
void play()
{
    system("cls");
    int ** snake=NULL;
    int node=3; //蛇的长度;
    int i,j;
    int food[2];
    char a='o',b=1,c=0;//控制变量;
    int tail[2];
    int score=0;
    srand((unsigned)time(NULL));
  //  srand((unsigned)time(0));
    init(food);
   snake=(int**)realloc(snake,sizeof(int*)*node);// 改变snake所指内存区域的大小为node长度
   for(i=0;i<node;i++)
   {
      snake[i]=(int*)malloc(sizeof(int)*2);

   }
   for(i=0;i<node;i++)
   {
       snake[i][0]=N/2;
       snake[i][1]=N/2+i;
       gotxy(snake[i][0],snake[i][1]);
       color(14);
       cout<<"*";
    }

    while(2)
    {

        tail[0]=snake[node-1][0];
        tail[1]=snake[node-1][1];
        gotxy(tail[0],tail[1]);
        color(1);
        cout<<" ";
        for(i=node-1;i>0;i--)//蛇移动的关键步骤
        {
            snake[i][0]=snake[i-1][0];
            snake[i][1]=snake[i-1][1];
            gotxy(snake[i][0],snake[i][1]);
            color(14);
            cout<<"*"<<endl;
        }
        if(kbhit())
        {

            gotxy(0,N+3);
           color(11);
          a=getche();

            c=b;
        }
        switch(a)
        {
            case 'w':snake[0][1]--;break;
            case 's':snake[0][1]++;break;
            case 'd':snake[0][0]++;break;
            case 'a':snake[0][0]--;break;
            case '1':mainPage();break;
            case'2':end();break;
            default :break;
        }
        gotxy(snake[0][0],snake[0][1]);
        color(11);
        cout<<"*";
        Sleep(abs(200-0.5*score));
        if(snake[0][0]==0||(snake[0][0]==N+1)||snake[0][1]==0||snake[0][1]==N+1)
        {
                //Sleep(INFINITE);

                gotxy(N/2,N/2);
                color(14);
                cout<<"GAME OVER !!!";
                color(14);
                Sleep(2000);
                end();
        }
        if(c==b)
        {
            for(j=1;j<node;j++)
            {
               if(snake[0][0]==snake[j][0]&&snake[0][1]==snake[j][1])
               {
                gotxy(N/2,N/2);
            color(13);
            cout<<"你吃到你自己了!!!"<<endl;
            gotxy(N/2+2,N/2+1);
            cout<<"GAME OVER !!!";
            color(14);
            Sleep(2000);
            end();
               }

            }

        }
        if(snake[0][0]==food[0]&&snake[0][1]==food[1])
        {
            node++;
            score++;
            Score=score;
            gotxy(N+16,2);
            cout<<score;
            snake=(int**)realloc(snake,sizeof(int)*node);
            snake[node-1]=(int*)malloc(sizeof(int)*2);
            food[0]=rand()%N+1;
            food[1]=rand()%N+1;
            gotxy(food[0],food[1]);
            color(14);
            cout<<"*";
        }
    }

}
void LoadData()
{
    system("cls");
    FILE *fp;
    int i;
    char s;
    i=Score;
    if((fp=fopen("E:\C语言学习\VC测试\贪吃蛇,呵呵!!!\贪吃蛇.text","r"))==NULL)
    {
        fopen("E:\C语言学习\VC测试\贪吃蛇,呵呵!!!\贪吃蛇.text","w");
        //return ;

    }
//         if(i<S)i=S;

    color(14);

    fread(&i,sizeof(i),1,fp);
    cout<<endl;
    cout<<"	亲!你最高的分为:"<<i;
    cout<<endl;

    cout<<"	按1返回

";

    cout<<"	按2退出

";

    s=getche();

    switch(s)

    {

        case '1':mainPage();break;

        case '2': end();break;

    }

    fclose(fp);



}
void SaveData()
{
    FILE *fp;
    int i;
    i=Score;
    if((fp=fopen("E:\C语言学习\VC测试\贪吃蛇,呵呵!!!\贪吃蛇.text","w"))==NULL)
    {
        cout<<"can't open file!"<<endl;
        exit(0);
    }
 //    if(i<S)i=S;

    color(14);

    fwrite(&i,sizeof(i),1,fp);

    fclose(fp);

}
void end()
{
    system("cls");
    system("cls");
    SaveData();
    char c;
    cout<<endl<<endl;
    cout<<"            **********************************************************"<<endl;
    cout<<"            *                                                        *"<<endl;
    cout<<"            *                 亲,欢迎下次再来!!!                 *"<<endl;
    cout<<"            *                                                        *"<<endl;
    cout<<"            *                  ****     ****                         *"<<endl;
    cout<<"            *                      ****                              *"<<endl;
    cout<<"            **********************************************************"<<endl;
    gotxy(12,9);
    c=getch();
    exit(0);

}
int main()
{

    mainPage();
    return 0;
}
原文地址:https://www.cnblogs.com/yaobolove/p/4108999.html