C语言控制台贪吃蛇

贪吃蛇


/*
//这个蛇是怎么变长的.....
游戏就是方块和空格的更新
没有墙的地方初始化为0, 有墙的地方初始化为1, 有蛇的地方初始化为2, 食物初始化为3;
所有的操作都在SnakeGroup里面进行;
//
*/
#include 
#include
#include
#include
#include   
#include   
#include   
const int WallWide_x = 28;//墙的宽度
const int WallLength_y = 28;//墙的长度


int LOfSnake;//记录蛇的长度
int SnakeGroup[WallWide_x][WallLength_y];//定义一个数组用来显示方块和空格
using namespace std;


int main()
{
int Level = 300;//初始难度;
int Score = 0;//分数
void GotoXY(int, int);//设置光标位置
int CreateFood();
//初始化墙
for (int i = 0; i < WallLength_y; i++)//y坐标
{
SnakeGroup[0][i] = 1;//初始化最左边的墙
for (int j = 1; j < WallWide_x; j++)//x坐标
{


if (i == 0)//初始化最上面的墙
{
SnakeGroup[j][0] = 1;
}
else
{
if (i == WallLength_y-1)//初始化最下面的墙
{
SnakeGroup[j][WallLength_y-1] = 1;
}
else
SnakeGroup[j][i] = 0;
}

}
SnakeGroup[WallWide_x - 1][i] = 1;//初始化最右边的墙

}
//显示墙
for (int i = 0; i < WallLength_y; i++)
{
for (int j = 0; j < WallWide_x; j++)
{
if (SnakeGroup[j][i] == 0)
cout << "  ";
else
{
if (SnakeGroup[j][i] == 3)
cout << "●";
if (SnakeGroup[j][i] != 0 && SnakeGroup[j][i] != 3)
cout << "█";
}


}
cout << endl;


}
///////////////////////////////////////////////////////////////////
//////////////////////创建一只蛇///////////////////////////////////
///////////////////////////////////////////////////////////////////


srand((unsigned(time(NULL))));
int SnakeLocation_x;
int SnakeLocation_y;
SnakeLocation_x = rand() % (WallWide_x-3);
SnakeLocation_y = rand() % (WallLength_y-3);
SnakeLocation_x = SnakeLocation_x ++;
SnakeLocation_y = SnakeLocation_y ++;
SnakeGroup[SnakeLocation_x][SnakeLocation_y] = 2;
CreateFood();
for (int i = 0; i < WallLength_y; i++)
{
for (int j = 0; j < WallWide_x; j++)
{
if (SnakeGroup[j][i] == 0)
cout << "  ";
else
{
if (SnakeGroup[j][i] == 3)
cout << "●";
if (SnakeGroup[j][i] != 0 && SnakeGroup[j][i] != 3)
cout << "█";
}


}
cout << endl;
}
///////////////////////////////////////////////////////////////
///////////////////蛇的移动////////////////////////////////////
///////////////////////////////////////////////////////////////
int a;
while (1)
{
if (Score ==1)
{
Level = 250;
}
if (Score ==2)
{
Level = 200;
}
if (Score ==3)
{
Level = 100;
}
if (Score ==4)
{
Level = 50;
}
a = _getch();
switch (a)
{
case 'w':
while (!_kbhit())
{


if (a == 'w')
{
if (SnakeGroup[SnakeLocation_x][SnakeLocation_y-1] != 1)
{
SnakeGroup[SnakeLocation_x][SnakeLocation_y] = 0;
SnakeGroup[SnakeLocation_x][--SnakeLocation_y] = 2;


if (SnakeGroup[SnakeLocation_x][SnakeLocation_y-1] == 3)
{
++Score;
SnakeGroup[SnakeLocation_x][SnakeLocation_y] = 0;
SnakeGroup[SnakeLocation_x][--SnakeLocation_y] = 2;
++LOfSnake;
CreateFood();
}
}
else
{ 
GotoXY(24,28);
cout << "游戏失败!" << endl;
GotoXY(24, 32);
cout <<"最终得分: "<< Score<


你可能感兴趣的:(C和C++)