作为一个没有基础的编程小白,写贪吃蛇的过程可以说是非常痛苦了…orz
简要说说自己的学习过程。鉴于篇幅有限,只在文末放上最终代码。
一、整体思路
1.先写出如下的伪代码:
输出字符矩阵
WHILE not 游戏结束 DO
ch=等待输入
CASE ch DO
‘A’:左前进一步,break
‘D’:右前进一步,break
‘W’:上前进一步,break
‘S’:下前进一步,break
END CASE
输出字符矩阵
END WHILE
输出 Game Over!!!
2.按“自顶向下”的方法,写出主程序框架
我们的思路很简单:
·打印地图
·定义一个snakemove函数,在主函数中,用switch判断输入字符的不同情况,再将不同参数传递给snakemove函数,进行相应的操作。
二、先做一个会动的蛇吧
在做第一版的时候,我的思路是通过检索找到蛇的头部和尾部的位置,因为蛇移动时只有头部、尾部所在位置会发生变化,因此可以忽略中间的字符。
然而这种方法产生了困难:蛇的尾部位置很难判断。
于是,我想到用定义一个struct结构的方法,传递、储存所有snake所有字符的位置。
三、添加食物,让蛇也能长大~
查询后发现,这里如果直接用rand函数,食物只会出现在一个随机位置;这里用到了srand函数,并使在地图无食物的情况下,每隔一段时间产生随机食物。
四、一个简单的贪吃蛇就完成啦!放上代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define up -1, 0
#define down 1, 0
#define left 0, -1
#define right 0, 1
char map[12][13] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};
struct coord{
int coordX;
int coordY;
};
/*打印地图函数*/
void printMap() {
for (int i = 0; i < 12; ++ i) {
for (int j = 0; j < 12; ++ j) printf("%c", map[i][j]);
putchar('\n');
}
}
//游戏结束函数
void gameover() {
printf("Game Over!!");
system("pause");//此处使界面能够显示完字符后再退出
exit(0);
}
//随机产生食物的坐标
struct coord food() {
struct coord food;
srand((unsigned)time(NULL));
food.coordX = (rand() % 10) + 1;
food.coordY = (rand() % 10) + 1;
return food;
}
//放置食物
void putfood() {
struct coord food1 = food();
int i = food1.coordX, j = food1.coordY;
if (map[i][j] == ' ') map[i][j] = '$';
else putfood();
}
//检验是否有食物存在
int testfood() {
for (int i = 1; i < 11; ++ i) {
for (int j = 1; j < 11; ++ j) {
if (map[i][j] == '$') return 0;
}
}
return 1;
}
//计算蛇长度的函数
int snakelength(struct coord snakecoord[]) {
for (int i = 1; i <= 100; ++i) {
if (snakecoord[i-1].coordX == 0) return i-1;
}
}
/*蛇的移动函数。这里有上下左右四种情况,使四种情况传递不同参数,寻找规律*/
struct coord snakemove(int m, int n, struct coord snakecoord[]) {
int newX, newY, tailX, tailY;
int length = snakelength(snakecoord);//获得蛇的长度
newX = snakecoord[0].coordX + m;//执行操作后的头部新位置
newY = snakecoord[0].coordY + n;
tailX = snakecoord[length-1].coordX;//尾部位置
tailY = snakecoord[length-1].coordY;
if (map[newX][newY] != ' ' && map[newX][newY] != '$') gameover();//若撞墙、撞到自己 则游戏结束
for (int i = length - 1; i > 0; -- i) {
snakecoord[i] = snakecoord[i-1];
map[snakecoord[i].coordX][snakecoord[i].coordY] = 'X';
}
if (map[newX][newY] == '$') {
snakecoord[length].coordX = tailX;
snakecoord[length].coordY = tailY;
map[tailX][tailY] = 'X';
}
else map[tailX][tailY] = ' ';
map[newX][newY] = 'H';
snakecoord[0].coordX = newX;
snakecoord[0].coordY = newY;
return snakecoord[100];
}
int main() {
//初始化蛇的初始位置
struct coord snakecoord[100];
for (int i = 0; i < 5; ++ i) {
snakecoord[i].coordX = 1;
snakecoord[i].coordY = 5 - i;
}
for (int i = 5; i < 100; ++ i) {
snakecoord[i].coordX = snakecoord[i].coordY = 0;
}
while (1) {
if (testfood()) putfood();
printMap();
char direction = getchar();
switch (direction) {
case 'A': snakecoord[100] = snakemove(left, snakecoord); break;
case 'S': snakecoord[100] = snakemove(down, snakecoord); break;
case 'D': snakecoord[100] = snakemove(right, snakecoord); break;
case 'W': snakecoord[100] = snakemove(up, snakecoord); break;
}
}
}