贪吃蛇游戏的功能结构

  • 上面大体是贪吃蛇的整体构架,我们按照这个思想往下写代码
    宏定义

  • 定义上下左右四个方向

#define U 1
#define D 2
#define L 3
#define R 4

定义全局变量

  • 包括结构体,以及其他各个函数需要用到的变量
typedef struct snake  //蛇身节点
{ int x;  //节点的x坐标int y;  //节点的y坐标struct snake *next;  //蛇身的下一个节点
}snake;int score = 0, add = 10;  //总得分和每一次吃食物得分
int HighScore = 0;  //最高分
int status, sleeptime = 200;  //蛇前进状态,每次运行的时间间隔
snake *head, *food;  //蛇头指针,食物指针
snake *q;  //遍历蛇时候用的指针
int endgamestatus = 0;  //游戏结束的情况,1:撞到墙,2:咬到自己,3:主动退出游戏
HANDLE hOut;  //控制台句柄

函数声明

  • 游戏中所有用到的函数
void gotoxy(int x, int y); //设置光标位置
int color(int c);//更改文字颜色
void printsnake();//字符画——蛇
void welcometogame();//开始画面
void createMap();//绘制地图
void scoreandtips();//游戏界面右侧的得分和小提示
void initsnake();//初始化蛇身,画蛇身
void createfood();//创建并随机出现食物
int biteself();//判断是否咬到自己
void cantcrosswall();//设置蛇撞墙的情况
void speedup();//加速
void speeddown();//减速
void snakemove();//控制蛇前进方向
void keyboardControl();//控制键盘按键
void Lostdraw();//游戏结束界面
void endgame();//游戏结束
void choose();//游戏失败之后的选择
void File_out();//在文件中读取最高分
void File_in();//存储最高分文件
void explation();//游戏说明

以上代码在头文件中定义

下面的我就介绍各个不同的函数

  • 定义color函数
int color(int c)  //改变颜色
{SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), c);return 0;
}
  • 定义gotoxy()函数
void gotoxy(int x, int y)  //设置光标位置
{COORD c;c.X = x;c.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_ERROR_HANDLE), c);
}
  • 绘制字符蛇的形状(大家可以根据个人的喜好,自行搭配颜色)
void printsnake()
{gotoxy(35, 1);color(6);printf("/^\\/^\\");gotoxy(34, 2);printf("|_| O|");gotoxy(33, 2);color(2); printf("_");gotoxy(25, 3);color(12);printf("\\/");gotoxy(31, 3);color(2);printf("/");gotoxy(37, 3);color(6);printf("\\_/");gotoxy(41, 3);color(10);printf("\\");gotoxy(26, 4);color(12);printf("\\____");gotoxy(32, 4);printf("__________/");gotoxy(31, 4);color(2);printf("|");gotoxy(43, 4);color(10);printf("\\");gotoxy(32, 5);color(2);printf("\\_____");gotoxy(44, 5);color(10);printf("\\");gotoxy(39, 6);printf("|    |             \\");gotoxy(38, 7);printf("/   /                \\");gotoxy(37, 8);printf("/  /                \\ \\");gotoxy(35, 9);printf("/  /                    \\ \\");gotoxy(34, 10);printf("/  /                        \\ \\");gotoxy(33, 11);printf("/  /          __---__     \\  \\");gotoxy(32, 12);printf("/  /        _-~     ~-_    \\  \\");gotoxy(31, 13);printf("             _-~    _--_  ~-_   -/  \ ");gotoxy(32, 14);printf("\\       ~-____-~   _-~    ~-_    ~-_-~    \\  \\");gotoxy(33, 15);printf("~-_         _-~    ~-_     _-~");gotoxy(35, 16);printf("~--______-~         ~-___-~");
}

  • 啊哈,我画的蛇比较难看,美术不好,呜呜呜,大家可以自己搭配,做一条好看的蛇
  • 绘制菜单选项
void welcometogame()
{int n;int i, j = 1;gotoxy(43, 18);color(11);printf("贪吃蛇游戏");color(14);for (i = 20; i < 26; i++){for (j = 27; j <= 74; j++){gotoxy(j, i);if (i == 20 || i == 26){printf("-");}else if (j == 27 || j == 74){printf("|");}}}color(12);gotoxy(35, 22);printf("1.开始游戏");gotoxy(55, 22);printf("2.游戏说明");gotoxy(35, 24);printf("3.退出游戏");gotoxy(29, 27);color(3);printf("请选择【1 2 3】:[ ]\b\b");color(14);scanf("%d", &n);switch (n){case 1:system("cls");createMap();initsnake();createfood();keyboardControl();break;case 2:explation();break;case 3:exit(0);break;default:color(12);gotoxy(40, 28);printf("请输入1--3之间的数");_getch();system("cls");printsnake();welcometogame();}
}

  • 创建游戏地图
void createMap()
{int i, j;for (i = 0; i < 58; i += 2){gotoxy(i, 0);color(5);printf("□");gotoxy(i, 26);printf("□");}for (i = 1; i < 26; i++){gotoxy(0, i);printf("□");gotoxy(56, i);printf("□");}for (i = 2; i < 56; i+=2){for (j = 1; j < 26; j++){gotoxy(i, j);color(3);printf("■\n\n");}}
}
  • 绘制右侧得分和小提示
void scoreandtips()
{File_out();gotoxy(64,4);color(11);printf("☆最高记录:%d", HighScore);gotoxy(64, 8);color(14);printf("得分:%d ", score);color(13);gotoxy(73, 11);printf("小 提 示");gotoxy(60, 13);color(6);printf("---------------------");gotoxy(60, 25);printf("---------------------");color(3);gotoxy(64, 14);printf("每个食物得分:%d分", add);gotoxy(64, 16);printf("不能穿墙,不能咬到自己");gotoxy(64, 18);printf("用↑↓←→分别控制蛇的移动");gotoxy(64, 20);printf("F1 为加速,F2 为减速");gotoxy(64, 22);printf("space:暂停游戏");gotoxy(64, 24);printf("ESC:退出游戏");
}
  • 读取游戏最高分
  • 我将游戏最高分放在文件中,取出来也是从文件中取出来
void File_out()
{FILE *fp;fp = fopen("E:\\vs 项目\贪吃蛇\Debug\save.txt","a+");fscanf(fp, "%d", &HighScore);fclose(fp);
}
  • 绘制蛇身
void initsnake()
{snake *tail;int i;tail = (snake*)malloc(sizeof(snake));tail->x = 24;tail->y = 5;tail->next = NULL;for (int i = 1; i < 4; i++){head = (snake*)malloc(sizeof(snake));head->next = tail;head->x = 24 + 2 * i;head->y = 5;tail = head;}while (tail != NULL){gotoxy(tail->x, tail->y);color(14);printf("★");tail = tail->next;}
}
  • 创建并随机出现食物
void createfood()
{snake* food_1;srand((unsigned)time(NULL));food_1 = (snake*)malloc(sizeof(snake));while ((food_1->x % 2) != 0){food_1->x = rand() % 52 + 2;}food_1->y = rand() % 24 + 1;q = head;while (q->next == NULL){if (q->x == food_1->x&&q->y == food_1->y){free(food_1);createfood();}q = q->next;}gotoxy(food_1->x, food_1->y);food = food_1;color(12);printf("●");
}
  • 判断蛇是否咬到自己
int biteself()
{snake* self;self = head->next;while (self != NULL){if (self->x == head->x&&self->y == head->y){return 1;}self = self->next;}return 0;
}
  • 判断蛇是否撞到墙
void cantcrosswall()
{if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26){endgamestatus = 1;endgame();}
}
  • 设置蛇加速前进
void speedup()
{if (sleeptime >= 50){sleeptime = sleeptime - 10;add = add + 2;if (sleeptime == 320){add = 2;}}
}
  • 设置蛇减速前进
void speeddown()
{if (sleeptime < 350){sleeptime = sleeptime + 30;add = add - 2;if (sleeptime == 350){add = 1;}}
}
  • 设置蛇在不按键的时候蛇的前进方向
void snakemove()
{snake *nexthead;cantcrosswall();nexthead = (snake*)malloc(sizeof(snake));if (status == U){nexthead->x = head->x;nexthead->y = head->y - 1;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (status == D){nexthead->x = head->x;nexthead->y = head->y + 1;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (status == L){nexthead->x = head->x - 2;nexthead->y = head->y;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (status == R){nexthead->x = head->x + 2;nexthead->y = head->y;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (biteself() == 1){endgamestatus = 2;endgame();}
}
  • 通过键盘方向键控制蛇前进的方向
void keyboardControl()
{status = R;while (1){scoreandtips();if (GetAsyncKeyState(VK_UP) && status != D){status = U;}else if (GetAsyncKeyState(VK_DOWN) && status != U){status = D;}else if (GetAsyncKeyState(VK_LEFT) && status != R){status = L;}else if (GetAsyncKeyState(VK_RIGHT) && status != L){status = R;}if (GetAsyncKeyState(VK_SPACE)){while (1){Sleep(300);if (GetAsyncKeyState(VK_SPACE)){break;}}}else if (GetAsyncKeyState(VK_ESCAPE)){endgamestatus = 3;break;}else if (GetAsyncKeyState(VK_F1)){speedup();}else if (GetAsyncKeyState(VK_F2)){if (sleeptime < 350){sleeptime = sleeptime + 30;add = add - 2;if (sleeptime == 350){add = 1;}}}Sleep(sleeptime);snakemove();}
}
  • 游戏失败界面设计
void Lostdraw()
{system("cls");int i, j;gotoxy(45, 2);color(6);printf("\\\\\\|///");gotoxy(43, 3);printf("\\\\");gotoxy(47, 3);color(15);printf(".-.-");gotoxy(54, 3);color(6);printf("//");gotoxy(44, 4);color(14);printf("(");gotoxy(47, 4);color(15);printf(".@.@");gotoxy(54, 4);color(14);printf(")");gotoxy(17, 5);color(11);printf("+-----------------");gotoxy(35, 5);color(14);printf("oOOo");gotoxy(39, 5);color(11);printf("-------");gotoxy(48, 5);color(14);printf("(__)");gotoxy(51, 5);color(11);printf("--------");gotoxy(61, 5);color(14);printf("oOOo");gotoxy(65, 5);color(11);printf("--------");for (i = 6; i <= 19; i++){gotoxy(17, i);printf("|");gotoxy(82, i);printf("|");}gotoxy(17, 20);printf("+-----------------");gotoxy(52, 20);color(14);printf("☆☆☆  ");gotoxy(60, 20);color(11);printf("-----------------+");
}
  • 打印游戏结束界面的信息
void endgame()
{system("cls");if (endgamestatus == 1){Lostdraw();gotoxy(35, 9);color(12);printf("对不起,您撞到墙了。游戏结束!");}else if (endgamestatus == 2){Lostdraw();gotoxy(35, 9);color(12);printf("对不起,您咬到自己了。游戏结束!");}else if (endgamestatus == 3){Lostdraw();gotoxy(40, 9);color(12);printf("您已经结束了游戏!");}gotoxy(43, 12);color(13);printf("您的得分是 %d", score);if (score >= HighScore){color(10);gotoxy(33, 16);printf("创造新记录了!最高分被你刷新了,真棒!!!");File_in();}else{color(10);gotoxy(33, 16);printf("继续努力吧~~~~你离最高分还差: %d", HighScore - score);}choose();
}
  • 存储游戏的最高分
void File_in()
{FILE *fp;fp = fopen("E:\\vs 项目\贪吃蛇\Debug\save.txt", "w+");fprintf(fp, "%d", score);fclose(fp);
}
  • 设计分支选项
void choose()
{int n;gotoxy(25, 23);color(12);printf("我要重新玩一局------ 1");gotoxy(52, 23);printf("不玩了,退出吧------ 2");gotoxy(46, 25);color(11);printf("选择:");scanf("%d", &n);switch (n){case 1:system("cls");score = 0;sleeptime = 200;add = 10;printsnake();welcometogame();break;case 2:exit(0);break;default:gotoxy(35, 27);color(12);printf("XXX您的输入有误,请重新输入XXX");system("pause>nul");endgame();choose();break;}
}
  • 游戏说明模块的实现
void explation()
{int i, j = 1;system("cls");color(13);gotoxy(44, 3);printf("游戏说明");color(2);for (i = 6; i <= 22; i++){for (j = 20; j <= 75; j++){gotoxy(j, i);if (i == 6 || i == 22)printf("=");else if (j == 20 || j == 75)printf("||");}}color(3);gotoxy(30, 8);printf("tip1:不能穿墙,不能咬到自己");color(10);gotoxy(30, 11);printf("tip2:用↑.↓.←.→分别控制蛇的移动");color(14);gotoxy(30, 14);printf("tip3:F1为加速,F2为减速");color(11);gotoxy(30, 17);printf("tip4:按空格键暂停游戏,再按空格键继续");color(4);gotoxy(30, 20);printf("tip5:ESP:退出游戏, space : 暂停游戏");_getch();system("cls");printsnake();welcometogame();
}

好了,所有函数已经完全实现

  • 看看主函数
int main()
{system("mode con cols=100 lines=30");printsnake();welcometogame();File_out();keyboardControl();endgame();system("pause");
}
  • 我做了一个系统的流程图,帮助大家理解

    最后附上程序的源代码
  • Snack.h
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>#define U 1
#define D 2
#define L 3
#define R 4typedef struct snake  //蛇身节点
{ int x;  //节点的x坐标int y;  //节点的y坐标struct snake *next;  //蛇身的下一个节点
}snake;int score = 0, add = 10;  //总得分和每一次吃食物得分
int HighScore = 0;  //最高分
int status, sleeptime = 200;  //蛇前进状态,每次运行的时间间隔
snake *head, *food;  //蛇头指针,食物指针
snake *q;  //遍历蛇时候用的指针
int endgamestatus = 0;  //游戏结束的情况,1:撞到墙,2:咬到自己,3:主动退出游戏
HANDLE hOut;  //控制台句柄void gotoxy(int x, int y); //设置光标位置
int color(int c);//更改文字颜色
void printsnake();//字符画——蛇
void welcometogame();//开始画面
void createMap();//绘制地图
void scoreandtips();//游戏界面右侧的得分和小提示
void initsnake();//初始化蛇身,画蛇身
void createfood();//创建并随机出现食物
int biteself();//判断是否咬到自己
void cantcrosswall();//设置蛇撞墙的情况
void speedup();//加速
void speeddown();//减速
void snakemove();//控制蛇前进方向
void keyboardControl();//控制键盘按键
void Lostdraw();//游戏结束界面
void endgame();//游戏结束
void choose();//游戏失败之后的选择
void File_out();//在文件中读取最高分
void File_in();//存储最高分文件
void explation();//游戏说明
  • Snack.c
#define _CRT_SECURE_NO_WARNINGS 1#include"snake.h"int color(int c)  //改变颜色
{SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), c);return 0;
}void gotoxy(int x, int y)  //设置光标位置
{COORD c;c.X = x;c.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_ERROR_HANDLE), c);
}void printsnake()
{gotoxy(35, 1);color(6);printf("/^\\/^\\");gotoxy(34, 2);printf("|_| O|");gotoxy(33, 2);color(2); printf("_");gotoxy(25, 3);color(12);printf("\\/");gotoxy(31, 3);color(2);printf("/");gotoxy(37, 3);color(6);printf("\\_/");gotoxy(41, 3);color(10);printf("\\");gotoxy(26, 4);color(12);printf("\\____");gotoxy(32, 4);printf("__________/");gotoxy(31, 4);color(2);printf("|");gotoxy(43, 4);color(10);printf("\\");gotoxy(32, 5);color(2);printf("\\_____");gotoxy(44, 5);color(10);printf("\\");gotoxy(39, 6);printf("|    |             \\");gotoxy(38, 7);printf("/   /                \\");gotoxy(37, 8);printf("/  /                \\ \\");gotoxy(35, 9);printf("/  /                    \\ \\");gotoxy(34, 10);printf("/  /                        \\ \\");gotoxy(33, 11);printf("/  /          __---__     \\  \\");gotoxy(32, 12);printf("/  /        _-~     ~-_    \\  \\");gotoxy(31, 13);printf("             _-~    _--_  ~-_   -/  \ ");gotoxy(32, 14);printf("\\       ~-____-~   _-~    ~-_    ~-_-~    \\  \\");gotoxy(33, 15);printf("~-_         _-~    ~-_     _-~");gotoxy(35, 16);printf("~--______-~         ~-___-~");
}void createMap()
{int i, j;for (i = 0; i < 58; i += 2){gotoxy(i, 0);color(5);printf("□");gotoxy(i, 26);printf("□");}for (i = 1; i < 26; i++){gotoxy(0, i);printf("□");gotoxy(56, i);printf("□");}for (i = 2; i < 56; i+=2){for (j = 1; j < 26; j++){gotoxy(i, j);color(3);printf("■\n\n");}}
}void scoreandtips()
{File_out();gotoxy(64,4);color(11);printf("☆最高记录:%d", HighScore);gotoxy(64, 8);color(14);printf("得分:%d ", score);color(13);gotoxy(73, 11);printf("小 提 示");gotoxy(60, 13);color(6);printf("---------------------");gotoxy(60, 25);printf("---------------------");color(3);gotoxy(64, 14);printf("每个食物得分:%d分", add);gotoxy(64, 16);printf("不能穿墙,不能咬到自己");gotoxy(64, 18);printf("用↑↓←→分别控制蛇的移动");gotoxy(64, 20);printf("F1 为加速,F2 为减速");gotoxy(64, 22);printf("space:暂停游戏");gotoxy(64, 24);printf("ESC:退出游戏");
}void File_out()
{FILE *fp;fp = fopen("E:\\vs 项目\贪吃蛇\Debug\save.txt","a+");fscanf(fp, "%d", &HighScore);fclose(fp);
}void initsnake()
{snake *tail;int i;tail = (snake*)malloc(sizeof(snake));tail->x = 24;tail->y = 5;tail->next = NULL;for (int i = 1; i < 4; i++){head = (snake*)malloc(sizeof(snake));head->next = tail;head->x = 24 + 2 * i;head->y = 5;tail = head;}while (tail != NULL){gotoxy(tail->x, tail->y);color(14);printf("★");tail = tail->next;}
}void createfood()
{snake* food_1;srand((unsigned)time(NULL));food_1 = (snake*)malloc(sizeof(snake));while ((food_1->x % 2) != 0){food_1->x = rand() % 52 + 2;}food_1->y = rand() % 24 + 1;q = head;while (q->next == NULL){if (q->x == food_1->x&&q->y == food_1->y){free(food_1);createfood();}q = q->next;}gotoxy(food_1->x, food_1->y);food = food_1;color(12);printf("●");
}int biteself()
{snake* self;self = head->next;while (self != NULL){if (self->x == head->x&&self->y == head->y){return 1;}self = self->next;}return 0;
}void cantcrosswall()
{if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26){endgamestatus = 1;endgame();}
}void speedup()
{if (sleeptime >= 50){sleeptime = sleeptime - 10;add = add + 2;if (sleeptime == 320){add = 2;}}
}void speeddown()
{if (sleeptime < 350){sleeptime = sleeptime + 30;add = add - 2;if (sleeptime == 350){add = 1;}}
}void snakemove()
{snake *nexthead;cantcrosswall();nexthead = (snake*)malloc(sizeof(snake));if (status == U){nexthead->x = head->x;nexthead->y = head->y - 1;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (status == D){nexthead->x = head->x;nexthead->y = head->y + 1;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (status == L){nexthead->x = head->x - 2;nexthead->y = head->y;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (status == R){nexthead->x = head->x + 2;nexthead->y = head->y;nexthead->next = head;head = nexthead;q = head;if (nexthead->x == food->x&&nexthead->y == food->y){while (q != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}score = score + add;speedup();createfood();}else{while (q->next->next != NULL){gotoxy(q->x, q->y);color(14);printf("★");q = q->next;}gotoxy(q->next->x, q->next->y);color(3);printf("■");free(q->next);q->next = NULL;}}if (biteself() == 1){endgamestatus = 2;endgame();}
}void keyboardControl()
{status = R;while (1){scoreandtips();if (GetAsyncKeyState(VK_UP) && status != D){status = U;}else if (GetAsyncKeyState(VK_DOWN) && status != U){status = D;}else if (GetAsyncKeyState(VK_LEFT) && status != R){status = L;}else if (GetAsyncKeyState(VK_RIGHT) && status != L){status = R;}if (GetAsyncKeyState(VK_SPACE)){while (1){Sleep(300);if (GetAsyncKeyState(VK_SPACE)){break;}}}else if (GetAsyncKeyState(VK_ESCAPE)){endgamestatus = 3;break;}else if (GetAsyncKeyState(VK_F1)){speedup();}else if (GetAsyncKeyState(VK_F2)){if (sleeptime < 350){sleeptime = sleeptime + 30;add = add - 2;if (sleeptime == 350){add = 1;}}}Sleep(sleeptime);snakemove();}
}void Lostdraw()
{system("cls");int i, j;gotoxy(45, 2);color(6);printf("\\\\\\|///");gotoxy(43, 3);printf("\\\\");gotoxy(47, 3);color(15);printf(".-.-");gotoxy(54, 3);color(6);printf("//");gotoxy(44, 4);color(14);printf("(");gotoxy(47, 4);color(15);printf(".@.@");gotoxy(54, 4);color(14);printf(")");gotoxy(17, 5);color(11);printf("+-----------------");gotoxy(35, 5);color(14);printf("oOOo");gotoxy(39, 5);color(11);printf("-------");gotoxy(48, 5);color(14);printf("(__)");gotoxy(51, 5);color(11);printf("--------");gotoxy(61, 5);color(14);printf("oOOo");gotoxy(65, 5);color(11);printf("--------");for (i = 6; i <= 19; i++){gotoxy(17, i);printf("|");gotoxy(82, i);printf("|");}gotoxy(17, 20);printf("+-----------------");gotoxy(52, 20);color(14);printf("☆☆☆  ");gotoxy(60, 20);color(11);printf("-----------------+");
}void endgame()
{system("cls");if (endgamestatus == 1){Lostdraw();gotoxy(35, 9);color(12);printf("对不起,您撞到墙了。游戏结束!");}else if (endgamestatus == 2){Lostdraw();gotoxy(35, 9);color(12);printf("对不起,您咬到自己了。游戏结束!");}else if (endgamestatus == 3){Lostdraw();gotoxy(40, 9);color(12);printf("您已经结束了游戏!");}gotoxy(43, 12);color(13);printf("您的得分是 %d", score);if (score >= HighScore){color(10);gotoxy(33, 16);printf("创造新记录了!最高分被你刷新了,真棒!!!");File_in();}else{color(10);gotoxy(33, 16);printf("继续努力吧~~~~你离最高分还差: %d", HighScore - score);}choose();
}void File_in()
{FILE *fp;fp = fopen("E:\\vs 项目\贪吃蛇\Debug\save.txt", "w+");fprintf(fp, "%d", score);fclose(fp);
}void choose()
{int n;gotoxy(25, 23);color(12);printf("我要重新玩一局------ 1");gotoxy(52, 23);printf("不玩了,退出吧------ 2");gotoxy(46, 25);color(11);printf("选择:");scanf("%d", &n);switch (n){case 1:system("cls");score = 0;sleeptime = 200;add = 10;printsnake();welcometogame();break;case 2:exit(0);break;default:gotoxy(35, 27);color(12);printf("XXX您的输入有误,请重新输入XXX");system("pause>nul");endgame();choose();break;}
}void explation()
{int i, j = 1;system("cls");color(13);gotoxy(44, 3);printf("游戏说明");color(2);for (i = 6; i <= 22; i++){for (j = 20; j <= 75; j++){gotoxy(j, i);if (i == 6 || i == 22)printf("=");else if (j == 20 || j == 75)printf("||");}}color(3);gotoxy(30, 8);printf("tip1:不能穿墙,不能咬到自己");color(10);gotoxy(30, 11);printf("tip2:用↑.↓.←.→分别控制蛇的移动");color(14);gotoxy(30, 14);printf("tip3:F1为加速,F2为减速");color(11);gotoxy(30, 17);printf("tip4:按空格键暂停游戏,再按空格键继续");color(4);gotoxy(30, 20);printf("tip5:ESP:退出游戏, space : 暂停游戏");_getch();system("cls");printsnake();welcometogame();
}
  • test.c
#define _CRT_SECURE_NO_WARNINGS 1void welcometogame()
{int n;int i, j = 1;gotoxy(43, 18);color(11);printf("贪吃蛇游戏");color(14);for (i = 20; i < 26; i++){for (j = 27; j <= 74; j++){gotoxy(j, i);if (i == 20 || i == 26){printf("-");}else if (j == 27 || j == 74){printf("|");}}}color(12);gotoxy(35, 22);printf("1.开始游戏");gotoxy(55, 22);printf("2.游戏说明");gotoxy(35, 24);printf("3.退出游戏");gotoxy(29, 27);color(3);printf("请选择【1 2 3】:[ ]\b\b");color(14);scanf("%d", &n);switch (n){case 1:system("cls");createMap();initsnake();createfood();keyboardControl();break;case 2:explation();break;case 3:exit(0);break;default:color(12);gotoxy(40, 28);printf("请输入1--3之间的数");_getch();system("cls");printsnake();welcometogame();}
}int main()
{system("mode con cols=100 lines=30");printsnake();welcometogame();File_out();keyboardControl();endgame();system("pause");
}
  • 给大家来三张游戏的图。


最后,所有的游戏代码已经完成,如果有错,欢迎留言讨论

贪吃蛇小游戏(C语言)相关推荐

  1. 控制台版贪吃蛇小游戏 C语言

    写在前面 最近我们C语言的课设快开始了,开始前刚好有时间就写了一下C语言的贪吃蛇小游戏(单链表实现),包含了经典模式和无边界模式 ,网上查了设置颜色 和 改变光标位置 还有 用方向键控制 的函数,第一 ...

  2. c语言小游戏 精简_一个简易的贪吃蛇小游戏C语言源码

    /* *程序名称:贪吃蛇v2.1 *程序描述:一个简易的贪吃蛇小游戏 *版本信息:v2.1 *v1.1版本更新:1:加入菜单选择项 *v1.2版本更新:1:修复菜单选择bug *v1.3班本更新:1: ...

  3. 贪吃蛇小游戏——C语言

    效果演示 Snach.c文件 #include <stdlib.h> #include <stdlib.h> #include <stdio.h> #include ...

  4. 基于C语言Ncurse库和链表的简单贪吃蛇小游戏

    参考:基于C语言Ncurse库和链表的简单贪吃蛇小游戏 作者:三速何时sub20 发布时间:2020-09-29 10:23:51 网址:https://blog.csdn.net/weixin_44 ...

  5. c语言贪吃蛇打包到桌面,C语言实现桌面贪吃蛇小游戏

    本篇写的是桌面贪吃蛇小游戏,大家自己看吧,感谢大家的支持,谢谢!O(∩_∩)O~~ #define _CRT_SECURE_NO_WARNINGS #include #include #include ...

  6. C语言实现贪吃蛇小游戏1.0

    C语言实现贪吃蛇小游戏1.0 贪吃蛇游戏要有三个东西:边框.蛇.食物 还有两个灵魂的东西:光标的移动与按键监控 一.光标的移动 在我看来在控制台上移动光标画图是实现这个小游戏的灵魂了,在这之前我一直以 ...

  7. C语言小游戏大全,C语言贪吃蛇小游戏(附源码)

    一.C语言小游戏大全,C语言贪吃蛇小游戏(附源码) 贪吃蛇小游戏源码和更多C语言课设项目小游戏源码免 费 下 载 链 接 如下: c语言项目课设小游戏源码资料压缩包.zip-C文档类资源-CSDN下载 ...

  8. 安卓c语言自动补全软件吾爱,C语言实现贪吃蛇小游戏

    本文实例为大家分享了C语言实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 一.程序实现的原理: 1.构造蛇身:定义一个坐标数组,存放的是蛇的每一节蛇身所在的坐标位置.这样就将移动蛇身的操作转换为 ...

  9. 【笔记】用vs2017 c语言写 贪吃蛇 小游戏

    目录 文章目录 前言 一.贪吃蛇游戏 设计逻辑 二.详细介绍 1.设置界面(界面大小,背景颜色) 2.设置贪吃蛇的身体并显示 3.使之能够移动并能够穿墙 4.添加食物并让蛇吃掉 5.设置背景音乐 完整 ...

  10. 再来一次的C语言贪吃蛇小游戏(三)

    8.游戏的不同界面 为了便于实现主要功能,之前我们所有的状态控制都是放在游戏中,但实际上我们应该把这些状态控制抽离出来,通过菜单来控制,以便在不同游戏界面间切换. 菜单界面 游戏界面 排行榜 游戏结束 ...

最新文章

  1. 【CVPR2020】目标检测方向论文更新
  2. 构建局域网的病毒防护体系
  3. activemq安全机制
  4. 别以为程序员的工作就是写代码
  5. 稳定性测试怎么测_心理测试:选择你喜爱的一种食物,测你2020年的运势怎么样...
  6. 年赚133亿!中国平安旗下陆金所向SEC递交招股书
  7. 罗翔 ---- 落俗不可避免,浪漫至死不渝
  8. 解决vs2005中文乱码问题
  9. 记录一下ListItem类的常用的方法
  10. Day002 20210207
  11. java网上零食销售网站系统
  12. gn: toolchain
  13. 行列式java_n阶行列式的全排列求解(Java)
  14. OSI 参考模型介绍
  15. 4.4 day14 内置函数
  16. win10计算机如何调到桌面上,win10系统下怎么将计算器放在桌面上
  17. Masimo宣布首款用于 Root(R)患者监护与互联总机的第三方Masimo Open Connect(R)模块获得CE标记
  18. Spring-工作原理
  19. Python爬虫日记2——使用requests
  20. 阿里云ECS服务器+WordPress快速搭建个人博客

热门文章

  1. scala spark 数据对比_Spark 实践——用 Scala 和 Spark 进行数据分析
  2. 面试题:React实现鼠标托转文字绕原点旋转
  3. Java编程:弗洛伊德算法(无向图所有顶点最小路径)
  4. 电子邮件是html文件吗,如何在电子邮件正文中嵌入HTML文件
  5. Oracle 11g ora 15018,OracleASM错误之--ORA-15031、ORA-15014
  6. java的if判读_java if判断
  7. OpenGL学习笔记_图形渲染管线及典型渲染流程(绘制一个三角形)
  8. OpenCV_(Laplacian Transform to find the edges)图像拉普拉斯变换查找边缘 图像识别
  9. 重磅!中国科协发布2020重大科学问题和工程技术难题
  10. 基于keras和tensorflow的yolo3物体检测