一·所需功能:
1.地图的边界及更新
2.蛇的初始化
3.蛇往四个方向的移动(用键盘实现)
4.蛇撞到身体、障碍(边界或你在地图中定义) 游戏结束
5.蛇吃到食物,蛇就长一节
6.随机放置食物,不可在蛇上或者超出边界
7.得分的显示和增加
二·游戏的流程图:
三·游戏的基本伪代码:
输出字符矩阵
WHILE not 游戏结束 DO
ch=等待输入
CASE ch DO
‘A’:左前进一步,break
‘D’:右前进一步,break
‘W’:上前进一步,break
‘S’:下前进一步,break
END CASE
IF 蛇撞到自己或边界 THEN END WHILE
IF 蛇吃到食物 THEN 蛇变长一截 重新生成食物
输出字符矩阵
END WHILE
输出 Game Over!!!
四.代码的实现
void snakeMove(int, int); //定义蛇的移动
void put_Money(void); //随机出现食物
void output(void); //输出图案
void gameover(void); //输出游戏结束
int shifousiwang(void); //判断蛇是否死亡
void foodChange(void); //判断是否需要更新食物
char map[12][13] = {“**”,
“XXXXH “,
“* *”,
“* *”,
“* *”,
“* *”,
“* *”,
“* *”,
“* *”,
“* *”,
“* *”,
“**”,}; //初始化地图
int snakeX[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1}; //初始化蛇的坐标
int snakeY[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeLength = 5; //初始化蛇的长度
int counter = 1;
int fx, fy, weibaX, weibaY, i; //定义一些临时变量
int main() {
printf(“按空格进入游戏\n”); //打印进入游戏界面的提示语
char ch1 = getch(); //获取第一个命令
if (ch1 == ’ ‘) { //如果输入正确进入游戏
put_Money(); //放置食物
output(); //打印初始地图
for (; 1; ) {
snakeMove(snakeX[snakeLength - 1], snakeY[snakeLength - 1]); //移动蛇
if (shifousiwang())
break; //判断蛇是否死亡
foodChange(); //判断食物是否需要更新
if (counter == 0)
continue; //判断命令是否正确,如果错误重新循环
output();
}
} //进入游戏
else
printf(“这是一个错误的指令\n”); //提示键入错误指令
return 0;
}
void move() {
for (i = 1; i < snakeLength; ++ i)
map[snakeX[snakeLength - i]][snakeY[snakeLength - i]] = map[snakeX[snakeLength - i - 1]][snakeY[snakeLength - i - 1]];
map[snakeX[0]][snakeY[0]] = ’ ‘;
weibaX = snakeX[0];
weibaY = snakeY[0];
for (i = 0; i < snakeLength - 1; ++ i) {
snakeX[i] = snakeX[i + 1];
snakeY[i] = snakeY[i + 1];
}
} //除头以外的移动
void moveA(int headX, int headY) {
map[headX][headY - 1] = map[headX][headY];
move();
snakeY[snakeLength - 1] = snakeY[snakeLength - 1] - 1;
} //A方向的移动
void moveS(int headX, int headY) {
map[headX + 1][headY] = map[headX][headY];
move();
snakeX[snakeLength - 1] = snakeX[snakeLength - 1] + 1;
} //S方向的移动
void moveD(int headX, int headY) {
map[headX][headY + 1] = map[headX][headY];
move();
snakeY[snakeLength - 1] = snakeY[snakeLength - 1] + 1;
} //D方向的移动
void moveW(int headX, int headY) {
map[headX - 1][headY] = map[headX][headY];
move();
snakeX[snakeLength - 1] = snakeX[snakeLength - 1] - 1;
} //W方向的移动
void snakeMove(int headX, int headY) { //蛇的移动
char ch2 = getch();
if (‘a’ <= ch2 && ch2 <= ‘z’)
ch2 = ch2 - ‘a’ + ‘A’;
if (ch2 != ‘A’ && ch2 != ‘S’ && ch2 != ‘D’ && ch2 != ‘W’) {
printf(“这是一个错误的指令\n”);
counter = 0;
return;
}
if (ch2 == ‘A’) {
moveA(headX, headY);
};
if (ch2 == ‘S’) {
moveS(headX, headY);
};
if (ch2 == ‘D’) {
moveD(headX, headY);
};
if (ch2 == ‘W’) {
moveW(headX, headY);
}; //判断方向
}
void output() {
int i;
for (i = 0; i < 12; ++ i)
printf(“%s\n”, map[i]);
} //打印新的场景
void gameover() {
printf(“游戏结束”);
} //提示游戏结束
void put_Money() { //在地图上随机产生食物
while(1) {
fx = rand() % 10;
fy = rand() % 10;
if(map[fx][fy] == ’ ‘) { //不能出现在蛇所占有的位置
map[fx][fy] = SNAKE_FOOD;
break;
}
}
}
int shifousiwang() {
if (snakeX[snakeLength - 1] == 0 || snakeX[snakeLength - 1] == 11 || snakeY[snakeLength - 1] == 0 || snakeY[snakeLength - 1] == 11) {
gameover();
return 1;
}
for (i = 0; i < snakeLength - 2; ++ i) {
if ( snakeX[snakeLength - 1] == snakeX[i] && snakeY[snakeLength - 1] == snakeY[i]) {
gameover();
return 1;
}
}
return 0;
} //判断蛇是否死亡
void foodChange() {
if (snakeX[snakeLength - 1] == fx && snakeY[snakeLength - 1] == fy) {
map[weibaX][weibaY] = SNAKE_BOOY;
++ snakeLength;
for (i = snakeLength - 1; i >= 1; – i) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] = weibaX;
snakeY[0] = weibaY;
put_Money();
}
} //食物的更新