终于开学了,我也大三了,新的一年新的开始,我也开始我的博客之旅,哈哈。。。。
#include<iostream>
#include<windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 23
static int rat=0;//改变蛇的行动快慢
static int w=0;
using namespace std;
static int x=1;
static int ki=0; //判断蛇是否在吃食物若为1则正在吃食物
void gotoxy(int x,int y) //光标函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a) //颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2]) //初始化围墙
{
int i,j;
int wall[N+5][N+11]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N+6;j++)
wall[i][j]=1;
}
for(i=0;i<=N+1;i++)
{
for(j=0;j<=N+7;j++)
{
color(28);
if(wall[i][j])
cout<<" ";
else
{
color(18);
cout<<"◆";
}
}
cout<<endl;
}
gotoxy(33,3);//显示信息
color(13);
cout<<"友情提示:"<<endl;
gotoxy(33,5);//显示信息
cout<<"WSAD上下左右"<<endl;
gotoxy(33,7);
cout<<"不可撞到围墙";
gotoxy(33,9);
color(13);
cout<<"不可以吃自己";
gotoxy(33,13);
cout<<"按任意键暂停"<<endl;
gotoxy(33,15);
cout<<"得分:"<<endl;
gotoxy(33,21);
cout<<"输掉了游戏"<<endl;
gotoxy(33,22);
cout<<"你还有人生"<<endl;
//随机产生食物
apple[0]=rand()%N+2;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(31);
cout<<"●"<<endl;//随机产生的第一个食物。
}
DWORD WINAPI Fun(LPVOID lpParamter) //创建播放音乐的线程
{
while(1)
{
PlaySound("music.wav", 0, SND_FILENAME | SND_SYNC);
}
}
int main()
{
HANDLE hThread=CreateThread(NULL,0,Fun,NULL,0,NULL);
CloseHandle(hThread);
while(1)
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=1;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<=len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<=len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(12);
// cout<<"★"<<endl;
}
while(x=1)//进入消息循环
{
Sleep(200-rat); //这个可以改变蛇行进的快慢,数值越大,行动越慢。
if(rat>=190)
rat=0;
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]); //蛇每走一步,蛇尾都会由背景填充。
color(28);
cout<<" "<<endl; //画出背景。
for(i=len-1;i!=0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(30);
cout<<"★"<<endl; //重复画蛇
}
if(kbhit())
{
gotoxy(0,0);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(30); //黄色
cout<<"★"<<endl;//蛇头
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1]) //吃掉苹果后蛇分数加10,蛇长加1
{
ki=1;
score+=10;
len++;
w++;
rat+=10;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+2;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(31);
cout<<"●"<<endl;//被蛇吞掉后随机产生的食物。
gotoxy(37,15);
color(14);
cout<<score<<endl; //输出分数
}
ki=0;
if(ki==0) //当蛇没有吃食物时,如果它撞到自己的身体上,则死亡!
for(i=len-1;i!=0;i--)
if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1])
{
//for(i=0;i<=len;i++)
//free(snake[i]);//释放缓存。
color(30);
system("cls");
Sleep(2000);//等待两秒
gotoxy(7,15);
cout<<"啊!你竟然愚蠢到自杀的地步..不好意思啦!..游戏结束!"<<endl;
//Sleep(INFINITE);
exit(1);
}
if(score==100)
{
x=0;
//Sleep(1000);
gotoxy(15,15);
color(30);
cout<<"恭喜你获得了突破了100分!"<<endl;
x=1;
}
if(snake[0][1]==0||snake[0][1]==N+1||snake[0][0]==0||snake[0][0]==N+7)//撞到围墙后失败
{
//for(i=0;i<=len;i++)
//free(snake[i]);//释放缓存。
system("cls");
Sleep(2000);//等待两秒
gotoxy(15,15);
color(30);
cout<<"Game over!"<<endl;
//Sleep(INFINITE);
exit(1);
}
}
return 0;
}
}