大一上学期大作业贪吃蛇

************准备借此记录下自己的成长历

大一上学期就要结束 了
最后一个的c++大作业是 贪吃蛇 看了好多博主 百度了好多东西
又结合自己写出了这个代码
以下是自己写的代码

#include
#include  
#include //控制台 
#include
#include 
#include
#include
#define a 27
using namespace std;
enum  {  U  ,  R   ,  D  ,  L , S};//上 右 下 左 
class map{
 protected:
 int map1[a+2][a+2];
 int color;
 public:
 void Setcolor(int n_color);
 void flagcolor();
 void Setmap();
 map()
 {
  for(int i=0;i>color;
 this->Setcolor(color);
}
void map::Setcolor(int n_color)
{
 switch(n_color)
 {
 case 1: 
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY); break;//原色
 case 2:
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE);break;//设置三色相加 白色 
 case 3:
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED);break;//设置红色//红色(设置的颜色为红色)<Setcolor(color);
 for(int i=0;iarray;
  int len;//蛇长度;
 public:
  void newSnake()
  {
   array.clear();
   ch=U;
   flag_live=1;
   len=2;
   snake d1,d2;
   d1.x =12;
   d1.y =12;
   d2.x=13;
   d2.y=13;
   array.push_back(d1);
   array.push_back(d2);
  };
  void gotoxy(int x,int y);
  void make_Food();
  void make_Snake();
  void alive_Snake();
  void upgrade_Snake();
  void run_Snake();
};
void Snake::gotoxy(int x ,int y)
{
 COORD c;//定义结构体变量
 c.X = 2*x;
 c.Y = y;
 HANDLE handle;//定义一个句柄
 handle = GetStdHandle(STD_OUTPUT_HANDLE);//获得输出设备的句柄
 SetConsoleCursorPosition(handle,c);//定位句柄控制光标
}
void Snake::make_Snake()
{
 this->Setcolor(color);
 this->gotoxy(tail[0],tail[1]);
 cout<<"  "; 
 for(int i=0;i0;i--)
    {
     array[i].x=array[i-1].x;
     array[i].y=array[i-1].y;
    }
   if (GetAsyncKeyState(VK_UP) && ch != D)
   {
    ch = U;
   }
   else if (GetAsyncKeyState(VK_DOWN) && ch!= U)
   {
    ch = D;
   }
   else if (GetAsyncKeyState(VK_LEFT) && ch!= R)
   {
    ch = L;
   }
   else if (GetAsyncKeyState(VK_RIGHT) && ch != L)
   {
    ch = R;
   }
   else if(GetAsyncKeyState(VK_SPACE)) 
   {
    ch=S;
   }
   switch(ch)
   {
    case U:
     array[0].y--;break;
    case R:
     array[0].x++;break;
    case D:
     array[0].y++;break;
    case L:
     array[0].x--;break;
    case S:
     for(int i=1;igotoxy(food[0],food[1]);
  this->Setcolor(3);//果实为红色; 
  cout<<"●";
}
void Snake::upgrade_Snake()
{
  if(array[0].x==food[0]&&array[0].y==food[1])
  {  
   snake tmp = array.back();//增加一个蛇身单位与蛇尾的状态一
   array.push_back(tmp);//新增加的一个蛇单位与蛇尾相连
   //食物被吃掉后重新设置一个新的食物
   len++;
   this->make_Food();
  }
} 
void Snake::alive_Snake()
{
 for(int i=1;i>num;
}
void Game::play()
{
 this->Setmap();
 this->newSnake();
 this->make_Food();
 this->gotoxy(30,7);
 cout<<"当前分数:";
 this->flag_live=1;
 while(this->flag_live==1)
 {
  this->Setcolor(color);
  this->run_Snake();
  this->make_Snake();
  this->upgrade_Snake();
  this->alive_Snake();
  score=(len-2)*num*num;
  this->Setcolor(1);
  this->gotoxy(32,8);
  cout<>name[i]>>num_play[i]; 
     } 
     look_hero.close();
  }
  void playGame();//开始玩游戏; 
  void look_player();//查看英雄榜
  void write_len();//记录当前游戏者的分数并写入英雄榜 
  void Set_name();//记录用户名字; 
};
void Player::Set_name()
{
 cout<<"请输入新用户的名字";
 cin>>this->playname; 
}
void Player::look_player()
{
 cout<<"~开始查看英雄榜"<=0;i--)
 {
  if(strcmp(this->playname,name[i])==0)
  {
   flag_name=1;//如果 名字重复的 话 记录  
   q=i;//记录 位次 记录名字位次 
  }
  if(score>num_play[i])
  k=i;//记录分数位次 
 }
 if(flag_name==0&&k<=9)
 {
  for(int i=k;i<9;i++)
  { 
   strcpy(name[k+1],name[k]);//开始交换 
   num_play[k+1]=num_play[k];//开始交换 
  }
  strcpy(name[k],playname);//相应的名次交换过来  
  num_play[k]=score;
 }
 else if(flag_name==1&&num_play[q]>score);
 else if(flag_name==1&&num_play[q]k;q--)
  {
   strcpy(name[i],name[i-1]);
  }
  strcpy(name[k],playname);
 }
 ofstream wpr("heronum10.txt");
 for(int i=0;i<10;i++)
 {
  wpr<gotoxy(27,5);//初始化 
 cout<<"欢迎进入贪吃蛇系统~"<gotoxy(27,6);
 cout<<"请开始你的选择";
 this->gotoxy(27,7);
 cout<<"1.查看英雄榜";
 this->gotoxy(27,8);
 cout<<"2.选择游戏难度";
 this->gotoxy(27,9);
 cout<<"3.选择背景颜色";
 this->gotoxy(27,10);
 cout<<"4.开始游戏";
 this->gotoxy(27,11);
 cout<<"5.退出游戏";
 this->gotoxy(27,12);
 cout<<"6.创建新用户";
 this->gotoxy(27,13);
 cout<<"请开始选择:"; 
 cin>>flag_G;
 if(flag_G==1)
 {
  system("cls");
  this->look_player();
  cout<<"按任意键继续";
  getchar();
  getch(); 
  this->playGame();
 }
 else if(flag_G==2)
 {
  system("cls");
  this->Setnum(); 
  cout<<"按任意键继续";
  getch();
  this->playGame();
 }
 else if(flag_G==3)
 {
  system("cls");
  this->flagcolor();
  cout<<"按任意键继续";
  getch();
  this->playGame(); 
 }
 else if(flag_G==4)
 {
  system("cls");
  this->play();
  this->write_len(); //记录分数;
  this->gotoxy(13,13);
  cout<<"GAME OVER!";
  getch();
  getch();
  this->playGame();
 }
 else if(flag_G==5)
 {
  system("cls");
  cout<<"游戏程序运行结束~~~~"; 
 }
 else if(flag_G==6)
 {
  system("cls");
  cout<<"请输入用户名:";
  cin>>playname;
  this->playGame(); 
 }
}
int main()
{
  Player hero;
  int p1;
 cout<<"1.进入游戏并创建人物~~~";
 cout<<"2.离开页面~~";
 cin>>p1;
 if(p1==1)
 {
  hero.Set_name();
  hero.playGame();
 }
 else
 {
  
 }
 return 0;
}

你可能感兴趣的:(c++大一新生贪吃蛇)