作者:峰吹云飞_974 | 来源:互联网 | 2023-05-17 21:07
上周码了字符贪吃蛇,这周老师将作业提升了难度,编写智能蛇——自己移动,并能绕过障碍物。捣鼓了很久,终于搞出个能看的样子。决定蛇行走的方向的函数的伪代码Hx,Hy:
上周码了字符贪吃蛇,这周老师将作业提升了难度,编写智能蛇——自己移动,并能绕过障碍物。
捣鼓了很久,终于搞出个能看的样子。
决定蛇行走的方向的函数的伪代码
function whereGoNext(Hx,Hy,Fx,Fy) {
}
下面是代码
#include
#include
#include
#include
#include
#include
char snake_Head = 'H';
char snake_Body = 'X';
char food_Char = '$';
char wall_Char ='*';
char blank_Char = ' ';
#define max_Length 100
char map[12][13] = {
"************",
"*XXXXH *",
"* * *",
"* * *",
"* *",
"* ** *",
"* * *",
"* * *",
"* * *",
"* * *",
"* *",
"************"
};
int snake_Y[max_Length] = { 5, 4, 3, 2, 1 };
int snake_X[max_Length] = { 1, 1, 1, 1, 1 };
int headX = 1, headY = 5;
int tail_Index = 4;
int foodX = 0, foodY = 0;
int lengthen = 0;
int gameRunning = 1;
int distance[4] = { 0 };
void printMap()
{
system("cls");
for (int i = 0; i <12; i++){
printf("%s\n", map[i]);
}
}
void putFood()
{
while (map[foodX][foodY] != ' '){
foodX = rand() % 10 + 1;
foodY = rand() % 10 + 1;
}
map[foodX][foodY] = food_Char;
}
void gameOver()
{
printf("GAME OVER!!!\n");
}
int leastPath(int path[])
{
int temp, iii = 0;
temp = path[0];
for (int i = 1; i <4; i++){
if (temp>path[i]){
temp = path[i];
iii = i;
}
}
if (temp == 100)
gameRunning = 0;
return iii;
}
int dissatisfy(int col)
{
switch (col){
case 0:
if (map[headX][headY - 1] == wall_Char || map[headX][headY - 1] == snake_Body) return 1;
else return 0;
break;
case 1:
if (map[headX][headY + 1] == wall_Char || map[headX][headY + 1] == snake_Body) return 1;
else return 0;
break;
case 2:
if (map[headX - 1][headY] == wall_Char || map[headX - 1][headY] == snake_Body) return 1;
else return 0;
break;
case 3:
if (map[headX + 1][headY] == wall_Char || map[headX + 1][headY] == snake_Body) return 1;
else return 0;
break;
default:
break;
}
}
int whereToGo()
{
int i, count = 4;
distance[0] = abs(headY - 1 - foodY) + abs(headX - foodX);
distance[1] = abs(headY + 1 - foodY) + abs(headX - foodX);
distance[2] = abs(headX - 1 - foodX) + abs(headY - foodY);
distance[3] = abs(headX + 1 - foodX) + abs(headY - foodY);
while (count--){
i = leastPath(distance);
if (dissatisfy(i)){
distance[i] = 100;
i = leastPath(distance);
}
}
return i;
}
void moveSnake(int direction)
{
int tempX = headX, tempY = headY;
map[tempX][tempY] = snake_Body;
map[snake_X[tail_Index]][snake_Y[tail_Index]] = blank_Char;
switch (direction){
case 0:
headY--;
break;
case 1:
headY++;
break;
case 2:
headX--;
break;
case 3:
headX++;
break;
default:
break;
}
for (int i = tail_Index; i > 0; i--){
snake_X[i] = snake_X[i - 1];
snake_Y[i] = snake_Y[i - 1];
}
snake_X[0] = headX;
snake_Y[0] = headY;
map[snake_X[tail_Index]][snake_Y[tail_Index]] = snake_Body;
map[headX][headY] = snake_Head;
printMap();
if (headX == foodX&&headY == foodY){
tail_Index++;
putFood();
}
}
int main()
{
system("color F0");
srand(time(NULL));
putFood();
printMap();
int j;
while (gameRunning){
Sleep(200);
j = whereToGo();
moveSnake(j);
}
gameOver();
return 0;
}
能用所学的东西写一个简单的游戏,是值得一试的。尽管最后的程序还是有一些缺陷,尽管对于我这样的初学者来说,其实并不简单。
下面是效果图