【c语言学习】利用基础语句实现贪吃蛇

这篇博客介绍了一种使用C语言编写贪吃蛇游戏的方法,包括结构体数组存储蛇的位置,二维数组检查碰撞,控制蛇的移动、生长和速度,以及用户输入响应。通过循环保持游戏状态,并使用sleep函数调整游戏速度。文章还提供了完整的代码实现。
摘要由CSDN通过智能技术生成

【C语言学习】利用基础语句实现贪吃蛇

1、利用结构体数组储存蛇身每一个坐标;
2、利用二维数组检查蛇头是否触碰是否、边界或身体;
3、头部打印字符,尾部打印空格实现移动,尾部不打印实现增长;
4、利用循环保持运动状态,当有读入时,根据读入改变运动状态;
5、利用sleep函数控制运动速度;

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

struct SNAKE
{
	int xtail;
	int ytail;
	int* point;
}snake[800];

int map[25][45],
flag = 0,
eat = 0,
c_check = -2,
c,
head = 1,
tail = 0,
hang = 2,
lie = 2,
score = 0;
char c_temp;

void xy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void showmap()
{
	printf("用wasd控制方向:\n");
	printf("------------------------------------------\n");
	for (int i = 2; i <= 21; i++)
	{
		xy(0, i);
		printf("|");
		xy(41, i);
		printf("|");
	}
	printf("\n------------------------------------------\n");
}

void get()
{
	if (_kbhit())
		c_temp = _getch();
	if (c_temp == 'w' || c_temp == 'W')
		c_check = 1;
	if (c_temp == 's' || c_temp == 'S')
		c_check = -1;
	if (c_temp == 'a' || c_temp == 'A')
		c_check = 2;
	if (c_temp == 'd' || c_temp == 'D')
		c_check = -2;
	if (c_check == -c)
		return;
	else
	{
		c = c_check;
		return;
	}
}

void Judge(int* n)
{
	if (*n == 799)
		*n = 0;
	else
		(*n)++;
}

void addfood()
{
	srand(time(0));
	int x = rand() % 20 + 2;
	srand(time(0) + 1);
	int y = rand() % 40 + 1;
	if (map[x][y] == 0)
	{
		map[x][y] = 3;
		xy(y, x);
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
		printf("*");
		eat = 0;
		return;
	}
	else
		addfood();
}

void walkandeat()
{
	*snake[head].point = 1;
	xy(lie, hang);
	snake[head].xtail = lie;
	snake[head].ytail = hang;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	printf("#");
	eat = 1;
	score++;
	xy(0, 23);
	printf("分数:%d\n", score);
}

void walk()
{
	*snake[head].point = 1;
	xy(lie, hang);
	snake[head].xtail = lie;
	snake[head].ytail = hang;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	printf("#");
	*snake[tail].point = 0;
	xy(snake[tail].xtail, snake[tail].ytail);
	printf(" ");
	Judge(&tail);
}

void move()
{
	switch (c)
	{
	case 1:
	{
		if ((hang - 1) >= 2 && (hang - 1) <= 21 && map[hang - 1][lie] != 1)
		{

			if (map[hang - 1][lie] == 3)
			{
				Judge(&head);
				snake[head].point = &map[hang - 1][lie];
				hang--;
				walkandeat();
			}
			else
			{
				Judge(&head);
				snake[head].point = &map[hang - 1][lie];
				hang--;
				walk();
			}
		}
		else
			flag = 1;
		break;
	}
	case -1:
	{
		if ((hang + 1) >= 2 && (hang + 1) <= 21 && map[hang + 1][lie] != 1)
		{
			if (map[hang + 1][lie] == 3)
			{
				Judge(&head);
				snake[head].point = &map[hang + 1][lie];
				hang++;
				walkandeat();
			}
			else
			{
				Judge(&head);
				snake[head].point = &map[hang + 1][lie];
				hang++;
				walk();
			}
		}
		else
			flag = 1;
		break;
	}
	case 2:
	{
		if ((lie - 1) >= 1 && (lie - 1) <= 40 && map[hang][lie - 1] != 1)
		{
			if (map[hang][lie - 1] == 3)
			{
				Judge(&head);
				snake[head].point = &map[hang][lie - 1];
				lie--;
				walkandeat();
			}
			else
			{
				Judge(&head);
				snake[head].point = &map[hang][lie - 1];
				lie--;
				walk();
			}
		}
		else
			flag = 1;
		break;
	}
	case -2:
	{
		if ((lie + 1) >= 1 && (lie + 1) <= 40 && map[hang][lie + 1] != 1)
		{
			if (map[hang][lie + 1] == 3)
			{
				Judge(&head);
				snake[head].point = &map[hang][lie + 1];
				lie++;
				walkandeat();
			}
			else
			{
				Judge(&head);
				snake[head].point = &map[hang][lie + 1];
				lie++;
				walk();
			}
		}
		else
			flag = 1;
		break;
	}
	}
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
}

int main()
{
	printf("请输入一个数字(60-150,越大速度越慢):\n");
	int speed;
	scanf_s("%d", &speed);
	while (!(speed >= 60 && speed <= 150))
	{
		getchar();
		system("cls");
		printf("请输入一个数字(60-150,越大速度越慢):\n");
		scanf_s("%d", &speed);
	}
	printf("游戏开始!");
	Sleep(800);
	system("cls");
	HideCursor();
	showmap();
	snake[0] = { 1,2,&map[2][1] };
	snake[1] = { 2,2,&map[2][2] };
	*(snake[0].point) = 1;
	*(snake[1].point) = 1;
	xy(1, 2);
	printf("#");
	xy(2, 2);
	printf("#");
	addfood();
	while (1)
	{
		if (eat == 1)
		{
			addfood();
		}
		get();
		move();
		if (flag == 1)
		{
			xy(0, 24);
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
			printf("你屎了~_~\n\n");
			system("pause");
			return 0;
		}
		if (c == -1 || c == 1)
			Sleep(speed);
		else
			Sleep(speed / 3 * 2);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值