贪吃蛇 C语言实现代码

C语言实现贪吃蛇

#include<stdio.h>
#include<stdlib.h>    
#include<time.h>    
#include<conio.h>    
#include<Windows.h>    
#define FRAME 3    
#define INFO 50    
#define WIDTH 40    
#define LENGTH 25    
void setColor(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	WORD word = x + y * 0x10;
	SetConsoleTextAttribute(handle, word);
}
void setPosition(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord = { x,y };
	SetConsoleCursorPosition(handle, coord);
}
typedef struct _snake {
	int x[50];
	int y[50];
	int speed;
	int count;
	int length;
}Snake;
typedef struct _food {
	int x;
	int y;
}Food;
Snake snakeArray[2] = {
	{ { FRAME + 4 },{ FRAME + 4 },170,0,3 },
	{ { FRAME + 4 },{ FRAME + 4 },170,0,3 }
};
Food foodArray;
Snake* snake;
Food* food;
int i, j;
int deCoord[2];
int Time = 120;
void start()
{
	char  ch;
	int X = FRAME, Y = FRAME + 4;
	setPosition(FRAME, FRAME);
	printf("方向键移动光标,回车键确定(游戏前请关闭输入法!!!!)");
	setPosition(FRAME * 5, FRAME * 4);
	printf("kawayi90hou     贪吃蛇1.0");
	while (1)
	{
		setPosition(FRAME, FRAME + 4);
		if (X == FRAME&&Y == FRAME + 4)setColor(0, 10);
		printf("开始游戏");
		setColor(10, 0);
		setPosition(FRAME, FRAME + 6);
		if (X == FRAME&&Y == FRAME + 6)setColor(0, 10);
		printf("结束游戏");
		ch = getch();
		if (ch == 80)Y += 2;
		if (ch == 72)Y -= 2;
		if (Y < FRAME + 4)Y = FRAME + 6;
		if (Y > FRAME + 6)Y = FRAME + 4;
		if (ch == '\r')
		{
			switch (Y)
			{
			case FRAME + 4:
				system("cls");//清屏    
				break;
			case FRAME + 6:
				exit(0);
				break;
			}
			return;
		}
	}
}
void InitSnake()
{
	snake = &snakeArray[0];
	food = &foodArray;
	for (i = 1; i < snake->length; i++)
	{
		snake->x[i] = snake->x[i - 1] + 2;
		snake->y[i] = snake->y[i - 1];
	}
}
void printSnake()
{
	for (i = 0; i < snake->length; i++)
	{
		setPosition(snake->x[i], snake->y[i]);
		if (i == 0)printf("◆"); //★●■    
		else if (i == snake->length - 1)printf("■");
		else printf("●");
	}
}
void Map()
{
	for (i = 0; i < WIDTH + 1; i += 2)
	{
		setPosition(FRAME + i, FRAME);
		printf("■");
		setPosition(FRAME + i, FRAME + LENGTH);
		printf("■");
	}
	for (i = 0; i < LENGTH + 1; i++)
	{
		setPosition(FRAME, FRAME + i);
		printf("■");
		setPosition(FRAME + WIDTH, FRAME + i);
		printf("■");
	}
	setColor(13, 0);
	setPosition(INFO - 1, 2);
	printf("游戏说明:");
	setPosition(INFO, 4);
	printf("按方向键控制移动");
	setPosition(INFO, 5);
	printf("长按移动键可以加速");
	setPosition(INFO, 6);
	printf("时间为0时游戏结束");
	setPosition(INFO, 7);
	printf("按空格键暂停");
}
void moveSnake()
{
	setPosition(snake->x[0], snake->y[0]);
	printf(" ");
	for (i = 1; i < snake->length; i++)
	{
		snake->x[i - 1] = snake->x[i];
		snake->y[i - 1] = snake->y[i];
	}
}
void printFood()
{
	srand(time(NULL));
	food->x = 2 * (rand() % (WIDTH - 4) / 2) + FRAME + 2;//必须保证所得的x坐标为偶数    
	food->y = 2 * (rand() % (LENGTH - 2) / 2) + FRAME + 1;
	for (i = 0; i < snake->length; i++)
	{
		if (food->x == snake->x[i] && food->y == snake->y[i])
		{
			printFood();//重新随机产生一个食物    
		}
	}
	if (i == snake->length)
	{
		setPosition(food->x, food->y);
		printf("★");
		return;//原来的食物被移动函数自带的空格符号清除    
	}
}
void eatFood()
{
	if (snake->x[snake->length - 1] == food->x&&snake->y[snake->length - 1] == food->y)
	{
		snake->length++;
		for (i = snake->length - 1; i > 0; i--)//要不包括蛇头    
		{
			snake->x[i] = snake->x[i - 1];
			snake->y[i] = snake->y[i - 1];
		}
		snake->x[0] = deCoord[0];//蛇尾坐标倒退一位    
		snake->y[0] = deCoord[1];
		snake->count++;
		if (snake->count % 3 == 0)snake->speed -= 20;
		printFood();
	}
}
int ifDead()
{
	if (snake->x[snake->length - 1] == FRAME || snake->x[snake->length - 1] == FRAME + WIDTH)return 0;
	if (snake->y[snake->length - 1] == FRAME || snake->y[snake->length - 1] == FRAME + LENGTH)return 0;
	for (i = 0; i < snake->length - 1; i++)
	{
		if (snake->x[snake->length - 1] == snake->x[i] && snake->y[snake->length - 1] == snake->y[i])
			return 0;
	}
	return 1;
}
void Info()
{
	//用来获取程序运行的时间    
	setColor(11, 0);
	setPosition(INFO - 1, 15);
	printf("贪吃蛇信息:");
	setPosition(INFO, 17);
	printf("速度:%d", snake->speed);
	setPosition(INFO, 18);
	printf("长度:%d", snake->length);
	setPosition(INFO, 19);
	printf("分数:%d", snake->count);
	setColor(14, 0);
	setPosition(INFO - 1, 21);
	printf("剩余时间:%d", Time);
}
int main()
{
	char key;
	setColor(10, 0);
	start();
	InitSnake();
	//定义的计时变量  
	clock_t start, end;
	start = clock();
	Map();
	Info();
	printSnake();
	printFood();
	key = getch();
	while (1)
	{
		deCoord[0] = snake->x[0];//在吃掉食物前的一个坐标    
		deCoord[1] = snake->y[0];
		j = 0;
		if (kbhit())
		{
			key = getch();
			if (kbhit())
			{
				Sleep(20);
				j = 1;
			}
		}
		switch (key)
		{
		case 72:
			moveSnake();
			snake->y[snake->length - 1]--;
			break;
		case 80:
			moveSnake();
			snake->y[snake->length - 1]++;
			break;
		case 75:
			moveSnake();
			snake->x[snake->length - 1] -= 2;
			break;
		case 77:
			moveSnake();
			snake->x[snake->length - 1] += 2;
			break;
		}
		printSnake();
		eatFood();
		end = clock();//要在程序执行到的那一刻用这个函数  
		Time = 120 - (end - start) / CLOCKS_PER_SEC;
		Info();
		if (j == 1)Sleep(20);
		else Sleep(snake->speed);
		if (ifDead() == 0 || snake->speed == 0)
		{
			setColor(12, 0);
			if (ifDead() == 0)
			{
				setPosition(FRAME + 12, LENGTH / 2);
				printf("G A M E O V E R");
			}
			if (snake->speed == 0)
			{
				setPosition(FRAME + 6, LENGTH / 2);
				printf("GONGRATULATIONS");
			}
			setPosition(FRAME + 8, LENGTH / 2 + 1);
			printf("1:继续  2:重来  3:退出");
			key = getch();
			if (key == '1' || key == '2')
			{
				system("cls");
				setColor(10, 0);
				if (key == '2') snakeArray[0] = snakeArray[1];
				InitSnake();
				Map();
				Info();
				printSnake();
				printFood();
				key = getch();
				continue;
			}
			if (key == '3') break;
		}
	}
	return 0;
}

阅读更多

没有更多推荐了,返回首页