window控制台程序 贪吃蛇

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <math.h>
//#include <unistd.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#define maxmap 10
#define realmap 40
void food();
void show();
void move();
void turn();
void check();
void ini();
struct pair
{
int x,y;
};
struct snake
{
int length;//长度
int speed;//速度
struct pair snakepos[100];//位置
int pointto;//方向  上下左右 1，2，3，4
};
////////////////////////
struct mycolor
{
int x,y;
int number;
}colorpos[10];
int colorcount;
int color[5];//white=7,yellow=6,pink=5;green=2,red=4;
/////////////////////////////////////////////
char map[maxmap][maxmap];
struct pair derict[4];
struct snake A;
int over;
/////////////////////////////////////
{
COORD pos;
HANDLE handle;
pos.X=26;
pos.Y=0;
handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,pos);
printf("white  is 1 ponit");

pos.Y=1;
SetConsoleCursorPosition(handle,pos);
printf("yellow is 2 ponit");

pos.Y=2;
SetConsoleCursorPosition(handle,pos);
printf("pink   is 3 ponit");

pos.Y=3;
SetConsoleCursorPosition(handle,pos);
printf("green  is 4 ponit");

pos.Y=4;
SetConsoleCursorPosition(handle,pos);
printf("red    is 5 ponit");

pos.Y=5;
SetConsoleCursorPosition(handle,pos);
}
void ini()
{
int i,j;
A.length=5;
A.speed=500;
A.pointto=4;//左
//////////////////////////////////////
colorcount=0;
color[0]=7;color[1]=6;color[2]=5;color[3]=2;color[4]=4;
///////////
over=0;
derict[0].x=-1;derict[0].y=0;//注意与数学坐标轴的区别
derict[1].x=1;derict[1].y=0;
derict[2].x=0;derict[2].y=-1;
derict[3].x=0;derict[3].y=1;
srand(time(NULL));
for(i=0;i<A.length;i++)//初始化snake
{
A.snakepos[i].x=1;
A.snakepos[i].y=i+1;
}
for(i=1;i<maxmap-1;i++)
{
for(j=1;j<maxmap-1;j++)
{
map[i][j]=' ';
}
}
for(i=0;i<maxmap;i++)//
{
map[0][i]='#';
map[maxmap-1][i]='#';
map[i][0]='#';
map[i][maxmap-1]='#';
}
for(i=0;i<A.length-1;i++)
{
map[A.snakepos[i].x][A.snakepos[i].y]='X';
}
map[A.snakepos[A.length-1].x][A.snakepos[A.length-1].y]='H';
//////////////////////
food();
}
void debug()
{
int i,j,k;
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄
for(i=0;i<maxmap;i++)
{
for(j=0;j<maxmap;j++)
{
if(map[i][j]=='O')
{
for(k=0;k<colorcount;k++)
{
if(colorpos[k].x==i&&colorpos[k].y==j)
{
SetConsoleTextAttribute(handle,color[colorpos[k].number]+0*0x10);//设置颜色
printf("%c",map[i][j]);
}
}
}
else
{
SetConsoleTextAttribute(handle,7+0*0x10);//设置颜色
printf("%c",map[i][j]);
}
}
printf("\n");
}
}
void show()
{
debug();
while(1)
{
Sleep(A.speed);
turn();
move();
if (over) {  //设置蛇死掉后可以进行的操作
while (1) {
char ch = _getch();
if (ch == 113) {  //输入‘q’结束
return;
}
else if (ch == 114) {  //输入‘r’重新开始
ini();
break;
}
}
}
system("cls");  //清屏
debug();
//calculate(); //计算并记录蛇头与食物距离
}
}
void move()
{
int i,j,t;
t=A.length;
check();
if(t==A.length)//没有吃到
{
map[A.snakepos[0].x][A.snakepos[0].y]=' ';
for(i=0;i<A.length-1;i++)
{
A.snakepos[i].x=A.snakepos[i+1].x;
A.snakepos[i].y=A.snakepos[i+1].y;
}
map[A.snakepos[A.length-1].x][A.snakepos[A.length-1].y]='X';
A.snakepos[A.length-1].x=A.snakepos[A.length-1].x+derict[A.pointto-1].x;
A.snakepos[A.length-1].y=A.snakepos[A.length-1].y+derict[A.pointto-1].y;
map[A.snakepos[A.length-1].x][A.snakepos[A.length-1].y]='H';
}
else
{
map[A.snakepos[A.length-2].x][A.snakepos[A.length-2].y]='X';//易错，A.length已经++了，
A.snakepos[A.length-1].x=A.snakepos[A.length-2].x+derict[A.pointto-1].x;
A.snakepos[A.length-1].y=A.snakepos[A.length-2].y+derict[A.pointto-1].y;
map[A.snakepos[A.length-1].x][A.snakepos[A.length-1].y]='H';
////////////////////////////////////////////////
food();
}
}
void food()
{
int x,y,color,i;
//x=rand()%(maxmap-1)+1;//1--maxmap-1
//y=rand()%(maxmap-1)+1;
///////////////////////设置颜色
colorcount=rand()%2+2;//2---6
for(i=0;i<colorcount;i++)
{
x=rand()%(maxmap-1)+1;//1--maxmap-1
y=rand()%(maxmap-1)+1;
while(map[x][y]!=' ')
{
x=rand()%(maxmap-1)+1;//1--maxmap-1
y=rand()%(maxmap-1)+1;
}
map[x][y]='O';
colorpos[i].x=x;
colorpos[i].y=y;
colorpos[i].number=rand()%5;
}
}
void turn()
{
if(_kbhit())
{
char ch=_getch();
switch(ch)
{
case 'w':A.pointto=1;break;
case 's':A.pointto=2;break;
case 'a':A.pointto=3;break;
case 'd':A.pointto=4;break;
default:break;
}
}
}
void check()
{
int i,j,x,y;
x=A.snakepos[A.length-1].x+derict[A.pointto-1].x;
y=A.snakepos[A.length-1].y+derict[A.pointto-1].y;
if (map[x][y] == '#' || map[x][y] == 'X') {  //如果地图上该坐标处字符为‘*’或‘X’就死亡
if (x != A.snakepos[0].x || y != A.snakepos[0].y) {  //蛇尾除外 ;前进了一格
j=i=maxmap/2;
map[i][j-4] = 'G'; map[i][j-3] = 'A'; map[i][j-2] = 'M'; map[i][j-1] = 'E';  //输出“GAME OVER”
map[i][j+1] = 'O'; map[i][j+2] = 'V'; map[i][j+3] = 'E'; map[i][j+4] = 'R';
map[i][j] = ' ';
system("cls");
for (i = 0; i < maxmap; i++) {
for (j = 0; j < maxmap; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
printf("Input 'r' to restart\nInput 'q' to quit\n");
over = 1;//游戏结束
}
}
else if (map[x][y] == 'O') {  //吃到苹果
A.length++;  //蛇身总长加1
A.speed = ((600 - A.length* 20)>100) ? (600 - A.length* 20) : 100;
for(i=0;i<colorcount;i++)
{
if(colorpos[i].x==x&&colorpos[i].y==y)
{
}
}
for(i=0;i<colorcount;i++)
{
if(map[colorpos[i].x][colorpos[i].y]!='X'&&map[colorpos[i].x][colorpos[i].y]!='H')
{
map[colorpos[i].x][colorpos[i].y]=' ';
}
}
}
}
int main(void)
{
/*init();
while(1){
if(kbhit()){
printf("The user presses a key.\n");
getch();
}
}
return 0; */
char ch;
printf("'w''s''a''d'控制上下左右\n蛇越长跑得越快~~~\n");
printf("按任意键开始\n");
ch = _getch();
system("cls");
ini();
show();
return 0;
}