代码太长了,一层楼居然放不下,那就分两层放吧
对了,感谢K-Z 12-17-2009 at 12:53 PM.在LinuxQuestions分享的kbhit()函数方法,自己想了好久没想通,虽然都没看懂,但先用着再说
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define random(x) (rand()%(x))
#define MAXSIZEX 20 //地图多少"行"
#define MAXSIZEY 50 //地图每"行"多"长"
#define VOID 0 //空白
#define FOOD -1 //食物
#define WALL -2 //边界
#define HEAD 1 //蛇头
int count = 1; //记录蛇身长度
void MapInit(short *map);
void MoveEat(short *map, short *px, short *py, char dir, short *pX, short *pY);
void GetRan(short *X, short *Y);
void PrintMap(short *map);
int kbhit(void);
int main()
{
short map[MAXSIZEX][MAXSIZEY] = {};
short X, Y, x, y; //食物和自身坐标
short speed = 1;
char dir = 'd'; //w s a d分别为上下左右
char ch = ' ';
srand((int)time(0));
MapInit(&map[0][0]);
x = MAXSIZEX / 2; //初始化蛇头坐标
y = MAXSIZEY / 2;
map[x][y] = HEAD;
GetRan(&X, &Y);
map[X][Y] = FOOD;
printf("游戏速度(每秒前进多少步):");
while(!scanf("%hd", &speed))
{
getchar();
}
getchar();
while(dir != 'q' && dir != 'Q')
{
do
{
MoveEat(&map[0][0], &x, &y, dir, &X, &Y);
PrintMap(&map[0][0]);
printf("\n\n");
printf("当前得分:%d\n", count);
printf("游戏说明:w s a d q分别为上下左右及退出键,按完后请加回车(按键在按下回车时对第一个有效字母作出响应)\n\n");
usleep(1000000 / speed);
} while(!kbhit());
do
{
ch = getchar();
}while(ch != 'w' && ch != 'W' && ch != 's' && ch != 'S' && ch != 'a' && ch != 'A' && ch != 'd' && ch != 'D' && ch != 'q' && ch != 'Q'); //ch的有效性判断
if((count > 1) && ((((dir == 'w' || dir == 'W') && (ch == 's' || ch == 'S'))) || (((dir == 's' || dir == 'S') && (ch == 'w' || ch == 'W'))) || (((dir == 'a' || dir == 'A') && (ch == 'd' || ch == 'D'))) || (((dir == 'd' || dir == 'D') && (ch == 'a' || ch == 'A'))))) //当蛇身长度>1时不能调头
{
ch = dir;
}
dir = ch;
while((ch = getchar()) != '\n')
{
;
}
}
if(dir == 'q' || dir == 'Q')
{
printf("\t感谢您的本次使用,祝您生活愉快^_^\n\n");
}
return 0;
}
void MapInit(short *map)
{
for(int X = 0; X < MAXSIZEX; X++) //初始化地图
{
for(int Y = 0; Y < MAXSIZEY; Y++)
{
if(!X || !Y || X == MAXSIZEX - 1 || Y == MAXSIZEY - 1)
{
*(map+X*MAXSIZEY+Y) = WALL;
}
else
{
*(map+X*MAXSIZEY+Y) = VOID;
}
}
}
}
void GetRan(short *X, short *Y)
{
do
{
*X = random(MAXSIZEX - 1);
} while(!*X);
do
{
*Y = random(MAXSIZEY - 1);
} while(!*Y);
}
void PrintMap(short *map)
{
for(int i = 0; i < MAXSIZEX; i++)
{
printf("\t");
for(int j = 0; j < MAXSIZEY; j++)
{
if(*(map+i*MAXSIZEY+j) == WALL)
{
printf("口");
}
else if(*(map+i*MAXSIZEY+j) == FOOD)
{
printf("田");
}
else if(*(map+i*MAXSIZEY+j) == VOID)
{
printf(" ");
}
else if(*(map+i*MAXSIZEY+j) == HEAD)
{
printf("回");
}
else
{
switch(*(map+i*MAXSIZEY+j)%10)
{
case 1:
printf("一");
break;
case 2:
printf("二");
break;
case 3:
printf("三");
break;
case 4:
printf("四");
break;
case 5:
printf("五");
break;
case 6:
printf("六");
break;
case 7:
printf("七");
break;
case 8:
printf("八");
break;
case 9:
printf("九");
break;
case 0:
printf("十");
break;
}
}
}
printf("\n");
}
return;
}
对了,感谢K-Z 12-17-2009 at 12:53 PM.在LinuxQuestions分享的kbhit()函数方法,自己想了好久没想通,虽然都没看懂,但先用着再说
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define random(x) (rand()%(x))
#define MAXSIZEX 20 //地图多少"行"
#define MAXSIZEY 50 //地图每"行"多"长"
#define VOID 0 //空白
#define FOOD -1 //食物
#define WALL -2 //边界
#define HEAD 1 //蛇头
int count = 1; //记录蛇身长度
void MapInit(short *map);
void MoveEat(short *map, short *px, short *py, char dir, short *pX, short *pY);
void GetRan(short *X, short *Y);
void PrintMap(short *map);
int kbhit(void);
int main()
{
short map[MAXSIZEX][MAXSIZEY] = {};
short X, Y, x, y; //食物和自身坐标
short speed = 1;
char dir = 'd'; //w s a d分别为上下左右
char ch = ' ';
srand((int)time(0));
MapInit(&map[0][0]);
x = MAXSIZEX / 2; //初始化蛇头坐标
y = MAXSIZEY / 2;
map[x][y] = HEAD;
GetRan(&X, &Y);
map[X][Y] = FOOD;
printf("游戏速度(每秒前进多少步):");
while(!scanf("%hd", &speed))
{
getchar();
}
getchar();
while(dir != 'q' && dir != 'Q')
{
do
{
MoveEat(&map[0][0], &x, &y, dir, &X, &Y);
PrintMap(&map[0][0]);
printf("\n\n");
printf("当前得分:%d\n", count);
printf("游戏说明:w s a d q分别为上下左右及退出键,按完后请加回车(按键在按下回车时对第一个有效字母作出响应)\n\n");
usleep(1000000 / speed);
} while(!kbhit());
do
{
ch = getchar();
}while(ch != 'w' && ch != 'W' && ch != 's' && ch != 'S' && ch != 'a' && ch != 'A' && ch != 'd' && ch != 'D' && ch != 'q' && ch != 'Q'); //ch的有效性判断
if((count > 1) && ((((dir == 'w' || dir == 'W') && (ch == 's' || ch == 'S'))) || (((dir == 's' || dir == 'S') && (ch == 'w' || ch == 'W'))) || (((dir == 'a' || dir == 'A') && (ch == 'd' || ch == 'D'))) || (((dir == 'd' || dir == 'D') && (ch == 'a' || ch == 'A'))))) //当蛇身长度>1时不能调头
{
ch = dir;
}
dir = ch;
while((ch = getchar()) != '\n')
{
;
}
}
if(dir == 'q' || dir == 'Q')
{
printf("\t感谢您的本次使用,祝您生活愉快^_^\n\n");
}
return 0;
}
void MapInit(short *map)
{
for(int X = 0; X < MAXSIZEX; X++) //初始化地图
{
for(int Y = 0; Y < MAXSIZEY; Y++)
{
if(!X || !Y || X == MAXSIZEX - 1 || Y == MAXSIZEY - 1)
{
*(map+X*MAXSIZEY+Y) = WALL;
}
else
{
*(map+X*MAXSIZEY+Y) = VOID;
}
}
}
}
void GetRan(short *X, short *Y)
{
do
{
*X = random(MAXSIZEX - 1);
} while(!*X);
do
{
*Y = random(MAXSIZEY - 1);
} while(!*Y);
}
void PrintMap(short *map)
{
for(int i = 0; i < MAXSIZEX; i++)
{
printf("\t");
for(int j = 0; j < MAXSIZEY; j++)
{
if(*(map+i*MAXSIZEY+j) == WALL)
{
printf("口");
}
else if(*(map+i*MAXSIZEY+j) == FOOD)
{
printf("田");
}
else if(*(map+i*MAXSIZEY+j) == VOID)
{
printf(" ");
}
else if(*(map+i*MAXSIZEY+j) == HEAD)
{
printf("回");
}
else
{
switch(*(map+i*MAXSIZEY+j)%10)
{
case 1:
printf("一");
break;
case 2:
printf("二");
break;
case 3:
printf("三");
break;
case 4:
printf("四");
break;
case 5:
printf("五");
break;
case 6:
printf("六");
break;
case 7:
printf("七");
break;
case 8:
printf("八");
break;
case 9:
printf("九");
break;
case 0:
printf("十");
break;
}
}
}
printf("\n");
}
return;
}