************准备借此记录下自己的成长历
大一上学期就要结束 了
最后一个的c++大作业是 贪吃蛇 看了好多博主 百度了好多东西
又结合自己写出了这个代码
以下是自己写的代码
#include<Windows.h>
#include<cstdio>
#include<conio.h> //控制台
#include<iostream>
#include <vector>
#include<fstream>
#include<iomanip>
#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<a+2;i++)
{
for(int j=0;j<a+2;j++)
map1[i][j]=0;
}
}
};
void map::flagcolor()
{
system("cls");
cout<<"颜色 大全"<<endl;
cout<<"1.原色"<<endl;
cout<<"2.白色"<<endl;
cout<<"3.红色"<<endl;
cout<<"4.绿色"<<endl;
cout<<"5.蓝色"<<endl;
cout<<"6.黄色"<<endl;
cout<<"7.粉色"<<endl;
cout<<"8.青色"<<endl;
cout<<"其他数选择 原色";
cin>>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;//设置红色//红色(设置的颜色为红色)<<endl;
case 4:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_GREEN);break;//设置绿色
case 5:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_BLUE);break;//蓝色
case 6:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN);break;//设置红色和绿色相加黄色
case 7:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE);break;//设置红色和蓝色相加粉色
case 8:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE);break;//设置绿色和蓝色相加青色
default:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY); break;//原色
}
}
void map::Setmap()
{
for(int i=1;i<=a;i++)
{
for(int j=1;j<=a;j++)
map1[i][j]=1;
}
this->Setcolor(color);
for(int i=0;i<a+2;i++)
{
for(int j=0;j<a+2;j++)
{
if(map1[i][j]==1)
cout<<" ";
else cout<<"■" ;
}
cout<<endl;
}
}
typedef struct snake
{
int x;
int y;
}snake; /*食物蛇头坐标*/
class Snake:public virtual map
{
protected:
int ch;//键盘获取
int flag_live;
int food[2];
int tail[2];
vector<snake>array;
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;i<len;i++)
{
gotoxy(array[i].x,array[i].y);
if(i==0)
cout<<"☆"<<endl;
else
cout<<"※";
}
}
void Snake::run_Snake()
{
tail[0]=array[len-1].x;
tail[1]=array[len-1].y;
for(int i=array.size()-1;i>0;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;i<len-1;i++)
{
array[i].x=array[i+1].x;
array[i].y=array[i+1].y;
}
array[len-1].x=tail[0];
array[len-1].y=tail[1];
break;
}
}
void Snake::make_Food()
{
food[0]=rand()%a+1;
food[1]=rand()%a+1;
for(int i=0;i<array.size();i++)
{
if(array[i].x==food[0]&&array[i].y==food[1])
make_Food();
}
this->gotoxy(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<array.size();i++)
{
if(array[i].x==array[0].x&&array[i].y==array[0].y)
flag_live=0;
}
if(array[0].x==a+1||array[0].y==a+1||array[0].x==0||array[0].y==0)
flag_live=0;
}
class Game:public virtual Snake
{
protected:
int num;
//级别//写到这里 记下级别;
int score;
Game()
{
num=1;
score=0;
};
public:
void play();
void Setnum();
};
void Game::Setnum()
{
system("cls");
cout<<"请开始选择你的游戏难度:";
cout<<"1.小儿科"<<endl;
cout<<"2.简单"<<endl;
cout<<"3.不太简单了哦"<<endl;
cout<<"4.开始难了"<<endl;
cout<<"5.真的不简单了啊"<<endl;
cout<<"6.地狱级别了哦"<<endl;
cout<<"6++++.你会后悔的"<<endl;
cin>>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<<score;
Sleep(abs(200-0.1*num*score));
}
}
class Player :public virtual Game
{
int flag_G;//选择方式;
char name[10][10];//龙王榜
int num_play[10];
char playname[10];
public:
Player()
{
ifstream look_hero("heronum11.txt");
for(int i=0;i<10;i++)
{
if(i==1&&look_hero.eof())/*用的Devc++因为会有一些小的bug所以我用的i==1 0的时候可能因为\r之类的影响*/
{
i=0;
look_hero.close();
ofstream look_hero2("heronum11.txt");
look_hero2<<"纳什男爵 "<<9999<<endl;
look_hero2<<"蛇精 "<<895<<endl;
look_hero2<<"龙神 "<<456<<endl;
look_hero2<<"大娃 "<<354<<endl;
look_hero2<<"小龙王 "<<346<<endl;
look_hero2<<"小哪吒 "<<300<<endl;
look_hero2<<"海中霸王 "<<264<<endl;
look_hero2<<"小靓仔 "<<231<<endl;
look_hero2<<"无敌火轮 "<<124<<endl;
look_hero2<<"靓仔 "<<endl<<123<<endl;
look_hero2.close();
look_hero.open("heronum11.txt");
}
look_hero>>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<<"~开始查看英雄榜"<<endl;
cout<<" 名字 积分"<<endl;
for(int i=0;i<10;i++)
{
cout<<setw(10)<<name[i]<<" "<<num_play[i]<<endl;
}
}
void Player::write_len()
{
int k=10,q;
//k 是 名次
//q 是 改 ID最高名次
int flag_name=0;
for(int i=9;i>=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]<score)
{
for(int i=q;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<<name[i]<<" "<<num_play[i]<<endl;
}
wpr.close();
}
void Player::playGame()
{
system("cls");
this->gotoxy(27,5);//初始化
cout<<"欢迎进入贪吃蛇系统~"<<endl;
this->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;
}