游戏效果
贪吃蛇吃食物,每吃一个得一分,撞到墙或者撞到自己死。
游戏代码
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int maxn;
void BackGround(unsigned int ForeColor = 7, unsigned int BackGroundColor = 0) {HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
}
void gotoxy(int x, int y) {HANDLE handle;COORD coord; //获取坐标轴结构体coord.X = x;coord.Y = y;handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄,值为-11SetConsoleCursorPosition(handle, coord); //移动光标到x,y处
}
//**初始化地图数据**//
void restart(int bk[50][50], int look[4], int move[50][50]) {//bk为总的地图数据记录整个地图,为1时表示墙体,为2时表示果实,为3时表示蛇//look记录数据,为0时表示朝向,为1时表示长度,为3时表示胜负情况,为4表示分数//move记录蛇走过的路程,用来打印蛇时判断用int pp, qq; //用来记录获取的随机坐标//接下来要初始化整个地图for (int i = 0; i<=maxn;i++) {for (int j = 0; j <= maxn; j++) {if (i == 0 || i == maxn || j == 0 || j == maxn) bk[i][j] = 1;//1表示墙体else bk[i][j] = 0; //0表示什么都没有move[i][j] = 0; //该数组用来记录蛇移动的轨迹}}//将蛇初始化在8,8坐标上bk[8][8] = 3;move[8][8] = 1;//则此时8,8,上的轨迹就应该记录为1move[0][0] = 1;//用此来记录步数pp = rand() % (maxn - 1) + 1;//范围是1-(maxn - 1)qq = rand() % (maxn - 1) + 1;bk[pp][qq] = 2;//表示这个位置有果实了look[0] = 1;//表示朝向,向上look[1] = 1;//表示长度look[2] = 0;//当为1是表示失败look[3] = 0;//记录得分//接下来要绘制地图for(int i = 0; i <= maxn; i++) {for (int j = 0; j <= maxn; j++) {gotoxy(i * 2, j);//光标移动,每个光标都是矩形switch (bk[i][j]) {case 0:BackGround(0, 0);break;//如果没有东西打印黑色case 1:BackGround(0, 1);break;//墙打印蓝色case 2:BackGround(0, 2);break;//果实打印绿色case 3:BackGround(0, 3);break;//蛇打印湖蓝色default:break;}printf(" ");//地图中直接就是涂空格符}}//接下来要显示积分//gotoxy(55, 0);BackGround(7, 0);//用白字黑底打印printf("现在得分是:%d,请再接再厉!^_^", look[2]);
}//运动主体
void map(int bk[50][50], int look[4], int xy[2], int move[50][50]) {//bk是地图信息,look作数据记录,xy记录坐标,move记录蛇的运动轨迹int b[10], qq = 0, pp = 0;//b用来吸收输入,qq和pp用来随机初始化果实坐标if (kbhit()) {//记录按下的是哪个方向键b[0] = getch();//用b来记录if (b[0] == 224) {b[0] = getch();//如果为224表示为方向键,但是要再一次获取才行}if (b[0] == 72 && look[0] != 2) {//如果输入的为上并且朝向不为下look[0] = 1;}if (b[0] == 80 && look[0] != 1) {look[0] = 2;}if (b[0] == 75 && look[0] != 4) {look[0] = 3;}if (b[0] == 77 && look[0] != 3) {look[0] = 4;}}switch (look[0]) {case 1://往上走xy[1]--;break;case 2://往下走xy[1]++;break;case 3://往左走xy[0]--;break;case 4://往右走xy[0]++;break;}//接下来蛇就开始走动了move[0][0]++;//蛇的步数加一move[xy[0]][xy[1]] = move[0][0];//记录当前格子中蛇的轨迹gotoxy(55, 2);BackGround(7, 0);printf("横坐标:%d,纵坐标:%d", xy[0],xy[1]);gotoxy(xy[0] * 2, xy[1]);//这里蛇头就往前移动了BackGround(0, 3);//与蛇体一个颜色printf(" ");//如果吃了果实if (bk[xy[0]][xy[1]] == 2) {look[2]++;//分数加一look[1]++;//长度加一//更新分数gotoxy(55, 0);BackGround(7, 0);printf("现在得分是:%d,请再接再厉!^_^", look[2]);while (bk[pp][qq] != 0) {pp = rand() % (maxn - 1) + 1;qq = rand() % (maxn - 1) + 1;}bk[pp][qq] = 2;//将这个地方变为果实gotoxy(pp * 2, qq);BackGround(0, 2);printf(" ");}//如果撞了墙或者自己//if (bk[xy[0]][xy[1]] == 1 || bk[xy[0]][xy[1]] == 3) {look[3] = 1;//表示已经输了gotoxy(6, 6);BackGround(7, 0);printf("最后得分:%d", look[2]);}bk[xy[0]][xy[1]] = 3;//使这个位置变成蛇//接下来要检测蛇然后刷新蛇的位置for (int i = 0; i <= maxn; i++) {for (int j = 0; j <= maxn; j++) {if (move[i][j] == move[xy[0]][xy[1]]- look[1]){//如果符合这个条件,则表示蛇已经移动出这个位置了//要删除这个位置的蛇尾巴//一次只有一个方块会符合要求吧?bk[i][j] = 0;gotoxy(i * 2, j);BackGround(0, 0);printf(" ");break;//一次只找一个}}}
}
int main() {printf ("请输入您想要的难度(1简单,2中等,3困难)\n");int c;scanf ("%d", &c);if (c == 1) {maxn = 24;} else if (c == 2) {maxn = 20;} else if (c == 3) {maxn = 16;} else {printf ("输入错误!\n"); main();}int bk[50][50], xy[2], move[50][50], look[4];xy[1] = xy[0] = maxn / 2;srand((unsigned) time(NULL));//初始化随机种子system("pause");restart(bk, look, move);while (look[3] == 0) {Sleep(200);//休眠400ms一次map(bk, look, xy, move);}system("pause");printf("游戏结束,感谢游玩!^_^");return 0;
}