【游戏引擎Easy2D实战】选择关卡场景示例

哈喽大家好,我是iecne,本期为大家带来的是CPP/C++游戏编写 —— 选择关卡场景示例 ,包教包会,快来看看吧!

本片文章所用到的是Easy2D引擎,快点赞收藏关注评论支持以下博主,蟹蟹

// 编译环境:Visual Studio 2019 / Easy2D v2.0.4
// 项目类型:Win32 Console Application

首先说明本文所引用的资料出处于,注重原创     

LevelSelectExample-Easy2D 发行版 - Gitee.comz

//---------------------------------------------------------
// 程序名称:关卡选择场景
// 作者:Nomango
// 编译环境:Visual Studio 2019 / Easy2D v2.0.0-beta7
// 编译环境:Visual Studio 2019 / Easy2D v2.0.4
// 项目类型:Win32 Console Application
//---------------------------------------------------------

 


目录

一.主函数说明

在 Win32 程序下使用 Easy2D

二.头文件与函数声明

三.主函数

四.函数

1.创建场景并进入

2.关卡

Jungle 

CHINA

Egypt

3.加载按钮

4.移动关卡面板

5.点击按钮

左LEFT

右RIGHT

五.效果图片

六.总结


一.主函数说明

在 Win32 程序下使用 Easy2D

引擎在 Win32 应用程序和 Win32 控制台程序下的代码是一样的,只是两种模式下主函数的写法不同。

写控制台程序时,只需声明 main 函数就可以:

int main()
{
	return 0;
}

写 Win32 程序时,主函数变得有些复杂,但我们可以把它和控制台下的 main 函数一样使用:

int WINAPI WinMain(
	HINSTANCE hInstance, 
	HINSTANCE hPrevInstance, 
	LPSTR lpCmdLine, 
	int nCmdShow
)
{
	return 0;
}

虽然引擎支持在控制台程序下工作,但是不推荐这样使用,因为游戏并不需要控制台。


二.头文件与函数声明

#include <easy2d/easy2d.h>

using namespace easy2d;


//---------------------------------------------------------
// 函数声明
//---------------------------------------------------------

void EnterScene();		
// 创建场景并进入

void InitJungle();		
// 加载 Jungle 关卡

void InitChina();		
// 加载 China 关卡

void InitEgypt();		
// 加载 Egypt 关卡

void InitButton();		
// 加载按钮

void MovePanel();		
// 移动关卡面板

void ClickLeft();		
// 点击左按钮

void ClickRight();		
// 点击右按钮



//---------------------------------------------------------
// 全局变量声明
//---------------------------------------------------------

Scene*	g_Scene;		// 场景

Node*	g_Panel;		// 关卡面板

int		g_Level;		// 当前选中的关卡数

Button* g_LeftButton;	// 左按钮

Button* g_RightButton;	// 右按钮

三.主函数

刚刚已经讲了主函数哈

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	if (Game::init(L"关卡选择", 480, 720))
	{
		// 设置节点默认中心点
		Node::setDefaultAnchor(0.5f, 0.5f);

		// 创建场景并进入
		EnterScene();

		Game::start();
	}

	Game::destroy();
	return 0;
}

游戏开始->结束


四.函数

重点部分哈

1.创建场景并进入

void EnterScene()
{
	// 创建一个空场景
	g_Scene = gcnew Scene;

	// 设置当前关卡为 0
	g_Level = 0;

	// 创建背景图对象
	auto background = gcnew Sprite(L"res/LS00.png");

	// 背景图居中显示
	background->setPos(Window::getSize() / 2);

	// 缩小背景图
	background->setScale(1.0f, 1.0f);

	// 将图片添加到场景中
	g_Scene->addChild(background);

	// 三个关卡图片合并为 panel 节点
	g_Panel = gcnew Node();
	g_Scene->addChild(g_Panel);

	// 加载关卡图片
	InitJungle();
	InitChina();
	InitEgypt();

	// 添加按钮
	InitButton();

	// 进入场景
	SceneManager::enter(g_Scene);
}

2.关卡

Jungle 

void InitJungle()
{
	// 创建图片
	auto JungleImage = gcnew Sprite(L"res/LS13.png");
	JungleImage->setScale(1.5f, 1.5f);
	JungleImage->setPosY(Window::getHeight() / 2 - 60);

	// 创建文字
	auto JungleText = gcnew Sprite(L"res/LS10.png");
	JungleText->setScale(1.5f, 1.5f);
	JungleText->setPosY(Window::getHeight() - 195);

	// 图片居中
	JungleImage->setPosX(Window::getWidth() / 2);
	JungleText->setPosX(JungleImage->getPosX());

	// 将两个精灵添加到 panel 节点
	g_Panel->addChild(JungleImage);
	g_Panel->addChild(JungleText);
}

CHINA

void InitChina()
{
	// 创建图片
	auto ChinaImage = gcnew Sprite(L"res/LS14.png");
	ChinaImage->setScale(1.5f, 1.5f);
	ChinaImage->setPosY(Window::getHeight() / 2 - 30);

	// 创建文字
	auto ChinaText = gcnew Sprite(L"res/LS11.png");
	ChinaText->setScale(1.5f, 1.5f);
	ChinaText->setPosY(Window::getHeight() - 195);

	// 图片居中
	ChinaImage->setPosX(Window::getWidth() / 2 + Window::getWidth());
	ChinaText->setPosX(ChinaImage->getPosX());


	// 将两个精灵添加到 panel 节点
	g_Panel->addChild(ChinaImage);
	g_Panel->addChild(ChinaText);
}

Egypt

void InitEgypt()
{
	// 创建图片
	auto EgyptImage = gcnew Sprite(L"res/LS15.png");
	EgyptImage->setScale(1.5f, 1.5f);
	EgyptImage->setPosY(Window::getHeight() / 2 - 30);

	// 设置文字位置
	auto EgyptText = gcnew Sprite(L"res/LS12.png");
	EgyptText->setScale(1.5f, 1.5f);
	EgyptText->setPosY(Window::getHeight() - 195);

	// 图片居中
	EgyptImage->setPosX(Window::getWidth() / 2 + Window::getWidth() * 2);
	EgyptText->setPosX(EgyptImage->getPosX());

	// 将两个精灵添加到 panel 节点
	g_Panel->addChild(EgyptImage);
	g_Panel->addChild(EgyptText);
}

3.加载按钮

void InitButton()
{
	// 创建开始按钮
	auto startBtn = gcnew Button;
	startBtn->setScale(1.5f, 1.5f);

	// 设置按钮图片
	startBtn->setNormal(gcnew Sprite(L"res/LS07.png"));

	// 设置按钮被选中时的图片
	startBtn->setSelected(gcnew Sprite(L"res/LS08.png"));

	// 设置按钮禁用时的图片
	startBtn->setDisabled(gcnew Sprite(L"res/LS09.png"));

	// 设置按钮位置
	startBtn->setPos(Window::getWidth() / 2, Window::getHeight() - 120);

	// 添加开始按钮
	g_Scene->addChild(startBtn);

	// 创建左选按钮
	g_LeftButton = gcnew Button;
	g_LeftButton->setScale(1.5f, 1.5f);
	g_LeftButton->setNormal(gcnew Sprite(L"res/LS01.png"));
	g_LeftButton->setSelected(gcnew Sprite(L"res/LS02.png"));
	g_LeftButton->setDisabled(gcnew Sprite(L"res/LS03.png"));
	g_LeftButton->setPosX(g_LeftButton->getWidth() / 2);
	g_LeftButton->setPosY((Window::getHeight() - g_LeftButton->getHeight()) / 2);
	g_LeftButton->setEnable(false);
	g_Scene->addChild(g_LeftButton);

	// 点击左按钮执行 ClickLeft 函数
	g_LeftButton->setClickFunc(ClickLeft);

	// 创建右选按钮
	g_RightButton = gcnew Button();
	g_RightButton->setScale(1.5f, 1.5f);
	g_RightButton->setNormal(gcnew Sprite(L"res/LS04.png"));
	g_RightButton->setSelected(gcnew Sprite(L"res/LS05.png"));
	g_RightButton->setDisabled(gcnew Sprite(L"res/LS06.png"));
	g_RightButton->setPosX(Window::getWidth() - g_RightButton->getWidth() / 2);
	g_RightButton->setPosY((Window::getHeight() - g_RightButton->getHeight()) / 2);
	g_Scene->addChild(g_RightButton);

	// 点击右按钮执行 ClickRight 函数
	g_RightButton->setClickFunc(ClickRight);
}

4.移动关卡面板

void MovePanel()
{
	// 创建目标点
	auto point = Point(-Window::getWidth() * g_Level, 0);

	// 创建移动动画
	auto action = gcnew MoveTo(0.5f, point);

	// 停止 panel 的所有动画
	g_Panel->stopAllActions();

	// 执行新动画
	g_Panel->runAction(action);
}

5.点击按钮

左LEFT

void ClickLeft()
{
	// level 为 1 时,禁用左选按钮
	if (g_Level == 1)
	{
		g_LeftButton->setEnable(false);
	}

	// 按下左选按钮后,把右选按钮启动
	g_RightButton->setEnable(true);
	
	// level 减一
	g_Level--;
	
	// 移动 panel
	MovePanel();
}

右RIGHT

void ClickRight()
{
	// level 为 1 时,禁用右选按钮
	if (g_Level == 1)
	{
		g_RightButton->setEnable(false);
	}

	// 按下右选按钮后,把左选按钮启动
	g_LeftButton->setEnable(true);

	// level 加一
	g_Level++;

	// 移动 panel
	MovePanel();
}

五.效果图片

9d17721a70f74ab2aeb78eac94df058e.png


六.总结

本节课主要是讲解了Easy2D引擎实现的选择关卡场景示例,至此选择关卡场景示例的思想博主已经真真切切彻彻底底分享完了,相信大家对这个逻辑有了一定的理解,大家可以自己动手敲敲代码,感受一下

包教包会,帅的人已然点赞收藏关注,而丑的人还在犹豫,被犹豫了,快三连吧!


  感谢每一个观看本篇文章的朋友,更多精彩敬请期待:iecne的博客_CSDN博客-C++保姆级入门教程领域博主

文章存在借鉴,如有侵权请联系修改删除!dd8b4788b2554df4bec4b8202395cad2.gif

  • 108
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 189
    评论
非常感谢您的提问!以下是使用 easy2d 编写单机版俄罗斯方块的代码: ```lua -- 导入 easy2d 模块 local easy2d = require("easy2d") -- 定义方块的形状 local shapes = { {0, 0, 0, 0, 1, 1, 1, 1}, -- I 形 {0, 0, 1, 0, 1, 1, 0, 1}, -- O 形 {0, 0, 1, 0, 2, 0, 2, 1}, -- L 形 {0, 0, 1, 0, 2, 0, 2, -1}, -- J 形 {0, 0, 1, 0, 0, 1, 1, 1}, -- Z 形 {0, 0, 0, 1, 1, 1, 1, 0}, -- S 形 {0, 0, 1, 0, 1, 1, 2, 1}, -- T 形 } -- 定义方块的颜色 local colors = { {0, 1, 1}, -- 青色 {1, 1, 0}, -- 黄色 {1, 0.5, 0}, -- 橙色 {0, 0, 1}, -- 蓝色 {1, 0, 0}, -- 红色 {0, 1, 0}, -- 绿色 {0.5, 0, 1}, -- 紫色 } -- 定义游戏区域的大小 local width, height = 10, 20 -- 定义方块的大小 local block_size = 30 -- 定义游戏区域的左上角坐标 local x0, y0 = 100, 100 -- 定义当前方块的位置和形状 local cur_x, cur_y, cur_shape = 0, 0, 0 -- 定义游戏区域的状态 local board = {} -- 初始化游戏区域的状态 for i = 1, height do board[i] = {} for j = 1, width do board[i][j] = 0 end end -- 随机生成一个新的方块 local function new_block() cur_x, cur_y = math.floor(width / 2), 1 cur_shape = math.random(#shapes) end -- 判断当前方块是否可以移动到指定位置 local function can_move(x, y, shape) for i = 1, 8, 2 do local xx, yy = x + shapes[shape][i], y + shapes[shape][i + 1] if xx < 1 or xx > width or yy < 1 or yy > height or board[yy][xx] ~= 0 then return false end end return true end -- 将当前方块放到游戏区域中 local function put_block() for i = 1, 8, 2 do local x, y = cur_x + shapes[cur_shape][i], cur_y + shapes[cur_shape][i + 1] board[y][x] = cur_shape end end -- 消除满行 local function clear_lines() local lines = {} for i = 1, height do local full = true for j = 1, width do if board[i][j] == 0 then full = false break end end if full then table.insert(lines, i) end end for i = #lines, 1, -1 do table.remove(board, lines[i]) table.insert(board, 1, {}) for j = 1, width do board[1][j] = 0 end end end -- 绘制游戏区域 local function draw_board() for i = 1, height do for j = 1, width do if board[i][j] ~= 0 then easy2d.draw_rect(x0 + (j - 1) * block_size, y0 + (i - 1) * block_size, block_size, block_size, colors[board[i][j]]) end end end end -- 绘制当前方块 local function draw_block() for i = 1, 8, 2 do local x, y = cur_x + shapes[cur_shape][i], cur_y + shapes[cur_shape][i + 1] easy2d.draw_rect(x0 + (x - 1) * block_size, y0 + (y - 1) * block_size, block_size, block_size, colors[cur_shape]) end end -- 更新游戏状态 local function update() -- 移动方块 if easy2d.is_key_pressed("left") and can_move(cur_x - 1, cur_y, cur_shape) then cur_x = cur_x - 1 end if easy2d.is_key_pressed("right") and can_move(cur_x + 1, cur_y, cur_shape) then cur_x = cur_x + 1 end if easy2d.is_key_pressed("down") and can_move(cur_x, cur_y + 1, cur_shape) then cur_y = cur_y + 1 end -- 旋转方块 if easy2d.is_key_pressed("up") then local new_shape = cur_shape % #shapes + 1 if can_move(cur_x, cur_y, new_shape) then cur_shape = new_shape end end -- 自动下落方块 if can_move(cur_x, cur_y + 1, cur_shape) then cur_y = cur_y + 1 else put_block() clear_lines() new_block() end end -- 绘制游戏界面 function easy2d.draw() easy2d.clear(1, 1, 1) draw_board() draw_block() end -- 启动游戏循环 new_block() easy2d.run(update) ``` 希望这个代码对您有所帮助!如果您有任何问题,请随时问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值