C language implementation [small game-greedy snake]


1.目标要求:

1. Control the direction of the snake head up, down, left and right
2. If the snake head touches the food, increase the length by one
3. If the snake head touches the frame, touches itself or the snake turns back, the game is over


2.C语言代码:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>

#define interface_x 19//画布行数
#define interface_y 60//画布列数 

/*
     	《贪吃蛇》	by:你最珍贵

	 --------------------------------------- 
	|控制信号: | 向上 | 向下 | 向左 | 向右 |
	 --------------------------------------- 
	|    键盘: |  'i' |  'k' |  'j' |  'l' | 
	 --------------------------------------- 
	 
	 1.上下左右控制蛇头转向  
	 2.若蛇头碰到食物,长度加一 
	 3.若蛇头碰到边框或自身,游戏结束 

*/

void HideCursor(){	//隐藏光标位置 ,这个函数复制代码就行 
	CONSOLE_CURSOR_INFO cursor_info={1,0}; 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void gotoxy(int x,int y){	//把光标放在(0,0)位置 ,这个函数复制代码就行
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle,pos);
} 

int IsEnd;//是否结束 
int canvas[interface_x+1][interface_y+1]={0};//画布行数、列数 
int scores;//分数 
int slow_v,slow_v_num;//移动变慢的值(越大速度越慢),移动变慢的变量
int food_x,food_y;//实物位置 
int snakeH_x,snakeH_y;//蛇头位置 
int snake_direction; //蛇的方向 

void startup(){	//【数据初始化】 
	int i,j; 
	HideCursor();//不显示光标 
	IsEnd = 0; 
	scores=0;	
	slow_v=2;
	slow_v_num=1; 
	for(i=1;i<=interface_x;i++){//设置边框 
		canvas[i][1]=-1;
		canvas[i][interface_y]=-1;
	} 
	for(i=1;i<=interface_y;i++){//设置边框 
		canvas[1][i]=-1;
		canvas[interface_x][i]=-1;
	}
	food_x=rand()%(interface_x-2)+2;//设置食物位置 
	food_y=rand()%(interface_y-2)+2;
	canvas[food_x][food_y]=-2;//设置食物 
	snakeH_x=(interface_x/2);//设置蛇头位置 
	snakeH_y=3;
	canvas[snakeH_x][snakeH_y]=1;//设置蛇头 
	snake_direction=4;
}
void show_begin(){//【初始页面展示】 
	int i,j;
	int start=0;
	char input;
 	while(!start){
 		gotoxy(0,0);//每次输出页面把鼠标放回(0,0) 
 		for(i=1;i<=24/2-4;i++) printf("\n"); 
		for(i=1;i<80/2-5;i++) printf(" ");
		printf("《贪吃蛇》\n\n"); 
		for(i=1;i<80/2-9;i++) printf(" ");
		printf("请按任意键开始游戏\n\n");
		for(i=1;i<=24/2-6;i++) printf("\n");
		if(kbhit()){
			input = getch();
			if(input) start=1;
		}
	}
} 
void show(){	//【显示画面】 
	gotoxy(0,0);//每次输出页面把鼠标放回(0,0) 
	int i,j,k;	
	for(i=1;i<=(80-interface_y)/2+interface_y/2-10;i++) printf(" ");
	printf("《贪吃蛇》") ;
	for(i=1;i<=interface_y/2-8-8;i++) printf(" ");
	printf("分数:%3d\n",scores);
	for(i=1;i<=interface_x;i++){
		for(k=1;k<=(80-interface_y)/2;k++) printf(" ");//使屏幕对称加的空格 
		for(j=1;j<=interface_y;j++){			
			if(canvas[i][j]==-1){//值为-1输出边框# 
				printf("#");
			}else if(canvas[i][j]==-2){//值为-2输出实物M 
				printf("M");
			}else if(canvas[i][j]==1){//值为1输出蛇头@ 
				printf("@");
			}else if(canvas[i][j]>1){//值>1输出蛇尾* 
				printf("*");
			}else printf(" ");//值为0输出空白 
		}
		printf("\n");
	}
	
}
void move_snake(){
	int i,j;
	int max=0;//最大值 
	int max_x,max_y;//最大值位置
	for(i=2;i<=interface_x-1;i++){
		for(j=2;j<=interface_y-1;j++){
			if(canvas[i][j]>max){//寻找最大值 
				max=canvas[i][j];
				max_x=i;max_y=j;
			}
			if(canvas[i][j]>0){//把蛇头蛇尾每一个值加1 
				canvas[i][j]++;
			}			 
		}
	}
	if(snake_direction==1){//上 
		snakeH_x--; 
	}else if(snake_direction==2){//下 
		snakeH_x++; 
	}else if(snake_direction==3){//左 
		snakeH_y--; 
	}else if(snake_direction==4){//右 
		snakeH_y++; 
	}	 
	if(canvas[snakeH_x][snakeH_y]==-1||canvas[snakeH_x][snakeH_y]>1){
		exit(1);//碰到边界或自身,游戏结束 
	}else if(canvas[snakeH_x][snakeH_y]==-2){//吃到食物 
		canvas[snakeH_x][snakeH_y]=1;//添加新蛇头 
		food_x=rand()%(interface_x-2)+2;//设置新食物位置 
		food_y=rand()%(interface_y-2)+2;
		canvas[food_x][food_y]=-2;
		printf("\a") ;
		scores++;
	}else{//什么都没有碰到则移动 
		canvas[snakeH_x][snakeH_y]=1;//添加新蛇头 
		canvas[max_x][max_y]=0;//去掉蛇尾 
	} 
}
void update_outinput(){	//【与输入无关的更新】 
	int i;
	slow_v_num=(slow_v_num==slow_v)? 1:slow_v_num+1;
	if(slow_v_num==slow_v){//每隔slow_v次运行一次		
		move_snake(); 
	} 
	
}
void update_input(){//【与输入有关的更新】 
	char input;
	if(kbhit()){
		input = getch();
		if(input=='i'){//上 
			if(snake_direction==1){//如果输入与原来方向一致是加速功能,否则为转向 
				move_snake();//加速 
			}else snake_direction=1;//转向 
		}else if(input=='k'){//下 
			if(snake_direction==2){
				move_snake();
			}else snake_direction=2;
		}else if(input=='j'){//左 
			if(snake_direction==3){
				move_snake();
			}else snake_direction=3;
		}else if(input=='l'){//右 
			if(snake_direction==4){
				move_snake();
			}snake_direction=4;
		}	
	}
}
void show_end(){//【显示失败界面】 
	system("cls"); 
	int i;
	for(i=1;i<24/2-2;i++) putchar('\n');
	for(i=1;i<80/2-5;i++) putchar(' ');
	printf("game over!\n\n");
	for(i=1;i<80/2-7;i++) putchar(' ');
	printf("您的分数是:%3d分\n",scores);
	for(i=1;i<24/2-3;i++) putchar('\n');	
	system("pause");//暂停 
	system("pause");//暂停
}

int main(){
	startup();	//数据初始化
	show_begin();//初始页面 
	while(!IsEnd){	//游戏循环执行 
		show();	// 显示画面 
		update_outinput();	//与输入无关的更新 
		update_input();	//与输入有关的更新 
	}
	show_end(); //显示失败界面 
	return 0;
}

3.运行结果:

Small gameplay:

 	      《贪吃蛇》	by:你最珍贵

 --------------------------------------- 
|控制信号: |  向上 |  向下 |  向左 |  向右 |
 --------------------------------------- 
|   键盘: |  'i'  |  'k' |  'j'  |  'l'  | 
 --------------------------------------- 

 and
 and

Published 8 original articles · Like1 · Visitors 179

Guess you like

Origin blog.csdn.net/weixin_43503632/article/details/105453690