c_贪吃蛇

c_贪吃蛇

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
#include <conio.h>

#define WIDTH 60
#define HEIGHT 25

struct BODY //记录蛇身体的坐标
{
	int X;
	int Y;
};
struct Snake//蛇结构
{
	int size;//蛇长度
	struct BODY body[WIDTH*HEIGHT];
};
int foot[2];
struct Snake snake;
void set_wall();//设置墙
void set_foot();//设置食物
void set_sanke();//设置蛇
void set_handle_position();//设置光标位置
void PlayGame();//开始游戏
int move_x=0;//蛇移动偏移量
int move_y=0;
int free_x = 0;//蛇尾清空
int free_y = 0;
void set_wall()
{
	for (int i = 0; i <= HEIGHT; i++)
	{
		for (int j = 0; j <= WIDTH; j++)
		{
			if (i == HEIGHT)
				putchar(22);
			else if (j == WIDTH)
				putchar(21);
			else
				putchar(' ');
		}
		putchar(10);
	}
}
void set_foot() {//随机生成食物
	foot[0] = rand() % WIDTH;
	foot[1] = rand() % HEIGHT;
}
void set_sanke() {//初始化蛇和蛇的位置
	snake.size = 2;
	snake.body[0].X = WIDTH / 2;
	snake.body[0].Y = HEIGHT / 2;
	snake.body[1].X = WIDTH / 2-1;
	snake.body[1].Y = HEIGHT / 2;
}
void set_handle_position() {
	COORD coord;
	coord.X = free_x;
	coord.Y = free_y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	putchar(' ');
	//蛇
	for (int i = 0; i < snake.size; i++)
	{
		coord.X = snake.body[i].X;
		coord.Y = snake.body[i].Y;
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);//固定格式 设置光标位置
		if (i==0)//蛇头
		{
			putchar(1);//ASCLL码值
		}
		else//蛇身
		{
			putchar(15);
		}
	}

	//食物
	coord.X = foot[0];
	coord.Y = foot[1];
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);//固定格式 设置
	putchar(2);
}
void PlayGame()
{
	char index = 'd';
	char pos = index;
	//判断蛇是否撞墙
	while (snake.body[0].X>=0&&snake.body[0].X<=WIDTH&&snake.body[0].Y >= 0 && snake.body[0].Y <= HEIGHT)
	{
		set_handle_position();//绘制界面
		while (_kbhit())//检测当前是否有键盘输入 有返回非0 否返回0
		{
			index = _getch();//无显示的从键盘输入获取字符 返回当前输入的字符
			if ((index=='d'&& pos !='a')||(index == 'a' && pos != 'd') || (index == 's' && pos != 'w') || (index == 'w' && pos != 's'))//检测是否反方向
			{
				pos = index;
			}
			else
			{
				index = pos;
			}
		}
		switch (index)//蛇方向移动
		{
		case 'd': move_x = 1; move_y = 0; break;
		case 'a': move_x = -1;move_y = 0; break;
		case 's': move_x = 0; move_y = 1; break;
		case 'w': move_x = 0; move_y = -1; break;
		}
		for (int i = 1; i < snake.size; i++)//判断是否和自身碰撞
		{
			if (snake.body[0].X == snake.body[i].X&&snake.body[0].Y == snake.body[i].Y)
			{
				return;
			}
		}

		if (snake.body[0].X==foot[0]&& snake.body[0].Y==foot[1])//判断与食物碰撞
		{
			snake.size++;
			set_foot();//更新食物
		}
		free_x = snake.body[snake.size - 1].X;//蛇尾清空
		free_y = snake.body[snake.size - 1].Y;
		for (int i = snake.size-1; i >0; i--)//蛇身子往蛇头方向移动
		{
			snake.body[i].X = snake.body[i - 1].X;//把前一位的值赋值给后一位
			snake.body[i].Y = snake.body[i - 1].Y;
		}
		snake.body[0].X += move_x;//蛇头移动
		snake.body[0].Y += move_y;
		Sleep(150);//睡眠(毫秒)
	}
}
int main() {
	srand((size_t)time(NULL));//设置随机数种子
	CONSOLE_CURSOR_INFO cci; //隐藏光标
	cci.dwSize = sizeof(cci);
	cci.bVisible = FALSE;//隐藏
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
	set_wall();
	set_foot();
	set_sanke();
	PlayGame();
	return 0;
}

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值