【EasyX】贪吃蛇小游戏

EasyX 开发的贪吃蛇小游戏

来水一篇博客,计算机组成原理太难了复习着复习着玩上电脑了,加上写这篇大水文花了大概两个小时吧,再也不再复习的时候玩电脑了

EasyX

EasyX是一款图形化开发工具,非常实在有用,很适合开发一些小游戏,小应用,比如说一些简单的抽签软件,我今天开发了一款非常简单的EasyX贪吃蛇小游戏要就直接拿走

核心逻辑1:蛇的移动

贪吃蛇大家都玩过吧,这里我们蛇移动的核心逻辑就是:确定一个当前蛇头的移动方向,我们仅需在蛇头的位置的前面(根据舌头移动方向确定)插入一个新蛇头,接着在舌头的最后删除一个段,我们就实现了蛇的移动,然后一旦触碰到边界/蛇身触发游戏结束。

核心逻辑2:蛇的成长

我们只需要早蛇头插入一个新蛇头即可完成该操作

简单的原理搞清楚我们直接上代码,毕竟是水的博客我就不那个详细说明了,但是我还是有些注释的,不过没那么多嘻嘻嘻,只有一个源文件,
大家如果想要实践一下的话就:
具体步骤(超级简化版):
下载VisualStudio20XX -->
下载EasyX -->
创建空项目 -->
新建源文件Main.cpp -->
复制代码 -->
运行

代码

#include <iostream>
#include <easyx.h>
#include <graphics.h>
#include <conio.h>
#include <vector>
#include <utility>
#include <cstdlib>  
#include <ctime>
#include <Windows.h>
using namespace std;
typedef pair<int, int> PII;

int map[43][32];
vector<PII> Snake;
bool isGaming = true;
PII Fruit;
bool isEating = true;

int moveX = 1, moveY = 0;
void CheckInput()
{
	// 检测上键
	if (GetKeyState(VK_UP) & 0x8000)
	{
		if (moveY != 1) {
			moveX = 0;
			moveY = -1;
		}
	}
	// 检测下键
	else if (GetKeyState(VK_DOWN) & 0x8000)
	{
		if (moveY != -1) {
			moveX = 0;
			moveY = 1;
		}
	}
	// 检测左键
	else if (GetKeyState(VK_LEFT) & 0x8000)
	{
		if (moveX != 1) {
			moveX = -1;
			moveY = 0;
		}
	}
	// 检测右键
	else if (GetKeyState(VK_RIGHT) & 0x8000)
	{
		if (moveX != -1) {
			moveX = 1;
			moveY = 0;
		}
	}
	// 检测W键
	else if (GetKeyState('W') & 0x8000)
	{
		if (moveY != 1) {
			moveX = 0;
			moveY = -1;
		}
	}
	// 检测S键
	else if (GetKeyState('S') & 0x8000)
	{
		if (moveY != -1) {
			moveX = 0;
			moveY = 1;
		}
	}
	// 检测A键
	else if (GetKeyState('A') & 0x8000)
	{
		if (moveX != 1) {
			moveX = -1;
			moveY = 0;
		}
	}
	// 检测D键
	else if (GetKeyState('D') & 0x8000)
	{
		if (moveX != -1) {
			moveX = 1;
			moveY = 0;
		}
	}
}
void DrawMap()
{
	setlinecolor(WHITE);
	int x1 = 0;
	for (int i = 0; i < 43; i++)
	{
		line(x1, 0, x1, 640);
		x1 += 20;
	}
	int y1 = 0;
	for (int i = 0; i < 32; i++)
	{
		line(0, y1, 860, y1);
		y1 += 20;
	}
}

void RandomFruit()
{
	int x = rand() % 43;
	int y = rand() % 32;
	Fruit.first = x;
	Fruit.second = y;
}

void DrawSnake()
{
	setfillcolor(BLUE);
	for (int i = 0; i < Snake.size(); i++)
	{	
		int x = Snake[i].first * 20 + 10;
		int y = Snake[i].second * 20 + 10;
		fillcircle(x, y, 10);
	}
}
void DrawFruit()
{
	setfillcolor(RED);
	fillcircle(Fruit.first * 20 + 10, Fruit.second * 20 + 10, 10);
}
void SnakeGrow()
{
	PII newHead = Snake[0];  // 复制当前蛇头

	// 修正移动方向计算
	newHead.first += moveX;
	newHead.second += moveY;

	Snake.insert(Snake.begin(), newHead);
}
void SnakeMove()
{
	PII newHead = Snake[0];  // 复制当前蛇头

	// 根据新的 moveX 和 moveY 更新蛇头的位置
	newHead.first += moveX;
	newHead.second += moveY;

	// 检查是否撞墙
	if (newHead.first < 0 || newHead.first >= 43 ||
		newHead.second < 0 || newHead.second >= 32)
	{
		isGaming = false;  // 游戏结束
		return;
	}

	// 检查是否撞到自己
	for (int i = 1; i < Snake.size(); i++)
	{
		if (newHead == Snake[i])
		{
			isGaming = false;
			return;
		}
	}

	// 检查是否吃到食物
	if (newHead == Fruit)
	{
		isEating = true;
		SnakeGrow();  // 调用增长函数
		return;
	}

	// 插入新头部并删除尾部
	Snake.insert(Snake.begin(), newHead);
	Snake.pop_back();
}


int main()
{
	initgraph(860, 640);
	srand((unsigned)time(NULL));
	BeginBatchDraw();

	// 初始化蛇身(3节)
	Snake.push_back({ 22, 16 });  // 蛇头
	Snake.push_back({ 21, 16 });  // 蛇身
	Snake.push_back({ 20, 16 });  // 蛇尾

	RandomFruit();

	while (isGaming)
	{
		cleardevice();

		// 处理输入
		CheckInput();

		// 游戏逻辑
		if (isEating)
		{
			map[Fruit.first][Fruit.second] = 0;
			RandomFruit();
			map[Fruit.first][Fruit.second] = 1;
			isEating = false;
		}

		SnakeMove();

		// 绘制
		DrawMap();
		DrawSnake();
		DrawFruit();

		FlushBatchDraw();
		Sleep(100);  // 调整移动速度
	}

	EndBatchDraw();
	closegraph();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值