C语言贪吃蛇项目简易版

#include 
#include 
#include 
#include 

#define MAXLENGTH 38
#define UP 72
#define DOWN 80
#define RIGHT 77
#define LEFT 75

int Map[40][40] = { 0 };
HANDLE hStd = 0;

typedef struct _SNAKE {
	int SnakeX[MAXLENGTH];
	int SnakeY[MAXLENGTH];
	int SnakeSize;
	int SnakeDirectory;
	int Flag;
}SNAKE,*pSNAKE;

typedef struct _FOOD {
	int FoodX;
	int FoodY;
	int nFlag;
}FOOD,*pFOOD;

SNAKE Snake;
FOOD Food;

bool SetWindowInfo();
bool InitMap();
bool WriteChar(char* pChar, int x, int y, WORD attr);
bool InitSnake();
bool CreateFood();
bool SetupSnakeAndFood();
bool SnakeMove();
bool KillSnake();

int main() {
	srand(time(NULL));
	hStd = GetStdHandle(STD_OUTPUT_HANDLE);
	SetWindowInfo();
	InitMap();
	InitSnake();
	SetupSnakeAndFood();
	while (1) {
		Sleep(100-10*Snake.SnakeSize);
		SnakeMove();
		SetupSnakeAndFood();
		
	}
	
	getchar();
	return 0;
}

bool SetWindowInfo() {
	SetConsoleTitle(TEXT("贪吃蛇"));
	SMALL_RECT sr;
	sr.Top = 0;
	sr.Bottom = 40;
	sr.Left = 0;
	sr.Right = 80;
	SetConsoleWindowInfo(hStd, true, &sr);
	COORD cd = {81,41};
	SetConsoleScreenBufferSize(hStd, cd);
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = sizeof(CONSOLE_CURSOR_INFO);
	cci.bVisible = false;
	SetConsoleCursorInfo(hStd, &cci);
	return true;
}

bool InitMap() {
	for (size_t i = 0; i < 40; i++) {
		for (size_t j = 0; j < 40; j++) {
			if (i == 0 || j == 0 || i == 39 || j == 39) {
				Map[i][j] = 2;
			}
		}
	}
	for (size_t i = 0; i < 40; i++) {
		for (size_t j = 0; j < 40; j++) {
			if(Map[i][j]==2){
				WriteChar("■", i, j, FOREGROUND_RED);
			}
		}
	}
	return true;
}

bool WriteChar(char* pChar, int x, int y, WORD attr) {
	COORD cr = { 2 * x,y };
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 1;
	cci.bVisible = false;
	SetConsoleCursorPosition(hStd, cr);
	SetConsoleTextAttribute(hStd, attr);
	printf(pChar);
	return true;
}

bool InitSnake() {
	Snake.SnakeSize = 2;
	Snake.SnakeX[0] = 1;
	Snake.SnakeY[0] = 2;
	Map[Snake.SnakeX[0]][Snake.SnakeY[0]] = 1;
	Snake.SnakeX[1] = 1;
	Snake.SnakeY[1] = 1;
	Map[Snake.SnakeX[1]][Snake.SnakeY[1]] = 1;
	Snake.Flag = 0;
	Snake.SnakeDirectory = RIGHT;
	CreateFood();
	return true;
}

bool SetupSnakeAndFood() {
	for (size_t i = 1; i < 40; i++) {
		for (size_t j = 1; j < 40; j++) {
			if (Map[i][j] == 0) {
				WriteChar("  ", j, i, 1);
			}
			if (Map[i][j] == 1) {
				WriteChar("●", j, i, FOREGROUND_BLUE);
			}
			if (Map[i][j] == 3) {
				WriteChar("★", j, i, FOREGROUND_GREEN);
			}
		}
	}
	Food.nFlag = 1;
	return true;
}

bool CreateFood() {
	if (Food.nFlag == 1)return true;
	while (1) {
		Food.FoodX = rand() % (40 - 5);
		Food.FoodY = rand() % (40 - 5);
		if (Map[Food.FoodX][Food.FoodY] != 1 && Map[Food.FoodX][Food.FoodY] != 2 && Food.FoodX % 2 == 0) {
			Map[Food.FoodX][Food.FoodY] = 3;
			break;
		}
	}
}

bool SnakeMove() {
	KillSnake();
	int tempX = Snake.SnakeX[0];
	int tempY = Snake.SnakeY[0];

	int pre_key = Snake.SnakeDirectory;//记录前一个按键的方向
	int key = 0;
	if (_kbhit())//如果用户按下了键盘中的某个键
	{
		fflush(stdin);//清空缓冲区的字符

		//getch()读取方向键的时候,会返回两次,第一次调用返回0或者224,第二次调用返回的才是实际值
		key = _getch();//第一次调用返回的不是实际值
		key = _getch();//第二次调用返回实际值
	}

	if (key == UP && pre_key !=  DOWN) {
		Snake.SnakeDirectory = key;
	}
	else if (key == DOWN && pre_key != UP) {
		Snake.SnakeDirectory = key;
	}else if (key == RIGHT && pre_key != LEFT) {
		Snake.SnakeDirectory = key;
	}else if (key == LEFT && pre_key != RIGHT) {
		Snake.SnakeDirectory = key;
	}

	switch (Snake.SnakeDirectory) {
	case UP: {
		Snake.SnakeX[0]--;
		break;
	}
	case DOWN: {
		Snake.SnakeX[0]++;
		break;
	}
	case LEFT: {
		Snake.SnakeY[0]--;
		break;
	}
	case RIGHT: {
		Snake.SnakeY[0]++;
		break;
	}
	}
	int score = (10 * Snake.SnakeSize - 20);
	if (Map[Snake.SnakeX[0]][Snake.SnakeY[0]] == 2) {
		MessageBox(0, "死掉咯", "游戏结束", 0);
		exit(0);
	}
	if (Map[Snake.SnakeX[0]][Snake.SnakeY[0]] == 3) {
		Snake.SnakeSize++;
		for (size_t i = Snake.SnakeSize - 1; i > 0; i--) {
			if (i == 1) {
				Snake.SnakeX[i] = tempX;
				Snake.SnakeY[i] = tempY;
				break;
			}
			Snake.SnakeX[i] = Snake.SnakeX[i - 1];
			Snake.SnakeY[i] = Snake.SnakeY[i - 1];
		}
		Snake.SnakeX[0] = Food.FoodX;
		Snake.SnakeY[0] = Food.FoodY;
		Map[Snake.SnakeX[0]][Snake.SnakeY[0]] = 1;
		Food.nFlag = 0;
		CreateFood();
	}
	else {
		Map[Snake.SnakeX[Snake.SnakeSize - 1]][Snake.SnakeY[Snake.SnakeSize - 1]] = 0;
		for (size_t i = Snake.SnakeSize - 1; i > 0; i--) {
			if (i == 1) {
				Snake.SnakeX[i] = tempX;
				Snake.SnakeY[i] = tempY;
				break;
			}
			Snake.SnakeX[i] = Snake.SnakeX[i - 1];
			Snake.SnakeY[i] = Snake.SnakeY[i - 1];
		}
	}
	Map[Snake.SnakeX[0]][Snake.SnakeY[0]] = 1;
	return true;
}

bool KillSnake() {
	int score = (10 * Snake.SnakeSize - 20);
	for (size_t i = Snake.SnakeSize; i > 0 ; i--) {
		if (Snake.SnakeX[0] == Snake.SnakeX[i] && Snake.SnakeY[0] == Snake.SnakeY[i]) {
			MessageBox(0, "死掉咯", "游戏结束", 0);
			exit(0);
		}
	}
	return true;
}

你可能感兴趣的:(c语言,开发语言,网络安全,系统安全,c++)