C语言贪吃蛇

新手自学C,第一次写游戏,蛮兴奋,记录一下

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

//辅助
#define mapheight 25
#define mapwidth 60
#define shejieshu 50

//辅助函数,光标移动
void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(handle, coord);

}




struct       //食物
{
	int x;
	int y;
}food;

//蛇
struct 
{
	int x[shejieshu];
	int y[shejieshu];
	int len;
	int v;
}she;

int fangxiang = 'A';
int chuchang = 0;
int grade = 0;

void map()
{
	srand((unsigned int)time(NULL));      //随机函数种子

	for (int i = 0; i <= mapheight; i++)
	{
		gotoxy(0, i);
		printf("□");
		gotoxy(mapwidth, i);
		printf("□");
	}
	for (int j = 0;  j<= mapwidth; j+=2)
	{
		gotoxy(j, 0);
		printf("□");
		gotoxy(j, mapheight);
		printf("□");
	}
	she.len = 3;
	she.v ;
	she.x[0] = mapwidth / 2;
	she.y[0] = mapheight / 2;
	gotoxy(she.x[0], she.y[0]);
	printf("■");
	for (int k = 1; k < she.len; k++)
	{
		she.x[k] = she.x[k - 1] + 2;
		she.y[k] = she.y[k - 1];
		gotoxy(she.x[k], she.y[k]);
		printf("■");
	}

	while (1)
	{

		food.x = rand() % (mapwidth - 4) + 2;
		food.y = rand() % (mapheight - 2) + 1;
		if (food.x % 2 == 0)
		{
			break;
		}
	}


	gotoxy(food.x, food.y);
	printf("■");

	
}

	

void makefood()
{
	if (she.x[0] == food.x&&she.y[0] == food.y)
	{
		srand((unsigned int)time(NULL));
		while (1)
		{
			int flag = 1;

			while (1)
			{
				food.x = rand() % (mapwidth - 4) + 2;
				food.y = rand() % (mapheight - 2) + 1;
				if (food.x % 2 == 0)
				{
					break;
				}
			}

			int k;
			for ( k = 0; k < she.len; k++);
			{
				
				if (she.y[k] == food.y&&she.x[k] == food.x||food.x % 2 != 0)
				{
					flag = 1;
					break;
				}
				if (flag == 0)
				{
					break;
				}
			}
			gotoxy(food.x, food.y);
			printf("■");

			she.len++;
			//chuchang = 1;
			grade = grade + 1;
			break;

		}
	}
	
}
void anjian()
{
	int chaoxiang;
	if (_kbhit())     //获取键盘信息
	{
		fflush(stdin);
		chaoxiang = _getch();
		if (fangxiang != 'A'&&chaoxiang == 'D')	
		{
			fangxiang = chaoxiang;
		}
		if (fangxiang != 'W'&&chaoxiang == 'S')
		{
			fangxiang = chaoxiang;
		}
		if (fangxiang != 'S'&&chaoxiang == 'W')
		{
			fangxiang = chaoxiang;
		}
		if (fangxiang != 'D'&&chaoxiang == 'A')
		{
			fangxiang = chaoxiang;
		}
	}
	if (!chuchang)
	{
		gotoxy(she.x[she.len - 1], she.y[she.len - 1]);
		printf("  ");
	}

	for (int i = she.len - 1; i > 0; i--)
	{
		she.x[i] = she.x[i - 1];
		she.y[i] = she.y[i - 1];

	}



	switch (fangxiang)
	{
		case 'W':she.y[0]--; break;
		case 'S':she.y[0]++; break;
		case 'A':she.x[0]-= 2; break;
		case 'D':she.x[0]+= 2; break;
	}



	gotoxy(she.x[0], she.y[0]);
	printf("■");
	chuchang = 0;
	gotoxy(mapwidth + 2, 0);

}
int shezhangtai()
{
	if (she.y[0] == 0 || she.y[0] == mapheight || she.x[0] == 0 || she.x[0] == mapwidth)
	{
		return 0;
	}

	for (int k = 1; k < she.len; k++)
	{
		if (she.x[0] == she.x[k] && she.y[k] == she.y[0])
			return 0;
	}

	return 1;
}

int main()
{
	int c ;
	
	while (1)
	{
		printf("请选择速度,1为普通,2为进阶,3为困难,4为神仙\n");
		scanf_s("%d", &c);

		switch (c)
		{
		case 1:she.v = 500; break;
		case 2:she.v = 300; break;
		case 3:she.v = 100; break;
		case 4:she.v = 50;  break;
		default:printf("请输入1,2,3中的一个数字\n");
		}
		if (c == 1 || c == 2 || c == 3 || c == 4)
		{
			break;
		}
	}

	printf("请打开大写");
	system("pause");
	
	system("cls");

	map();

	while (1)
	{
		gotoxy(mapwidth + 2, 0);
		printf("得分:%d", grade);
		makefood();
		Sleep(she.v);
		anjian();
		if (!shezhangtai())
		{
			break;
		}
		
	}

	gotoxy(mapwidth , mapheight +1);
	printf("GAME OVER\n");
	system("pause");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值