Snake Basics·Framework Version (Simple) There is original code at the beginning

It’s not easy to update, so please give me likes, your questions are welcome, and thank you for forwarding,

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Um, I'm a little dizzy. I'll start with the framework version today and optimize it at night. This code is a bit too much and it jumps around. It has F1 and F2 to implement it. The effect is not good if I play it again. There are also some calls that bite me. question.

This is the first version of Snake. It will be upgraded later. I hope I can understand every step instead of copying the code.

Table of contents

Original code, for debugging only

First of all, this time my compiler is the following one (needs to install the package, please send a private message to Xiaomiao), to develop a good habit.

Window settings

mapping

Snake node creation

food creation

Keyboard event listening

snake movement

Edit

 Screen tips

 end page

Pinocchio

End page implementation and its sub-function implementation

start page

head File

global variables

function declaration

main function

(Water word count) Introduction to the game Snake

background settings

Operation guide

Game features

Game review


Original code, for debugging only

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>//要使用windows的api ,lrq 
//因为要用到随机数函数,所以要调用time.h文件
#include<time.h> 
#include<conio.h>//用来接收键盘的输入输出 
//
//预定义
#define  U 1
#define  D 2
#define  L 3
#define  R 4
//U上 D下 L左 R右 
/
//定义全局变量 
typedef struct snake
{
	int x;
	int y;
	struct snake* next;
}snake;

snake *head;//指向蛇头的指针
snake *food;//创建的食物 
snake *q;//用来遍历蛇的结点的 
 
int status;
 
int add=10; //每个食物的得分 
int sumScore=0;//总得分 
int highScore=0;//最高分 

//三种结束状态变量
//1,撞墙    2,自己咬到自己了    3,主动退出 
int endGameStatus=0; 
   
//函数声明
void createMap();
//坐标转换
void gotoXY(int a,int b); 
//颜色更换
int color(int x);
//创建蛇
void initSnake(); 
//创建食物
void createFood(); 
//侦听键盘事件
void keyboardControl(); 
//让蛇动起来
int snakeMove();
//撞墙停止
int cantCrossWall();
//自己咬到自己
int biteSelf();
//主页面上小提示
void screenTips();
//结束页面 
void endGame();
//匹诺曹绘制
void LostDraw();
void File_In(); 
void File_Out();
void choose();
void printsnake();
void welcometoGame();

/*
	字符拼成英文图案
*/
void printsnake()
{
	color(2);
	printf("                                                                                         \n");
	printf("                       __________       ___                                              \n");
	printf("                      /          \\     / \\ \\    |____      __\\__                     \n");
	printf("                     /  ________  \\   / ___ \\  _/ __     | |   /                       \n");
	printf("                     |  |      |__|     _/_   |_|  /    [|] |/                           \n");
	printf("                     |  |              | | |      /     _|_ \\__/                        \n");
	printf("                     \\  \\_______        / \\      |___/        ____                    \n");
	printf("                      \\         \\    ____ ____      ____   __ |  |  ___   ______       \n");
	printf("                       \\_______  \\   |  |/    \\    /    \\_/ / |  | /  /  /      \\   \n");
	printf("                               \\  \\  |    ___  \\  / ____   /  |  |/  /  /  ____  \\   \n");
	printf("                     __        |  |  |   /   \\  \\ | |  |  /   |     /  |  /____\\  |   \n");
	printf("                    \\  \\_______|  |  |  |    |  | | |__|  |   |     \\  |  ________/   \n");
	printf("                     \\            /  |  |    |  |  \\       \\  |  |\\  \\  \\  \\____  \n");
	printf("                      \\__________/   |__|    |__|   \\___/\\__\\ |__| \\__\\  \\______/ \n");

}
void welcometoGame()
{
	int n=0;
	int i=0;
	int j=0;
	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("|");
			}
		} 
	}
	gotoXY(36,22);
	color(10);
	printf("1.开始游戏");
	gotoXY(50,22);
	color(10);
	printf("2.游戏说明");
    gotoXY(36,24);
	color(10);
	printf("3.退出游戏");
    gotoXY(28,27);
    color(3);
    printf("请选择[1 2 3]:[ ]\b\b");//\b是退格使光标处于两个中括号之间
	color(14);
	scanf("%d",&n);
	switch(n)
	{
	case 1:
		system("cls");
		File_Out();
		sumScore=0;
		createMap();
		screenTips();
		initSnake();
		createFood();
		keyboardControl();
		break;
	case 2:
		//游戏说明界面 
		break;
	case 3:
		exit(0); 
		break;
	default:
		break;
		
	} 
			
}



void endGame()
{
	//清屏操作 
	system("cls");
	LostDraw();
	if(endGameStatus==1)
	{
		//TODO
		gotoXY(35,9);
		color(7);
		printf("对不起,你撞墙了!游戏结束"); 
	}else if(endGameStatus==2)
		{
			//TODO
			gotoXY(35,9);
			color(7);
			printf("对不起,你咬到自己了!游戏结束"); 
		}
	else if(endGameStatus==3)
			{
				//TODO
				gotoXY(35,9);
				color(7);
				printf("游戏结束!"); 
			} 
	gotoXY(43,12);
	printf("你的得分是:%d分",sumScore);
	//判断得分是否需要存储
	if(sumScore>=highScore)
	{
		color(10);
		gotoXY(33,16);
		printf("创纪录了,最高分被你刷新了!"); 
		//最高分存入文件当中 
		File_In();
	} else{
		gotoXY(33,16);
		printf("继续努力吧~你离最高分还差:%d",highScore-sumScore);
	} 
	//调用选择选项
	 choose(); 
	
}


void choose()
{
	int n=0;
	gotoXY(30,23);
	color(12);
	printf("重玩一局[1]");
	gotoXY(50,23);
	printf("溜了溜了[2]");
	gotoXY(43,25);
	printf("选择:");
	scanf("%d",&n);
	switch(n)
	{
		case 1:
			system("cls");
		    printsnake();
		    //开始界面
			 welcometoGame(); 
			break;
		case 2:
			exit(0);
			break;
		default:
			gotoXY(35,27);
			color(12);
			printf("你输入的有误,请重新输入");
			break; 
			
	} 
} 


//文件读写
void File_In()
{
	//定义文件指针 
	FILE *fp;
	//指针关联文件 
	fp=fopen("save.txt","w+");
	//通过文件指针向文件写入内容 
	fprintf(fp,"%d",sumScore);
	//关闭文件 
	fclose(fp);
}

void File_Out()
{
	FILE *pp;
	pp=fopen("save.txt","a+");
	fscanf(pp,"%d",&highScore);
	fclose(pp);
	
}


void LostDraw() {
	/*
		失败界面
	*/
	system("cls");
	int i;
    gotoXY(45, 1);
	color(6);
	printf(" |-----|   ");		//匹诺曹的帽子
	gotoXY(45, 2);
	color(6);
	printf(" |     |   ");
	gotoXY(43, 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(48, 6);
	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(60, 20);
	color(11);
	printf("----------------------+");						//下边框



}


void screenTips(){
	color(11);
	gotoXY(64,4);
	printf("☆最高记录是:%d☆",highScore);
	color(14);
	gotoXY(64,6);
	printf("当前得分:%d",sumScore);
	gotoXY(73,11);
	printf("小提示");
	
	gotoXY(60,13);
	color(6);
	printf("§--------------------§");
	
	gotoXY(60,26);
	color(6);
	printf("§--------------------§");
	
	gotoXY(64,14);
	color(3);
	printf("每个食物得分:%d分",add);
	
	gotoXY(64,16);
	printf("不能撞墙,不能咬到自己");

	gotoXY(64,18);
	printf("用上,下,左,右箭头分别控制方向");	
	
	gotoXY(64,20);
	printf("F1键加速,F2键减速");
	
	gotoXY(64,22);
	printf("空格键暂停游戏");
	
	gotoXY(64,24);
	printf("Esc键退出游戏");
	 
} 


int biteSelf()
{
	snake *self;//这里不涉及内存分配,它只是涉及只向的内容的变化
	self=head->next;
	while(self!=NULL)
	{
		//TODO 
		if(self->x==head->x&&self->y==head->y)
			{
				return 1;
			} 
		self = self->next;
	}
    return 0;
}


int cantCrossWall(){
	if(head->x==0||head->x==56||head->y==0||head->y==26)
	{
		system("cls");
		gotoXY(30,6);
		printf("撞墙了");
		return 1;
	}
	return 0;
} 
 
 
int snakeMove()
{
	snake *newNode;
	
	newNode = (snake*)malloc(sizeof(snake));
	if(status==U)
	{
		newNode->x=head->x;
		newNode->y=head->y-1;
	}
    if (status == D) {
		newNode->x = head->x;
		newNode->y = head->y + 1;
	}
	if (status == L) {
		newNode->x = head->x - 2;
		newNode->y = head->y;
	}
	if (status == R) {
		//TODO
		newNode->x = head->x + 2;
		newNode->y = head->y;
	}

		newNode->next=head;
		//将新的结点赋值给head 
		head=newNode;
		if(cantCrossWall())
		{
				status = 0;
				endGame();
				return 3;
		} 
		
		//判断新的结点是否有食物
		q=head;
		if(newNode->x==food->x&&newNode->y==food->y)
		{
			while(q!=NULL)
			{
				//TODO
				gotoXY(q->x,q->y);
				color(14);
				printf("◆");
				q=q->next;
			}
			//吃掉食物过后总分要加上单个食物的分值 
			sumScore+=add;
			//调用一次
			screenTips(); 
			//食物被吃掉了,再创建一个
			createFood(); 
			
		} else{
		        while(q->next->next!=NULL)
					{
						//TODO
						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)
	          {
			  	endGame(); 
				  return 2; 
			  }
	return 0;
}


void keyboardControl()
{
	status =R;//默认向右走 *
	while(1)
	{
		//TODO
		//侦听所有键盘事件状态 
		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))
		{
			//TODO
			//第一次按空格键,进程挂起, 
			while(1)
			{
				Sleep(300);
				//第二次空格键,进程退出 
				if(GetAsyncKeyState(VK_SPACE))
				{
					break;
				}
			}//按下ESC键,游戏结束 
		} else if(GetAsyncKeyState(VK_ESCAPE)){
			endGameStatus=3;
			endGame();
			break; 
		}
		Sleep(300);
		//让蛇动起来 
		if(snakeMove()!=0)
		{
			break;
		}
	}
}


void createFood()
{
	snake* food1;
	food1=(snake*)malloc(sizeof(snake)); 
	//加上随机数种子,防止伪随机
	srand((unsigned)time(NULL)); 
	food1->x=rand()%53+2;//2.54没听懂?问问
	//【2~54】
	while(food1->x%2!=0)
	{
		//TODO
		food1->x=rand()%53+2;
	}
	food1->y=rand()%23+1;//?2.58
	//food1->next=NULL;?
	q=head;
	while(q->next!=NULL)
	{
		if(q->x==food1->x&&q->y==food1->y)
		{
			free(food1);
			createFood();//如果和蛇身重合了,就再次创建食物 
		}
		q=q->next; 
	}
	//把食物打出来
	gotoXY(food1->x,food1->y);
	color(10);
	printf("●");
	food=food1;
}



void initSnake()
{
	int i=1;
	color(15);
	snake *ss;//结点 
	//分配内存空间,使用头插法(单链表),以设定的x,y位置开始插入
	ss =(snake*)malloc(sizeof(snake));//?
	ss->x=24;
	ss->y=5;
	ss->next=NULL; 
	//利用头插法,插入剩下的四个结点 
	for(i=1;i<=4;i++)
	{
	head=(snake*)malloc(sizeof(snake));
	head->next=ss;
	head->x=24+i*2;
	head->y=5;
	ss=head;//将蛇头变成蛇尾,然后重复循环 
	}
	//打印蛇身
	while(ss!=NULL)
	{
		//TODO
		gotoXY(ss->x,ss->y);
		printf("◆");
		ss=ss->next;
	} 
}

int color(int x)
{
	//更改待打印颜色属性 
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x); 
	return 0;
} 


void gotoXY(int a,int b)
{
	COORD c;//生成c的实例 
	c.X=a;
	c.Y=b;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); 
}
//函数定义/函数实现
 void createMap()
 {
 	int i,j;
 	color(6);//0~255 
 	//两个for填边框的色 
 	for(i=0;i<58;i+=2)
 	{
	 	gotoXY(i,0);
	 	printf("□");
	 	gotoXY(i,26);
	 	printf("□");
	 }
 	for(i=1;i<26;i++)
 	{
	 	gotoXY(0,i);
		printf("□");
		gotoXY(56,i);
		printf("□");
	 }
	 //填中间的色 
	 color(3); 
	 for(i=2;i<56;i+=2)
	 {
	 	for(j=1;j<26;j++)
	 	{
		 	gotoXY(i,j);
		 	printf("■");
		 } 
	 }
	    
 } 
 
 
int main()
{
	//设置控制台的宽高 
	    system("mode con cols=110 lines=30");
	    printsnake();
	    welcometoGame();
		while(1)
		{
			
		} 
	return 0;
}

First of all, this time my compiler is the following one (You need to install the package, please send a private message to Xiaomiao), develop A good habit.

 good habits:

Everything is subject to the overall original code, and the picture introduction is for reference only. If you haven't completed one step, I suggest you compile it and conduct phased testing. This is a very practical habit. It can reduce the bug to the scope of the subroutine and ensure the completion efficiency of 800 lines of code. Thank you.

Xiaomiao feels that pictures can display the code more intuitively (colors have intuitive differences) and facilitate debugging, but the csdn system hopes that Xiaomiao has a more complete description. All Xiaomiao will attach partial codes for debugging purposes only, and duplication is inevitable. , please understand, the number of words is too high.


Window settings


mapping

Partial code (for debugging only)

void gotoXY(int a,int b)
{
	COORD c;//生成c的实例 
	c.X=a;
	c.Y=b;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); 
}
//函数定义/函数实现
 void createMap()
 {
 	int i,j;
 	color(6);//0~255 
 	//两个for填边框的色 
 	for(i=0;i<58;i+=2)
 	{
	 	gotoXY(i,0);
	 	printf("□");
	 	gotoXY(i,26);
	 	printf("□");
	 }
 	for(i=1;i<26;i++)
 	{
	 	gotoXY(0,i);
		printf("□");
		gotoXY(56,i);
		printf("□");
	 }
	 //填中间的色 
	 color(3); 
	 for(i=2;i<56;i+=2)
	 {
	 	for(j=1;j<26;j++)
	 	{
		 	gotoXY(i,j);
		 	printf("■");
		 } 
	 }
	    
 } 

Write a color transformation function

Conduct phased testing

Describe the functions


Snake node creation

void initSnake()
{
	int i=1;
	color(15);
	snake *ss;//结点 
	//分配内存空间,使用头插法(单链表),以设定的x,y位置开始插入
	ss =(snake*)malloc(sizeof(snake));//?
	ss->x=24;
	ss->y=5;
	ss->next=NULL; 
	//利用头插法,插入剩下的四个结点 
	for(i=1;i<=4;i++)
	{
	head=(snake*)malloc(sizeof(snake));
	head->next=ss;
	head->x=24+i*2;
	head->y=5;
	ss=head;//将蛇头变成蛇尾,然后重复循环 
	}
	//打印蛇身
	while(ss!=NULL)
	{
		//TODO
		gotoXY(ss->x,ss->y);
		printf("◆");
		ss=ss->next;
	} 
}


food creation

void createFood()
{
	snake* food1;
	food1=(snake*)malloc(sizeof(snake)); 
	//加上随机数种子,防止伪随机
	srand((unsigned)time(NULL)); 
	food1->x=rand()%53+2;//2.54没听懂?问问
	//【2~54】
	while(food1->x%2!=0)
	{
		//TODO
		food1->x=rand()%53+2;
	}
	food1->y=rand()%23+1;//?2.58
	//food1->next=NULL;?
	q=head;
	while(q->next!=NULL)
	{
		if(q->x==food1->x&&q->y==food1->y)
		{
			free(food1);
			createFood();//如果和蛇身重合了,就再次创建食物 
		}
		q=q->next; 
	}
	//把食物打出来
	gotoXY(food1->x,food1->y);
	color(10);
	printf("●");
	food=food1;
}


Keyboard event listening

void keyboardControl()
{
	status =R;//默认向右走 
	while(1)
	{
		//TODO
		//侦听所有键盘事件状态 
		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;
		}
		Sleep(500);
		//让蛇动起来 
		snakeMove(); 
	}
}

snake movement

  

//让蛇动起来
void snakeMove();
 
void snakeMove()
{
	snake *newNode;
	
	newNode = (snake*)malloc(sizeof(snake));
	if(status==U)
	{
		newNode->x=head->x;
		newNode->y=head->y-1;
	}
    if (status == D) {
		newNode->x = head->x;
		newNode->y = head->y + 1;
	}
	if (status == L) {
		newNode->x = head->x - 2;
		newNode->y = head->y;
	}
	if (status == R) {
		//TODO
		newNode->x = head->x + 2;
		newNode->y = head->y;
	}

		newNode->next=head;
		//将新的结点赋值给head 
		head=newNode;
		//判断新的结点是否有食物
		q=head;
		if(newNode->x==food->x&&newNode->y==food->y)
		{
			while(q!=NULL)
			{
				//TODO
				gotoXY(q->x,q->y);
				color(14);
				printf("◆");
				q=q->next;
			}
			//食物被吃掉了,再创建一个
			createFood(); 
		} else{
		        while(q->next->next!=NULL)
					{
						//TODO
						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; 
	          }
	
}

 The snake can move, but there is a bug after the snake hits the wall, and further modifications are needed.

 Screen tips

Global variables:

 subroutine

//主页面上小提示
void screenTips();

void screenTips(){
	color(11);
	gotoXY(64,4);
	printf("☆最高记录是:53☆");
	color(14);
	gotoXY(64,6);
	printf("当前得分:20");
	gotoXY(73,11);
	printf("小提示");
	
	gotoXY(60,13);
	color(6);
	printf("§--------------------§");
	
	gotoXY(60,26);
	color(6);
	printf("§--------------------§");
	
	gotoXY(64,14);
	color(3);
	printf("每个食物得分:%d分",add);
	
	gotoXY(64,16);
	printf("不能撞墙,不能咬到自己");

	gotoXY(64,18);
	printf("用上,下,左,右箭头分别控制方向");	
	
	gotoXY(64,20);
	printf("F1键加速,F2键减速");
	
	gotoXY(64,22);
	printf("空格键暂停游戏");
	
	gotoXY(64,24);
	printf("Esc键退出游戏");
	 
} 

 end page

Pinocchio

void LostDraw() {
	/*
		失败界面
	*/
	system("cls");
	int i;
    gotoXY(45, 1);
	color(6);
	printf(" |-----|   ");		//匹诺曹的帽子
	gotoXY(45, 2);
	color(6);
	printf(" |     |   ");
	gotoXY(43, 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(48, 6);
	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(60, 20);
	color(11);
	printf("----------------------+");						//下边框



}

End page implementation and its sub-function implementation

void endGame()
{
	//清屏操作 
	system("cls");
	LostDraw();
	if(endGameStatus==1)
	{
		//TODO
		gotoXY(35,9);
		color(7);
		printf("对不起,你撞墙了!游戏结束"); 
	}else if(endGameStatus==2)
		{
			//TODO
			gotoXY(35,9);
			color(7);
			printf("对不起,你咬到自己了!游戏结束"); 
		}
	else if(endGameStatus==3)
			{
				//TODO
				gotoXY(35,9);
				color(7);
				printf("游戏结束!"); 
			} 
	gotoXY(43,12);
	printf("你的得分是:%d分",sumScore);
	//判断得分是否需要存储
	if(sumScore>=highScore)
	{
		color(10);
		gotoXY(33,16);
		printf("创纪录了,最高分被你刷新了!"); 
		//最高分存入文件当中 
		File_In();
	} else{
		gotoXY(33,16);
		printf("继续努力吧~你离最高分还差:%d",highScore-sumScore);
	} 
	//调用选择选项
	 choose(); 
	
}


void choose()
{
	int n=0;
	gotoXY(30,23);
	color(12);
	printf("重玩一局[1]");
	gotoXY(50,23);
	printf("溜了溜了[2]");
	gotoXY(43,25);
	printf("选择:");
	scanf("%d",&n);
	switch(n)
	{
		case 1:
			system("cls");
		    printsnake();
		    //开始界面
			 welcometoGame(); 
			break;
		case 2:
			exit(0);
			break;
		default:
			gotoXY(35,27);
			color(12);
			printf("你输入的有误,请重新输入");
			break; 
			
	} 
} 


//文件读写
void File_In()
{
	//定义文件指针 
	FILE *fp;
	//指针关联文件 
	fp=fopen("save.txt","w+");
	//通过文件指针向文件写入内容 
	fprintf(fp,"%d",sumScore);
	//关闭文件 
	fclose(fp);
}

void File_Out()
{
	FILE *pp;
	pp=fopen("save.txt","a+");
	fscanf(pp,"%d",&highScore);
	fclose(pp);
	
}

start page

/*
	字符拼成英文图案
*/
void printsnake()
{
	color(2);
	printf("                                                                                         \n");
	printf("                       __________       ___                                              \n");
	printf("                      /          \\     / \\ \\    |____      __\\__                     \n");
	printf("                     /  ________  \\   / ___ \\  _/ __     | |   /                       \n");
	printf("                     |  |      |__|     _/_   |_|  /    [|] |/                           \n");
	printf("                     |  |              | | |      /     _|_ \\__/                        \n");
	printf("                     \\  \\_______        / \\      |___/        ____                    \n");
	printf("                      \\         \\    ____ ____      ____   __ |  |  ___   ______       \n");
	printf("                       \\_______  \\   |  |/    \\    /    \\_/ / |  | /  /  /      \\   \n");
	printf("                               \\  \\  |    ___  \\  / ____   /  |  |/  /  /  ____  \\   \n");
	printf("                     __        |  |  |   /   \\  \\ | |  |  /   |     /  |  /____\\  |   \n");
	printf("                    \\  \\_______|  |  |  |    |  | | |__|  |   |     \\  |  ________/   \n");
	printf("                     \\            /  |  |    |  |  \\       \\  |  |\\  \\  \\  \\____  \n");
	printf("                      \\__________/   |__|    |__|   \\___/\\__\\ |__| \\__\\  \\______/ \n");

}
void welcometoGame()
{
	int n=0;
	int i=0;
	int j=0;
	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("|");
			}
		} 
	}
	gotoXY(36,22);
	color(10);
	printf("1.开始游戏");
	gotoXY(50,22);
	color(10);
	printf("2.游戏说明");
    gotoXY(36,24);
	color(10);
	printf("3.退出游戏");
    gotoXY(28,27);
    color(3);
    printf("请选择[1 2 3]:[ ]\b\b");//\b是退格使光标处于两个中括号之间
	color(14);
	scanf("%d",&n);
	switch(n)
	{
	case 1:
		system("cls");
		File_Out();
		sumScore=0;
		createMap();
		screenTips();
		initSnake();
		createFood();
		keyboardControl();
		break;
	case 2:
		//游戏说明界面 
		break;
	case 3:
		exit(0); 
		break;
	default:
		break;
		
	} 
			
}

head File

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>//要使用windows的api ,lrq 
//因为要用到随机数函数,所以要调用time.h文件
#include<time.h> 
#include<conio.h>//用来接收键盘的输入输出 
//
//预定义
#define  U 1
#define  D 2
#define  L 3
#define  R 4
//U上 D下 L左 R右 

global variables

//定义全局变量 
typedef struct snake
{
	int x;
	int y;
	struct snake* next;
}snake;

snake *head;//指向蛇头的指针
snake *food;//创建的食物 
snake *q;//用来遍历蛇的结点的 
 
int status;
 
int add=10; //每个食物的得分 
int sumScore=0;//总得分 
int highScore=0;//最高分 

//三种结束状态变量
//1,撞墙    2,自己咬到自己了    3,主动退出 
int endGameStatus=0; 

function declaration

//函数声明
void createMap();
//坐标转换
void gotoXY(int a,int b); 
//颜色更换
int color(int x);
//创建蛇
void initSnake(); 
//创建食物
void createFood(); 
//侦听键盘事件
void keyboardControl(); 
//让蛇动起来
int snakeMove();
//撞墙停止
int cantCrossWall();
//自己咬到自己
int biteSelf();
//主页面上小提示
void screenTips();
//结束页面 
void endGame();
//匹诺曹绘制
void LostDraw();
void File_In(); 
void File_Out();
void choose();
void printsnake();
void welcometoGame();

main function

int main()
{
	//设置控制台的宽高 
	    system("mode con cols=110 lines=30");
	    printsnake();
	    welcometoGame();
		while(1)
		{
			
		} 
	return 0;
}

(Water word count) Introduction to the game Snake

background settings

Editor Broadcast

Snake game pictures (19 photos)

In 1976, Blockade, a classic arcade game, was launched on the Gremlin platform. In the game, two players each control a character to move on the screen and build fences wherever they pass. The character can only turn 90 degrees to the left or right, and the goal of the game is to make the opponent hit the screen or the fence first. It sounds a bit complicated, but it actually looks like this:

It's basically a competition between two greedy snakes that grow bigger with every step they take. All the player has to do is avoid hitting obstacles and getting longer and longer. For more photos and videos, see GamesDBase’s introduction.

Blockade was very popular, and similar games appeared on early game consoles and computers such as the Atari 2600, TRS-80, and Apple 2. But what really made this game form popular all over the world was the Snake game that came to the world with Nokia mobile phones 21 years later.

Operation guide

Editor Broadcast

The player uses the direction keys to control a long snake to continuously swallow beans. At the same time, the snake's body continues to grow longer with the swallowed beans. The game ends when the snake's head hits the snake's body or a barrier. Snake was originally known as a small game included with Nokia mobile phones, and it accompanied Nokia mobile phones to the world. There are now many derivative versions of Snake and they have been ported to various platforms.

Game features

Editor Broadcast

Snake prototype game pictures

The Snake game is simple to operate and has high playability. The most difficult part of this game is not when the snake grows very long, but at the beginning. At that time, the body of the snake was very short and it didn't look difficult, but it was the easiest to die because playing with a short snake made people easily distracted and lost their patience. Due to the low difficulty, you will unknowingly speed up the adjustment of direction. As you move freely, the snake's body gradually lengthens. However, the player does not realize the danger, and at the end is complacent a> died suddenly at the moment.

Another dangerous period for Snake is dozens of seconds after the game starts. Because the player's attention is highly concentrated and the spirit is tense, if the situation is slightly better at this time, he will involuntarily want to relax. As a result, the greedy snake will die as soon as his finger relaxes. So Snake can be regarded as an agile mini-game.

Game review

Editor Broadcast

Snake game screen

Different from other games, Snake is a tragicgame. Many video games end with the operator's victory, but the ending of Snake is death< /span>. No matter how skillfully you play or how superb your technique is, all you hear in the end is the scream of a greedy snake. When the little snake on the phone grows longer and longer, and the points get higher and higher, death gets closer and closer. At that time, I was busy not eating beans to grow taller, but to avoid hitting the wall. You will find yourself overwhelmed and running around. The most terrible thing about the greedy snake is the word "greed".

I have to die, I have to die, I have to die, I only have one breath left, ah————.

I originally wanted to write while typing, but the code was jumping around and my soul had already escaped. I will sort it out again tomorrow. This blog is the result of 16 40-minute blog posts. I think after finishing one blog, my brain will feel a little overwhelmed. pain. Hopefully combing will be better tomorrow, I've already vomited blood today.

It’s not easy to update, so please give me likes, your questions are welcome, and thank you for forwarding,

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Guess you like

Origin blog.csdn.net/ormstq/article/details/128433220