【原创】只学到二维数组和结构体,不用链表也能写一个C贪食蛇?(四)

时间:2022-06-29 14:42:50

全系列Index:

  1. 【原创】只学到二维数组和结构体,不用链表也能写一个C贪食蛇?(一)
  2. 【原创】只学到二维数组和结构体,不用链表也能写一个C贪食蛇?(二)
  3. 【原创】只学到二维数组和结构体,不用链表也能写一个C贪食蛇?(三)
  4. 【原创】只学到二维数组和结构体,不用链表也能写一个C贪食蛇?(四)

由于用system("cls")重绘会有较大的闪烁,我google了网上GOTOXY的实现来完成帧的重绘。

全部程序的源代码:

/*
Powered by wmydx
Snake verson 2.0
2013.10.19
verson 1.0的遗留问题已经解决:标记蛇头的访问顺序,谁小跟谁
注意:用户摁键过快会产生BUG,已解决
*/
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define ROW 40
#define COL 20
struct SnakeTail
{
int x;
int y;
};
struct SnakeHead
{
int x;
int y;
int dis;
};
struct Food
{
int x;
int y;
};
struct Map
{
char a;
unsigned long long num;
};
struct Point
{
int x;
int y;
int flag;
};
SnakeTail myTail;
SnakeHead myHead;
Food myFood;
unsigned long long count = ;
Map map[COL][ROW];
int gameSpeed = ;
int winPoint = ;
int isDead = ;
int isPoint = ;
//以下是网上找来的GOTOXY实现
void GoToXY(int x, int y)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 SetConsoleCursorPosition(hOut, pos);
} void WhereXY(int *x, int *y)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
COORD pos = bInfo.dwCursorPosition;
*x = pos.X;
*y = pos.Y;
}
//以上时网上找来的GOTOXY实现
void plantFood()
{
int tmpx,tmpy;
srand((int)time());
tmpx = rand() % COL;
tmpy = rand() % ROW;
while(((tmpx <= && tmpx >= COL - ) || (tmpy <= && tmpy >= ROW - )) || map[tmpx][tmpy].a == '*')
{
tmpx = rand() % COL;
tmpy = rand() % ROW;
}
myFood.x = tmpx;
myFood.y = tmpy;
map[myFood.x][myFood.y].a = '$';
}
void initialization() //初始化游戏
{
int col = ;
for(int i = ;i < COL;i++)
for(int j = ;j < ROW;j++)
{
map[i][j].a = ' ';
map[i][j].num = ;
}
for(int i = ;i <= ;i++)
{
map[col][i].a = '*';
map[col][i].num = count ++;
}
myTail.x = ;
myTail.y = ;
myHead.x = ;
myHead.y = ;
myHead.dis = ;
plantFood();
}
void print()
{
printf(" ");
for(int i = ;i < ROW;i++)
printf("-");
printf("\n");
for(int i = ;i < COL;i++)
{
printf(" ");
for(int j = ;j < ROW;j++)
{
if(j == )printf("|");
printf("%c",map[i][j].a);
}
printf("|\n");
}
printf(" ");
for(int i = ;i < ROW;i++)
printf("-");
}
int ifDie(int x,int y)
{
if(x < || y < || x >= COL || y >= ROW || map[x][y].a == '*')return ;
else return ;
}
int ifGetPoint()
{
if(map[myHead.x][myHead.y].a == '$')
{
plantFood();
return ;
}
else return ;
}
void deleteTail()
{
Point a[];
Point temp;
int test = ;
unsigned long long min = 0xFFFFFFFF;
for(int i = ;i < ;i++)
a[i].flag = ;
if(map[myTail.x + ][myTail.y].a == '*')
{
a[].flag = ;
a[].x = myTail.x + ;
a[].y = myTail.y;
}
if(map[myTail.x - ][myTail.y].a == '*')
{
a[].flag = ;
a[].x = myTail.x - ;
a[].y = myTail.y;
}
if(map[myTail.x][myTail.y + ].a == '*')
{
a[].flag = ;
a[].x = myTail.x;
a[].y = myTail.y + ;
}
if(map[myTail.x][myTail.y - ].a == '*')
{
a[].flag = ;
a[].x = myTail.x;
a[].y = myTail.y - ;
}
for(int i = ;i < ;i++)
{
if(a[i].flag == )
{
test ++;
if(map[a[i].x][a[i].y].num < min)
{
min = map[a[i].x][a[i].y].num;
temp = a[i];
} }
}
map[myTail.x][myTail.y].a = ' ';
map[myTail.x][myTail.y].num = ;
myTail.x = temp.x;
myTail.y = temp.y;
}
void snakeMove()
{
switch(myHead.dis)
{
case : myHead.x --;
if(ifGetPoint())
{
winPoint ++;
isPoint = ;
}
if(!ifDie(myHead.x,myHead.y))
{
map[myHead.x][myHead.y].a = '*';
map[myHead.x][myHead.y].num = count ++;
}
else
{
isDead = ;
}
break;
case : myHead.x ++;
if(ifGetPoint())
{
winPoint ++;
isPoint = ;
}
if(!ifDie(myHead.x,myHead.y))
{
map[myHead.x][myHead.y].a = '*';
map[myHead.x][myHead.y].num = count ++;
}
else
{
isDead = ;
}
break;
case : myHead.y --;
if(ifGetPoint())
{
winPoint ++;
isPoint = ;
}
if(!ifDie(myHead.x,myHead.y))
{
map[myHead.x][myHead.y].a = '*';
map[myHead.x][myHead.y].num = count ++;
}
else
{
isDead = ;
}
break;
case : myHead.y ++;
if(ifGetPoint())
{
winPoint ++;
isPoint = ;
}
if(!ifDie(myHead.x,myHead.y))
{
map[myHead.x][myHead.y].a = '*';
map[myHead.x][myHead.y].num = count ++;
}
else
{
isDead = ;
}
break;
}
if(isPoint == )
{
deleteTail();
}
else isPoint = ;
}
void changeSnake(char ch)
{
if(ch == '.')return;
switch(ch)
{
case 'w' : if(myHead.dis != )myHead.dis = ;
break;
case 'a' : if(myHead.dis != )myHead.dis = ;
break;
case 's' : if(myHead.dis != )myHead.dis = ;
break;
case 'd' : if(myHead.dis != )myHead.dis = ;
break;
default : ;//printf("ERROR!"); BUG检测代码
}
}
int main(void)
{
char control = '.';
initialization();
int end = ;
while()
{
while(kbhit() == )
{
changeSnake(control); //这个函数要放入循环体!否则摁键过快,前次操作未进入循环体,后次操作又改变dis了
control = '.';
print();
snakeMove();
if(isDead == )
{
end = ;
break;
}
Sleep(gameSpeed);
GoToXY(,);
}
if(end)break;
control = getch();
}
printf("\n YOUR SCORE IS %d \n",winPoint);
system("pause");
return ;
}