#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
#include <time.h>
/*
Powered by William McCall;
*/
int i=0,j=0;
//------------定义全局变量并初始化---------------------
int image[20][20]={0}; //画图,默认都是0
int FoodX=0; //食物的坐标
int FoodY=0;
int SnakeX; //蛇头的坐标
int SnakeY;
int head=3; //头的位置
int tail=0; //尾巴的位置
int Snake[2][2000]; //蛇身子的位置
int node=4; //设的大小(节数)
int direction=6; //运动方向
char aarroKey; //获取按键
//-------------------------------------------
void GotoXY(int x,int y) //设定光标输出位置,Windows API
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void Color(int a) //颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
//--------------初始化----------------------------
void Init()
{
for (i=0;i<4;i++) //初始化蛇每一节的位置
{
Snake[0][i]=0;
Snake[1][i]=i;
}
for (i=0;i<3;i++)
{
image[0][i]=3; //初始化画图
}
image[0][3]=1;
}
//---------------判断游戏是否结束------------------------
int Gameover()
{
if (SnakeX < 0 || SnakeX > 19 || SnakeY < 0 || SnakeY > 19) //假设出了边界
{
GotoXY(1,25);
return -1;
}
if (image[SnakeX][SnakeY]!=0&&!(SnakeX==FoodX&&SnakeY==FoodY)) //头和尾相碰
{
GotoXY(1,35);
return -1;
}
return 0;
}
//---------------调整方向---------------------------
void shiftLeft()
{
direction=4;
}
void shiftRight()
{
direction=6;
}
void shiftUp()
{
direction=8;
}
void shiftDown()
{
direction=2;
}
//-------------自动移动------------------
void 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:break;
}
}
//--------------产生食物------------------
int GenerateFood()
{
srand((unsigned int)time(NULL)); //生成随机数种子
do
{
FoodX=rand()%20;
FoodY=rand()%20;
} while (image[FoodX][FoodY]!=0);
image[FoodX][FoodY]=2;
return image[FoodX][FoodY];
}
//--------------显示函数----------------
void display()
{
Color(10);
GotoXY(0,0);
for (i=0;i<22;i++)
{
printf("■");
}
GotoXY(0,1);
for (i=0;i<20;i++)
{
Color(10);
printf("■");
for (j=0;j<20;j++)
{
switch(image[i][j])
{
case 0:printf(" ");break;
case 1:Color(11);printf("□");break;
case 2:Color(12);printf("☆");break;
case 3:Color(13);printf("●");break;
}
}
Color(10);
printf("■\n");
}
Color(10);
for (i=0;i<22;i++)
{
printf("■");
}
}
int DrawSnake()
{
if (SnakeX==FoodX&&SnakeY==FoodY)
{
node++;
image[SnakeX][SnakeY]=1;
image[Snake[0][head]][Snake[1][head]]=3;
head=(head+1)%10000;
Snake[0][head]=SnakeX;
Snake[1][head]=SnakeY;
GenerateFood();
display();
}
else
{
image[Snake[0][tail]][Snake[1][tail]]=0;
tail=(tail+1)%10000;
image[Snake[0][head]][Snake[1][head]]=3;
head=(head+1)%10000;
Snake[0][head]=SnakeX;
Snake[1][head]=SnakeY;
image[Snake[0][head]][Snake[1][head]]=1;
display();
}
return 0;
}
//-----------------------------------------------------
int PlayGame()
{
Init();
GenerateFood();
display();
while(1)
{
if (kbhit())
{
aarroKey=getch();
}
if (direction==2||direction==8)
{
switch (aarroKey)
{
case 'd':shiftRight();break;
case 'a':shiftLeft();break;
default:break;
}
}
else if (direction==4||direction==6)
{
switch (aarroKey)
{
case 'w':shiftUp();break;
case 's':shiftDown();break;
default:break;
}
}
Sleep(200);
AutoMove();
if (Gameover()==-1)
{
break;
}
DrawSnake();
display();
}
getch();
return 0;
}
void main()
{
system("cls");
PlayGame();
system("pause");
}
纯C语言 贪吃蛇 工程实践
最新推荐文章于 2021-05-23 16:52:18 发布