c++之贪吃蛇

来源:互联网 发布:mac zip文件怎么打开 编辑:程序博客网 时间:2024/04/28 10:21
#include <stdio.h>#include <windows.h>#include <time.h>#include <conio.h>#include <iostream>using namespace std;//-------------------------------------------------------------------void GotoXY(int x, int y)      //设定输出位置,定位光标{COORD c;c.X = x-1;c.Y = y-1;SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);}//-----------------------------------------------------------------void color(int a)              //颜色函数{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}struct HeroList{char Name[10];int Highscore;}Hero[10];//----------------------------------------------------------void Save()                    //存储最高分及名字{FILE *fp;if((fp=fopen("Hero_list","wb"))==NULL){cout<<"can not open file!"<<endl;return;}if(fwrite(&Hero[0],sizeof(struct HeroList),1,fp)!=1)cout<<"file write error!"<<endl;fclose(fp);}//**********************************************************class Snake{     int image[20][20];        //游戏面板,即小蛇活动范围     int FoodX;                //事物出现的X坐标     int FoodY;                //事物出现的Y坐标     int snakeX;               //记录小蛇的头部X坐标     int snakeY;               //记录小蛇的头部Y坐标     int head;                 //小蛇头部下标     int tail;                 //小蛇尾部下标     int snake[2][2000];         //记录小蛇所有身体的坐标 int direction;            //小蛇头部的方向     int i,j; int gamespeed;            //速度 int score;                //记录分数 char arrowKey;            //接受按键的变量 int GameOver;             //使游戏结束的变量 int level;                //设置等级 int length;               //为了设置等级而与node一样记录设的长度public:Snake()        //构造函数初始化部分变量{        head=3;                //初始化头部下标        tail=0;                //初始化尾部下标    gamespeed=300;         //游戏速度(移动的间隔时间)    score=0;               //初始化分数为0        direction=6;           //小蛇的初始方向向右GameOver=0;            //进入游戏初始化为0level=1;length=4;}~Snake()     //析构函数{}void Init();                //初始化游戏面板及小蛇的坐标int  ENTSnake();           //记录小蛇每次移动后头部及身体的下一坐标void  Automove();            //使小蛇自动移动    int  Gameover();            //判断游戏是否结束int  Generatefood();        //产生食物void display();             //显示游戏面板void shiftLeft();           //控制左移void shiftRight();          //控制右移void shiftDown();           //控制下移void shiftUp();            //控制上移int  PlayGame();            //执行游戏的函数void Setspeed();            //设置速度int Pause();                //游戏的暂停}; void Snake::Init(){ for(i=0;i<20;i++)   for(j=0;j<20;j++)image[i][j]=0; for(i=0;i<4;i++)    { snake[0][i]=1;      snake[1][i]=i+1;    } for(i=1;i<=3;i++)        image[1][i]=3;   image[1][4]=1;}int Snake::Gameover(){  if(snakeX<0 || snakeX>19 ||snakeY<0 || snakeY>19)  {  GotoXY(46,22);      cout << "Game over!" << endl;      GameOver=1;     return GameOver;  }  if((image[snakeX][snakeY]!=0)&&!(snakeX==FoodX&&snakeY==FoodY))   {  GotoXY(46,22);  cout << "Game over!" << endl;  GameOver=1;  return GameOver;  }  return 0;}int Snake::ENTSnake(){   if(snakeX==FoodX && snakeY==FoodY)     {      score+=10;  length++;  if(length>=8)  {length-=8;  level++;}      image[snakeX][snakeY]=1;      image[snake[0][head]][snake[1][head]] = 3;      head=(head+1);      snake[0][head] = snakeX;      snake[1][head] = snakeY;      Generatefood();  display();      }     else     {      image[snake[0][tail]][snake[1][tail]]=0;      tail=(tail+1);      image[snake[0][head]][snake[1][head]]=3;      head=(head+1);      snake[0][head]=snakeX;      snake[1][head]=snakeY;      image[snake[0][head]][snake[1][head]]=1;      display();      }  return 0;}void Snake::shiftLeft(){    direction=4;}void Snake::shiftRight(){    direction=6;}void Snake::shiftUp(){    direction=8;}void Snake::shiftDown(){    direction=2;}void Snake::Automove(){switch(direction){case 6:snakeX= snake[0][head]; snakeY= snake[1][head]+1;break;case 4:snakeX= snake[0][head]; snakeY= snake[1][head]-1;break;case 8:snakeX= snake[0][head]-1; snakeY= snake[1][head];break;case 2:snakeX= snake[0][head]+1; snakeY= snake[1][head];break;    default:;}}int Snake::Generatefood() {srand(time(0));do{     FoodX=rand()%20;     FoodY=rand()%20;}while(image[FoodX][FoodY]!=0);    image[FoodX][FoodY]=2; return image[FoodX][FoodY];}void Snake::display(){color(15);    GotoXY(1,1);    for(i=0;i<22;i++)    printf("■");    GotoXY(1,2);    for(i=0;i<20;i++){color(15);printf("■");for(j=0;j<20;j++){switch(image[i][j]){case 0:color(15);printf("  ");break;case 1:color(13);printf("■");break;case 2:color(15);printf("●");break;case 3:color(15);printf("●");break;}}  color(15);printf("■\n");}for(i=0;i<22;i++)printf("■");  color(15);GotoXY(46,9); printf(" SCORE:%d",score);GotoXY(46,10); printf(" LEVEL=%d",level);color(15);GotoXY(46,6);cout<<" 上移:W    下移:S"<<endl;GotoXY(46,7);cout<<" 左移:A    右移:D"<<endl;GotoXY(46,8);cout<<" 暂停:空格 退出:E"<<endl;}void Snake::Setspeed(){int Esc=0;while(!Esc){int speed;color(12);cout<<"1  2  3"<<endl;cout<<"请输入您的选择:";cin>>speed;switch(speed){case 0:Esc=1;gamespeed=280;break;case 1:gamespeed=280;Esc=1;break;case 2:gamespeed=200;Esc=1;break;case 3:gamespeed=80;Esc=1;break;}}}int Snake::Pause(){    char c;    do{    c=getch();    }while(c!=' ');arrowKey=0;return arrowKey;}int Snake::PlayGame() {   Init();   Generatefood();   display();while (!GameOver)    {        if(kbhit()){          arrowKey = getch();        }if(direction==2||direction==8){              switch(arrowKey)              {  case'd':shiftRight();break;  case'a':shiftLeft();break;  case'e':exit(1);break;  case' ':Pause();break;  default:break;               }}else if(direction==4||direction==6){      switch(arrowKey)  {  case'w':shiftUp();break;  case's':shiftDown();break;  case'e':exit(1);break;  case' ':Pause();break;  default:break;  }}Sleep(gamespeed);Automove();Gameover();if(GameOver==1)            break;ENTSnake();display();}Hero[0].Highscore=score;getch();return 0;}void Welcome(){    color(12);    cout<<"               *                         ● ●                           "<<endl;    cout<<"             *    *                     ●    ●                         "<<endl;    cout<<"           *   *    *                ☆       ●                        "<<endl;    cout<<"         *    ****    *                    ●                           "<<endl;    cout<<"       *       **       *                 ●                            "<<endl;    cout<<"           * * * * *                    ●                              "<<endl;    cout<<"           *   *   *                    ●                              "<<endl;    cout<<"           *   *   *                     ●   ●   ●                     "<<endl;    cout<<"             *   *                    ●    ●       ●                   "<<endl;    cout<<"           *       *               ●   ●   ●  ●   ●                    "<<endl;}int main(){Snake SNAKE;char Name[10];char ch;Welcome();while(1){cout<<"     ▁▁▁▁▁▁▁▁▁▁▁▁▁▁      "<<endl;cout<<"     ▏                          ▏    "<<endl;cout<<"     ▏       1.开始游戏         ▏    "<<endl;cout<<"     ▏       2.设置速度         ▏    "<<endl;cout<<"     ▏       3.英雄榜           ▏    "<<endl;cout<<"     ▏       4.退出             ▏    "<<endl;cout<<"     ▏                          ▏    "<<endl;cout<<"     ▔▔▔▔▔▔▔▔▔▔▔▔▔▔      "<<endl;cin>>ch;if(ch=='1'){system("cls");SNAKE.PlayGame();system("cls");if(Hero[0].Highscore<100){cout<<"You also need to refuel!"<<endl;cout<<endl;}else if(400>Hero[0].Highscore&&Hero[0].Highscore>200){cout<<"You must be superior!"<<endl;cout<<endl;}if(Hero[0].Highscore>200){cout<<"Please enter your name:";cin>>Hero[0].Name;Save();}system("pause");system("cls");break;}else if(ch=='2'){system("cls");SNAKE.Setspeed();system("pause");system("cls");continue;}else if(ch=='3'){system("cls");color(10);FILE *fp;fp=fopen("Hero_list","rb");fread(&Hero[0],sizeof(struct HeroList),1,fp);cout<<"$#######_---   英雄榜:  ---_#######$"<<endl;cout<<"               名字:"<<Hero[0].Name<<endl;cout<<"               最高分:"<<Hero[0].Highscore<<endl;cout<<"--------------------------------------------"<<endl;fclose(fp);system("pause");system("cls");continue;}else if(ch=='4'){system("cls");exit(0);}else{system("cls");cout<<"请重新输入!"<<endl;continue;}}}


0 0
原创粉丝点击