Easyx实现贪吃蛇小游戏

基于Easyx实现的贪吃蛇小游戏(若有不足,还请谅解)

#include<easyx.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>

#define oppo 40

void linecut()//绘制网格
{
	for (int x = 0; x <= 800; x += 40)
	{
		line(x, 0, x, 600);
	}
	for (int y = 0; y <= 600; y+=40)
	{
		line(0, y, 800, y);
	}
}

typedef struct {
	int x;
	int y;
}node;

void Snake(node* snake, int n)//绘制占位为5的蛇
{
	int left, right, top, bottom;
	for (int i = 0; i < n; i++)
	{
		left = snake[i].x * oppo;
		top = snake[i].y * oppo;
		right = (snake[i].x + 1) * oppo;
		bottom = (snake[i].y + 1) * oppo;
		solidrectangle(left, top, right, bottom);
	}
}

enum direction//枚举各项参数
{
	eup,
	edown,
	eleft,
	eright
};

node LetSnakeMove(node* snake, int length, int direction)//使蛇运动
{
	node tail = snake[length - 1];
	for (int i = length - 1; i > 0; i--)
	{
		snake[i] = snake[i - 1];
	}
	node newhead;
	newhead = snake[0];
	if (direction == eup)
	{
		newhead.y--;
	}
	else if (direction == edown)
	{
		newhead.y++;
	}
	else if (direction == eleft)
	{
		newhead.x--;
	}
	else
	{
		newhead.x++;
	}
	snake[0] = newhead;
	return tail;
}

void ControlSnake(enum direction *opp)//通过键盘交互控制·蛇上下左右运动(W、S、D、A)
{
	if (_kbhit() != 0)
	{
		char c = _getch();
		switch (c)
		{
		case'w':
			if (*opp != edown)
				*opp = eup;
			break;
		case 's':
			if (*opp != eup)
				*opp = edown;
			break;
		case 'a':
			if (*opp != eright)
				*opp = eleft;
			break;
		case 'd':
			if (*opp != eleft)
				*opp = eright;
			break;
		}
	}
}

node createfood(node* snake, int length)
{
	node food;
	while (1)
	{
		food.x = rand() % (800 / oppo);
		food.y = rand() % (600 / oppo);

		int i;
		for (i = 0; i < length; i++)
		{
			if (snake[i].x == food.x && snake[i].y == food.y)
			{
				break;
			}
		}
		if (i < length)
			continue;
		else
			break;
	}
	return food;
}

void paintfood(node food)
{
	int left, top, right, bottom;
	left = food.x * oppo;
	top = food.y * oppo;
	right = (food.x + 1) * oppo;
	bottom = (food.y + 1) * oppo;
	setfillcolor(YELLOW);
	solidrectangle(left, top, right, bottom);
	setfillcolor(WHITE);

}

bool isgameover(node*snake,int length)
{
	if (snake[0].x < 0 || snake[0].x>800 / oppo)
		return true;
	if (snake[0].y < 0 || snake[0].y>600 / oppo)
		return true;
	for (int i = 1; i < length; i++)
	{
		if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
			return true;
	}
	return false;
}

void reset(node* snake, int* plength, enum direction* d)
{
	snake[0] = node{ 5,7 };
	snake[1] = node{ 4,7 };
	snake[2] = node{ 3,7 };
	snake[3] = node{ 2,7 };
	snake[4] = node{ 1,7 };
	*plength = 5;
	*d = eright;
}

int main()//主函数
{
	initgraph(800, 600);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();

	node snake[100] = { {5,7},{4,7},{3,7},{2,7},{1,7} };//传入蛇的初始位置
	int length = 5;//蛇的长度
	enum direction d = eright;//设定蛇的初始运动方向
	srand(unsigned int(time(NULL)));//以时间为种子产生随机数
	node food = createfood(snake, length);//定义生成的食物,(以时间为种子随机产生食物的坐标)

	while (1)//死循环执行命令
	{
		cleardevice();
		linecut();
		Snake(snake, length);
		paintfood(food);
		Sleep(500);
		ControlSnake(&d);
		//LetSnakeMove(snake, length, d);
		node lasttail = LetSnakeMove(snake, length, d);
		if (snake[0].x == food.x && snake[0].y == food.y)
		{
			if (length < 100)
			{
				snake[length] = lasttail;
				length++;
			}
			food = createfood(snake, length);
		}

		if (isgameover(snake, length) == true)
		{
			reset(snake, &length, &d);
			food = createfood(snake, length);
		}

		
	}

	getchar();
	closegraph();

	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值