贪吃蛇_代码+解析

头文件

宏定义

一大堆变量

颜色及移动光标函数

打印游戏规则

初始化

画地图

画蛇

移动函数

擦除尾巴

输入

放置食物

增加长度

GAME OVER

主函数

完整代码

头文件

#include<windows.h>
#include<stdlib.h>
#include<fstream>
#include<stdio.h>
#include<conio.h>
#include<time.h>

宏定义

#define UP 0    //上
#define DOWN 1  //下
#define LEFT 2  //左
#define RIGHT 3 //右
#define X 23    //X坐标
#define Y 40    //Y坐标
#define MAXLEN 200    //最大长度
#define MINTIME 75    //等待时间

一大堆变量

unsigned int snake[MAXLEN][2];//蛇的坐标
unsigned int len;             //长度
unsigned int lastt[2];        //蛇尾坐标

unsigned int score;           //得分
unsigned int max_score;       //最大得分

unsigned int way;             //方向

double wait;                  //等待时间

int input;                    //输入

unsigned int food[2];         //食物信息
bool empty=true;              //食物空

颜色及移动光标函数

void color(int _color){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
	return;
}

void gotoxy(int xx,int yy){
    COORD position={yy*2,xx};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
    return;
}

打印游戏规则

void welcome(){
	printf("游戏规则:\n");
	printf("w,a,s,d,控制移动\n");
	getch();
	system("cls");
	return;
}

初始化

void init(){
	
	system("title 贪吃蛇");//标题
	
	CONSOLE_CURSOR_INFO cursor_info={1,0};                             //隐藏
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);//光标
	
	fstream in("score.txt",ios::in);
	if(!in.fail())
		in>>max_score;//读取存档
	else
		welcome();//输出规则
	in.close();
	
	len=4;                    //以下代码设置设定蛇的初始信息
	
	snake[0][0]=X>>1;
	snake[0][1]=Y>>1;
	
	snake[1][0]=(X>>1)+1;
	snake[1][1]=Y>>1;
	
	snake[2][0]=(X>>1)+2;
	snake[2][1]=Y>>1;
	
	snake[3][0]=(X>>1)+3;
	snake[3][1]=Y>>1;
	
	way=UP;
	
	wait=150.0;
	
	return;
}

画地图

void drawmap(){
	color(255);
	for(unsigned int xx=0;xx<X;xx++){
		gotoxy(xx,0);
		printf("  ");
		gotoxy(xx,Y-1);
		printf("  ");
	}
	for(unsigned int yy=0;yy<Y;yy++){
		gotoxy(0,yy);
		printf("  ");
		gotoxy(X-1,yy);
		printf("  ");
	}
	return;
}

画蛇

void drawsnake(){
	
	color(255);
	gotoxy(0,0);
	printf("  ");
	
	color(10);
	gotoxy(snake[0][0],snake[0][1]);
	printf("□");
	gotoxy(snake[1][0],snake[1][1]);
	printf("■");
	
	return;
}

移动函数

void move(){
	
	lastt[0]=snake[len-1][0];
	lastt[1]=snake[len-1][1];
	
	for(unsigned int tc=len-1;tc>0;tc--){
		snake[tc][0]=snake[tc-1][0];
		snake[tc][1]=snake[tc-1][1];
	}
	
	switch(way){
		case UP:{
			snake[0][0]--;
			break;
		}
		case DOWN:{
			snake[0][0]++;
			break;
		}
		case LEFT:{
			snake[0][1]--;
			break;
		}
		case RIGHT:{
			snake[0][1]++;
			break;
		}
	}
	
	return;
}

擦除尾巴

void clt(){
	color(0);
	gotoxy(lastt[0],lastt[1]);
	printf("  ");
	return;
}

输入

void getin(){
	if(kbhit()!=0){
		while(kbhit()!=0)
			input=getch();
		switch(input){
			case 'W':case 'w':{
				if(way!=DOWN)
					way=UP;
				break;
			}
			case 'S':case 's':{
				if(way!=UP)
					way=DOWN;
				break;
			}
			case 'A':case 'a':{
				if(way!=RIGHT)
					way=LEFT;
				break;
			}
			case 'D':case 'd':{
				if(way!=LEFT)
					way=RIGHT;
				break;
			}
		}
	}
	return;
}

放置食物

void putfood(){
	
	if(empty){
		bool flag=true;
		srand(time(NULL));
		while(flag){
			food[0]=rand()%(X-2)+1;
			food[1]=rand()%(Y-2)+1;
			flag=false;
			for(unsigned int tc=0;tc<len;tc++)
				if(snake[tc][0]==food[0]&&snake[tc][1]==food[1])
					flag=true;
		}
		empty=false;
		color(14);
		gotoxy(food[0],food[1]);
		printf("◆");
	}
	return;
}

增加长度

void eatfood(){
	if(snake[0][0]==food[0]&&snake[0][1]==food[1]){
		empty=true;
		score++;
		if(wait>=MINTIME)
			wait-=0.5;
		if(len<MAXLEN)
			len++;
	}
	return;
}

GAME OVER

void gameover(){
	bool over=false;
	if(snake[0][0]==0||snake[0][0]==X-1||snake[0][1]==0||snake[0][1]==Y-1)
		over=true;
	for(int tc=1;tc<len;tc++)
		if(snake[0][0]==snake[tc][0]&&snake[0][1]==snake[tc][1])
			over=true;
	if(over==true){
		
		system("cls");
		
		while(kbhit()!=0)
			getch();
		
		printf("Game over!\n");
		printf("Score:%d\n",score);
		printf("Max score:%d\n",max_score);
		
		getch();
		system("cls");
		printf("Save...\n");
		
		if(score>max_score){
			fstream write;
			write.open("score.txt",ios::out);
			write<<score;
			write.close();
			max_score=score;
		}
		
		exit(0);
		
	}
	return;
}

主函数

int main(){
	init();
	drawmap();
	_sleep(3000);
	while(true){
		clt();
		drawsnake();
		putfood();
		eatfood();
		getin();
		move();
		gameover();
		_sleep(int(wait));
	}
	return 0;
}

完整代码

#include<windows.h>
#include<stdlib.h>
#include<fstream>
#include<stdio.h>
#include<conio.h>
#include<time.h>

using namespace std;

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define X 23
#define Y 40
#define MAXLEN 200
#define MINTIME 75

unsigned int snake[MAXLEN][2];
unsigned int len;
unsigned int lastt[2];

unsigned int score;
unsigned int max_score;

unsigned int way;

double wait;

int input;

unsigned int food[2];
bool empty=true;

void color(int _color){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
	return;
}

void gotoxy(int xx,int yy){
    COORD position={yy*2,xx};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
    return;
}

void welcome(){
	printf("游戏规则:\n");
	printf("w,a,s,d,控制移动\n");
	getch();
	system("cls");
	return;
}

void init(){
	
	system("title 贪吃蛇");
	
	CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
	
	fstream in("score.txt",ios::in);
	if(!in.fail())
		in>>max_score;
	else
		welcome();
	in.close();
	
	len=4;
	
	snake[0][0]=X>>1;
	snake[0][1]=Y>>1;
	
	snake[1][0]=(X>>1)+1;
	snake[1][1]=Y>>1;
	
	snake[2][0]=(X>>1)+2;
	snake[2][1]=Y>>1;
	
	snake[3][0]=(X>>1)+3;
	snake[3][1]=Y>>1;
	
	way=UP;
	
	wait=150.0;
	
	return;
}

void drawmap(){
	color(255);
	for(unsigned int xx=0;xx<X;xx++){
		gotoxy(xx,0);
		printf("  ");
		gotoxy(xx,Y-1);
		printf("  ");
	}
	for(unsigned int yy=0;yy<Y;yy++){
		gotoxy(0,yy);
		printf("  ");
		gotoxy(X-1,yy);
		printf("  ");
	}
	return;
}

void drawsnake(){
	
	color(255);
	gotoxy(0,0);
	printf("  ");
	
	color(10);
	gotoxy(snake[0][0],snake[0][1]);
	printf("□");
	gotoxy(snake[1][0],snake[1][1]);
	printf("■");
	
	return;
}

void move(){
	
	lastt[0]=snake[len-1][0];
	lastt[1]=snake[len-1][1];
	
	for(unsigned int tc=len-1;tc>0;tc--){
		snake[tc][0]=snake[tc-1][0];
		snake[tc][1]=snake[tc-1][1];
	}
	
	switch(way){
		case UP:{
			snake[0][0]--;
			break;
		}
		case DOWN:{
			snake[0][0]++;
			break;
		}
		case LEFT:{
			snake[0][1]--;
			break;
		}
		case RIGHT:{
			snake[0][1]++;
			break;
		}
	}
	
	return;
}

void clt(){
	color(0);
	gotoxy(lastt[0],lastt[1]);
	printf("  ");
	return;
}

void getin(){
	if(kbhit()!=0){
		while(kbhit()!=0)
			input=getch();
		switch(input){
			case 'W':case 'w':{
				if(way!=DOWN)
					way=UP;
				break;
			}
			case 'S':case 's':{
				if(way!=UP)
					way=DOWN;
				break;
			}
			case 'A':case 'a':{
				if(way!=RIGHT)
					way=LEFT;
				break;
			}
			case 'D':case 'd':{
				if(way!=LEFT)
					way=RIGHT;
				break;
			}
		}
	}
	return;
}

void putfood(){
	
	if(empty){
		bool flag=true;
		srand(time(NULL));
		while(flag){
			food[0]=rand()%(X-2)+1;
			food[1]=rand()%(Y-2)+1;
			flag=false;
			for(unsigned int tc=0;tc<len;tc++)
				if(snake[tc][0]==food[0]&&snake[tc][1]==food[1])
					flag=true;
		}
		empty=false;
		color(14);
		gotoxy(food[0],food[1]);
		printf("◆");
	}
	return;
}

void eatfood(){
	if(snake[0][0]==food[0]&&snake[0][1]==food[1]){
		empty=true;
		score++;
		if(wait>=MINTIME)
			wait-=0.5;
		if(len<MAXLEN)
			len++;
	}
	return;
}

void gameover(){
	bool over=false;
	if(snake[0][0]==0||snake[0][0]==X-1||snake[0][1]==0||snake[0][1]==Y-1)
		over=true;
	for(int tc=1;tc<len;tc++)
		if(snake[0][0]==snake[tc][0]&&snake[0][1]==snake[tc][1])
			over=true;
	if(over==true){
		
		system("cls");
		
		while(kbhit()!=0)
			getch();
		
		printf("Game over!\n");
		printf("Score:%d\n",score);
		printf("Max score:%d\n",max_score);
		
		getch();
		system("cls");
		printf("Save...\n");
		
		if(score>max_score){
			fstream write;
			write.open("score.txt",ios::out);
			write<<score;
			write.close();
			max_score=score;
		}
		
		exit(0);
		
	}
	return;
}

int main(){
	init();
	drawmap();
	_sleep(3000);
	while(true){
		clt();
		drawsnake();
		putfood();
		eatfood();
		getin();
		move();
		gameover();
		_sleep(int(wait));
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值