Skip to content

Latest commit

 

History

History
232 lines (226 loc) · 5.63 KB

贪吃蛇游戏.md

File metadata and controls

232 lines (226 loc) · 5.63 KB

title: 贪吃蛇游戏 categories: 编程 tags: [编程] date: 2019-07-02 20:42:00

  • 好久之前写的了,现在发布出来
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
#include<cstring>
using namespace std;
int hard = 300;
int score = 0;
int is_alive = 1;
time_t timeNow = time(NULL);
int towards = 4;	//蛇的朝向	1:向上	2:向下	3:向左	4:向右
int snake[2][225] = {{5},{7}};	//蛇的xy坐标
int snakeEnd = 2;
int Seed[2] = {-1,-1};
char Boardiniti[16][31]= {				//初始化的普通板子
	{"ssyzxssyzxssyzxssyzxssyzxssyzx"},	//0没用
	{"##############################"},	//1
	{"#                            #"},	//2
	{"#                            #"},	//3
	{"#                            #"},	//4
	{"#                            #"},	//5
	{"#                            #"},	//6
	{"#                            #"},	//7
	{"#                            #"},	//8
	{"#                            #"},	//9
	{"#                            #"},	//10
	{"#                            #"},	//11
	{"#                            #"},	//12
	{"#                            #"},	//13
	{"#                            #"},	//14
	{"##############################"},	//15
};
char Board[16][31];						//真正的板子
int start();					//游戏初始化	返回值1->开始
void printBoard();				//打印棋盘
void play();
void changBoard(int x,int y,int changeFlag);	//改变棋盘1方块,2蛇头,3蛇身,4随机点
void genSeed();					//生成种子
void move();					//移动
void getTowards();				//获取方向	蛇的朝向	1:向上	2:向下	3:向左	4:向右
void changeSnakeBody();			//改变蛇身,帮助move()
void initiBoard();				//初始化板子
void updataBoard();				//更新板子
void Judge();					//吃种子判断
void End();						//挂了
int main() {
	initiBoard();
	while(1) {
		int sta = start();			//调用start 返回值1->开始
		if(sta == 1) {
			play();
		} else if(sta == 2) {
			system("cls");
			cout<<endl<<endl<<endl<<endl<<endl<<endl<<"   您想要的难度(20-1000)"<<endl<<"         默认300:";
			int hardin;
			cin>>hardin;
			if(hardin<=1000&&hard>=20) {
				hard = hardin;
			} else {
				system("cls");
				cout<<endl<<endl<<endl<<endl<<endl<<endl<<"       错误,回到主界面";
				Sleep(500);
			}
		}
	}
	return 0;
}
int start() {
	system("mode con cols=30 lines=16");
	while(1) {
		/*开始界面*/
		{
			for(int i = 1; i <= 5; i++) {
				cout<<endl;
			}
			cout<<"┌━━━━━━━━━━━━━┐";
			cout<<"│    输入1以开始游戏...    │";
			cout<<"│    输入2以更改难度...    │";
			cout<<"└━━━━━━━━━━━━━┘";
		}
		char a = getch();
		if(a == '1') {
			return 1;
		} else if(a == '2') {
			return 2;//难易调整
		} else {
			system("cls");
			cout<<"	     错误!";
		}
	}
}
void initiBoard() {
	for(int i = 1; i<=15; i++) {
		strcpy(Board[i],Boardiniti[i]);	//把Boardiniti复制到Board中
	}
}
void printBoard() {
	system("cls");
	for(int i = 1; i <= 15; i++) {
		cout<<Board[i];
	}
}
void changeBoard(int x,int y,int changeFlag) {

	if(changeFlag==1) {
		Board[y][x] = ' ';
	} else if(changeFlag==3) {
		Board[y][x] = 'X'; //■█□
	} else if(changeFlag==4) {
		Board[y][x] = '*';
	}
}
void changeiniBoard(int x,int y,int changeFlag) {
	if(changeFlag == 1)
		Boardiniti[y][x] = '*';
	if(changeFlag == 2)
		Boardiniti[y][x] = ' ';
}
void changeSnakeBody() {
	for(int i = 0; i <=1; i++) {
		for(int j = snakeEnd; j >=1; j--) {
			snake[i][j] = snake[i][j-1];
		}
	}
}
void move() {
	if(towards == 2) { //
		changeSnakeBody();
		snake[1][0]++;
	} else if(towards == 1) { //
		changeSnakeBody();
		snake[1][0]--;
	} else if(towards == 3) { //
		changeSnakeBody();
		snake[0][0]--;
	} else if(towards == 4) { //
		changeSnakeBody();
		snake[0][0]++;

	}
}
void getTowards() {
	int twds=towards;
	timeNow = GetTickCount();
	while(GetTickCount()<timeNow+hard) {
		if(kbhit()) {
			char tows = getch();
			if((tows == 'w'||tows == 'W')&&twds!=2) towards = 1;
			else if((tows == 's'||tows == 'S')&&twds!=1) towards = 2;
			else if((tows == 'a'||tows == 'A')&&twds!=4) towards = 3;
			else if((tows == 'd'||tows == 'D')&&twds!=3) towards = 4;
		}
	}
}
void updataBoard() {
	initiBoard();
	for(int i = snakeEnd; i >=0; i--) {
		changeBoard(snake[0][i],snake[1][i],3);
	}
}
void genSeed() {
	if(Seed[0]==-1) {
		srand((unsigned)GetTickCount());
		int selfx =(rand() % (29-2+1))+ 1;
		int selfy =(rand() % (14-2+1))+ 2;
		Seed[0] = selfx;
		Seed[1] = selfy;
		changeiniBoard(selfx,selfy,1);
	}
}
void Judge() { //对吃豆进行判断
	for(int i = 0; i<=snakeEnd; i++) {
		if(Seed[0]==snake[0][i]&&Seed[1]==snake[1][i]) {
			changeiniBoard(Seed[0],Seed[1],2);//恢复格子
			Seed[0]=-1;//恢复到要生成状态
			score++;
			snakeEnd++;
		}
		if(snake[1][0]==snake[1][i]&&snake[0][0]==snake[0][i]&&i!=0&&i!=1&&i!=2) {
			is_alive = 0;
		}
	}
	if(Boardiniti[snake[1][0]][snake[0][0]] == '#') {
		is_alive=0;
	}
}
void End() {
	{
		system("cls");
		for(int i = 1; i <= 5; i++) {
			cout<<endl;
		}
		cout<<"┌━━━━━━━━━━━━━┐";
		cout<<"│    游戏结束-=-=-=-=-=    │";
		cout<<"└━━━━━━━━━━━━━┘";
		cout<<"         score="<<score<<endl;
		system("pause");
	}
}
void play() {
	score = 0;
	is_alive = 1;
	towards = 4;	//蛇的朝向	1:向上	2:向下	3:向左	4:向右
	snake[0][0] = 5;	//蛇的xy坐标
	snake[1][0] = 7;
	snakeEnd = 1;
	changeiniBoard(Seed[0],Seed[1],2);
	Seed[0] = -1;
	Seed[1] = -1;
	while(is_alive) {
		getTowards();			//获取朝向
		move();					//让大蛇飞
		updataBoard();
		printBoard();			//画 板 子
		genSeed();				//生成种子
		Judge();				//判断是否吃豆
	}
	End();
}