Skip to content

Commit

Permalink
v2.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Nomango committed Oct 22, 2022
1 parent 7a21581 commit 7b821d5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 60 deletions.
84 changes: 84 additions & 0 deletions PushBox/Buttons.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "Buttons.h"
#include "Data.h"

static DrawingStyle buttonNormalStyle = DrawingStyle(Color::Black);
static DrawingStyle buttonHighlightStyle = DrawingStyle(Color::Blue);
static DrawingStyle buttonDisabledStyle = DrawingStyle(Color::LightGray);

MyTextButton::MyTextButton(const String& text, Function<void()> callback)
{
this->setText(text);
this->setFont(Font(L"新宋体", 28, Font::Weight::Bold));
this->setDrawingStyle(buttonNormalStyle);

// 添加按钮监听器
auto lis = gcnew ButtonListener([=](ButtonEvent evt) {
if (enabled) {
if (evt == ButtonEvent::MouseEntered) {
// 鼠标放在按钮上,变色
this->setDrawingStyle(buttonHighlightStyle);
}
if (evt == ButtonEvent::MouseExited) {
// 鼠标移出按钮,恢复颜色
this->setDrawingStyle(buttonNormalStyle);
}
if (evt == ButtonEvent::Clicked) {
// 点击鼠标
callback();
}
}
});
this->addListener(lis);
}

void MyTextButton::setEnable(bool enable)
{
this->enabled = enable;
if (!enable)
{
this->setDrawingStyle(buttonDisabledStyle);
}
else
{
this->setDrawingStyle(buttonNormalStyle);
}
}

SoundButton::SoundButton()
{
static auto sound = L"res/background.wav";

lis = gcnew ToggleButtonListener(g_SoundOpen, [=](ButtonEvent evt, bool state) {
if (evt == ButtonEvent::Clicked) {
if (state)
{
// 继续背景音乐
MusicPlayer::resume(sound);
}
else
{
// 暂停背景音乐
MusicPlayer::pause(sound);
}
g_SoundOpen = state;
this->setState(state);
// 保存信息到文件
Data::saveBool(L"sound", g_SoundOpen);
}
});
this->addListener(lis);
this->updateStatus();
}

void SoundButton::updateStatus()
{
this->setState(g_SoundOpen);
lis->setState(g_SoundOpen);
}

void SoundButton::setState(bool state)
{
static auto soundon = L"res/soundon.png";
static auto soundoff = L"res/soundoff.png";
this->open(state ? soundon : soundoff);
}
27 changes: 27 additions & 0 deletions PushBox/Buttons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <easy2d/easy2d.h>
using namespace easy2d;

class MyTextButton : public Text
{
public:
MyTextButton(const String& text, Function<void()> callback);

void setEnable(bool enable);

private:
bool enabled = true;
};

class SoundButton : public Sprite
{
public:
SoundButton();

void updateStatus();

private:
void setState(bool state);

ToggleButtonListener* lis = nullptr;
};
6 changes: 6 additions & 0 deletions PushBox/PlayScene.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "PlayScene.h"
#include "SuccessScene.h"
#include "Buttons.h"

PlayScene::PlayScene(int level)
{
Expand Down Expand Up @@ -32,6 +33,11 @@ PlayScene::PlayScene(int level)
restartText->setPos(520, 290);
this->addChild(restartText);

// 声音开关按钮
auto soundBtn = gcnew SoundButton();
soundBtn->setPos(520, 330);
this->addChild(soundBtn);

// 添加地图部分
mapLayer = gcnew Node();
this->addChild(mapLayer);
Expand Down
2 changes: 1 addition & 1 deletion PushBox/PlayScene.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <easy2d/easy2d.h>
#include "Data.h"
#include <easy2d/easy2d.h>
using namespace easy2d;

class PlayScene :
Expand Down
2 changes: 2 additions & 0 deletions PushBox/PushBox.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Buttons.cpp" />
<ClCompile Include="Data.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="PlayScene.cpp" />
<ClCompile Include="StartScene.cpp" />
<ClCompile Include="SuccessScene.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Buttons.h" />
<ClInclude Include="Data.h" />
<ClInclude Include="PlayScene.h" />
<ClInclude Include="resource.h" />
Expand Down
6 changes: 6 additions & 0 deletions PushBox/PushBox.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="SuccessScene.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Buttons.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Data.h">
Expand All @@ -47,6 +50,9 @@
<ClInclude Include="SuccessScene.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Buttons.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="logo.ico">
Expand Down
62 changes: 12 additions & 50 deletions PushBox/StartScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,39 @@ StartScene::StartScene()
// 修改节点默认中心点位置
Node::setDefaultAnchor(0.5, 0.5);

// 创建字体
auto fontNormal = Font(L"新宋体", 28, Font::Weight::Bold);
auto fontSelect = Font(L"新宋体", 28, Font::Weight::Bold);
auto fontDisable = Font(L"新宋体", 28, Font::Weight::Bold);

// 创建样式
auto styleNormal = DrawingStyle(Color::Black);
auto styleSelect = DrawingStyle(Color::Blue);
auto styleDisable = DrawingStyle(Color::LightGray);

// 创建开始游戏按钮
auto startBtn = gcnew Button();
startBtn->setNormal(gcnew Text(L"新游戏", fontNormal, styleNormal));
startBtn->setMouseOver(gcnew Text(L"新游戏", fontSelect, styleSelect));
auto startBtn = gcnew MyTextButton(L"新游戏", [=]() {
this->Start();
});
// 设置按钮位置
startBtn->setPos(Window::getWidth() / 2, 260);
// 设置按钮回调函数
startBtn->setClickFunc(std::bind(&StartScene::Start, this));
this->addChild(startBtn);

// 继续游戏按钮
resumeBtn = gcnew Button();
resumeBtn->setNormal(gcnew Text(L"继续关卡", fontNormal, styleNormal));
resumeBtn->setMouseOver(gcnew Text(L"继续关卡", fontSelect, styleSelect));
resumeBtn->setDisabled(gcnew Text(L"继续关卡", fontDisable, styleDisable));
resumeBtn = gcnew MyTextButton(L"继续关卡", [=]() {
this->Continue();
});
// 设置按钮位置
resumeBtn->setPos(Window::getWidth() / 2, 300);
// 若当前关卡是第一关,禁用这个按钮
if (g_CurrentLevel == 1)
{
resumeBtn->setEnable(false);
}
// 设置按钮回调函数
resumeBtn->setClickFunc(std::bind(&StartScene::Continue, this));
this->addChild(resumeBtn);

// 创建退出按钮
auto exitBtn = gcnew Button();
exitBtn->setNormal(gcnew Text(L"退出", fontNormal, styleNormal));
exitBtn->setMouseOver(gcnew Text(L"退出", fontSelect, styleSelect));
auto exitBtn = gcnew MyTextButton(L"退出", [=]() {
this->Exit();
});
// 设置按钮位置
exitBtn->setPos(Window::getWidth() / 2, 340);
// 设置按钮回调函数
exitBtn->setClickFunc(std::bind(&StartScene::Exit, this));
this->addChild(exitBtn);

// 创建声音开关按钮
soundBtn = gcnew Button(gcnew Sprite(g_SoundOpen ? L"res/soundon.png" : L"res/soundoff.png"));
soundBtn = gcnew SoundButton();
// 设置按钮位置
soundBtn->setPos(50, 50);
// 设置按钮回调函数
soundBtn->setClickFunc(std::bind(&StartScene::Sound, this));
this->addChild(soundBtn);

// 恢复节点默认中心点位置
Expand All @@ -80,6 +61,8 @@ void StartScene::onEnter()
{
resumeBtn->setEnable(true);
}
// 更新音乐按钮状态
soundBtn->updateStatus();
}

void StartScene::Start()
Expand All @@ -103,24 +86,3 @@ void StartScene::Exit()
// 退出游戏
Game::quit();
}

void StartScene::Sound()
{
// 获取按钮状态
if (!g_SoundOpen)
{
// 继续背景音乐
MusicPlayer::resume(L"res/background.wav");
soundBtn->setNormal(gcnew Sprite(L"res/soundon.png"));
g_SoundOpen = true;
}
else
{
// 暂停背景音乐
MusicPlayer::pause(L"res/background.wav");
soundBtn->setNormal(gcnew Sprite(L"res/soundoff.png"));
g_SoundOpen = false;
}
// 保存信息到文件
Data::saveBool(L"sound", g_SoundOpen);
}
13 changes: 5 additions & 8 deletions PushBox/StartScene.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <easy2d/easy2d.h>
using namespace easy2d;
#pragma once
#include "Buttons.h"

class StartScene :
public Scene
Expand All @@ -17,12 +17,9 @@ class StartScene :
// 退出游戏
void Exit();

// 打开或关闭声音
void Sound();

protected:
// 声音开关
Button* soundBtn;
// 继续游戏按钮
Button* resumeBtn;
MyTextButton* resumeBtn;
// 音乐开关按钮
SoundButton* soundBtn;
};
2 changes: 1 addition & 1 deletion PushBox/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//---------------------------------------------------------
// 程序名称:推箱子
// 作者:Nomango
// 编译环境:Visual Studio 2017 / Easy2D v2.0.4
// 编译环境:Visual Studio 2017 / Easy2D v2.1.5
// 项目类型:Win32 Application
//---------------------------------------------------------

Expand Down

0 comments on commit 7b821d5

Please sign in to comment.