目录
MC2D v0.0.6 Source Code
Declaration
A Minecraft 2D Version Powered By Qt
main.cpp
#include "Tool.h"
#include "selectworld.h"
#include "maininterface.h"
#include "optionwidget.h"
#include "createmap.h"
#include "gamewidget.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainInterface *main_interface = new MainInterface(nullptr, 1280, 720, TITLE);
SelectWorld *select_world = new SelectWorld(nullptr, 1280, 720, TITLE);
OptionWidget *option_widget = new OptionWidget(nullptr, 1280, 720, TITLE);
CreateMap *create_map = new CreateMap(nullptr, 1280, 720, TITLE);
GameWidget *game_widget = new GameWidget(30, nullptr, 1280, 720, TITLE);
/* Main Interface -> Select Map */
QObject::connect(main_interface, &MainInterface::select_world, [=]() {
select_world->updateMapList();select_world->Show(main_interface);});
/* Select Map -> Main Interface*/
QObject::connect(select_world, &SelectWorld::main_interface, [=]() {
main_interface->Show(select_world);});
/* Select Map -> Create Map */
QObject::connect(select_world, &SelectWorld::create_map, [=]() {
create_map->Show(select_world);});
/* Main Interface -> Option Widget */
QObject::connect(main_interface, &MainInterface::option, [=]() {
option_widget->Show(main_interface);});
/* Option Widget -> Main Interface*/
QObject::connect(option_widget, &OptionWidget::main_interface, [=]() {
main_interface->Show(option_widget);});
/* Create Map -> Select World*/
QObject::connect(create_map, &CreateMap::select_world, [=]() {
select_world->updateMapList();select_world->Show(create_map);});
QObject::connect(select_world, &SelectWorld::game_widget, [=]() {
game_widget->startGame();
game_widget->Show(select_world);
});
QObject::connect(option_widget, &OptionWidget::update_setting, [=]() {
/* Update Font */
if(option_widget->langMode == 1)
option_widget->setLang->langs->setCurrentIndex(0);
else if(option_widget->langMode == 2)
option_widget->setLang->langs->setCurrentIndex(1);
else if(option_widget->langMode == 3)
option_widget->setLang->langs->setCurrentIndex(2);
else if(option_widget->langMode == 4)
option_widget->setLang->langs->setCurrentIndex(3);
main_interface->updateFont(option_widget->font());
select_world->updateFont(option_widget->font());
option_widget->updateFont(option_widget->font());
create_map->updateFont(option_widget->font());
main_interface->updateLang(option_widget->langMode);
option_widget->updateLang(option_widget->langMode);
select_world->updateLang(option_widget->langMode);
create_map->updateLang(option_widget->langMode);
});
QObject::connect(main_interface, &BaseWidget::GameOver, [=]() {
sendInfo("Game has been over");
option_widget->saveSetting();
exit(0);
});
QObject::connect(select_world, &BaseWidget::GameOver, [=]() {
sendInfo("Game has been over");
option_widget->saveSetting();
exit(0);
});
QObject::connect(option_widget, &BaseWidget::GameOver, [=]() {
sendInfo("Game has been over");
option_widget->saveSetting();
exit(0);
});
QObject::connect(create_map, &BaseWidget::GameOver, [=]() {
sendInfo("Game has been over");
option_widget->saveSetting();
exit(0);
});
option_widget->loadSetting();
/* Start Game (Centered) */
main_interface->Show((QApplication::desktop()->width() - 1280) / 2, (QApplication::desktop()->height() - 720) / 2, 0);
return a.exec();
}
basewidget.h
#ifndef BASEWIDGET_H
#define BASEWIDGET_H
#include "Tool.h"
/* A Basic Widget in Game */
class BaseWidget : public QWidget {
Q_OBJECT
public:
explicit BaseWidget(QWidget *parent = nullptr,
int width = 1280,
int height = 720,
QString title = TITLE);
void Show(int x = 0, int y = 0, int mode = 0);
void Show(BaseWidget *widget);
void Hide();
virtual void updateFont(QFont font);
void closeEvent(QCloseEvent *event) override;
virtual void updateLang(int langMode);
int langMode;
/* Button Clicked Sound */
QSound *buttonSound = new QSound(":/sound/musics/ButtonClicked.wav");
signals:
void GameOver();
void widgetShow();
};
#endif // BASEWIDGET_H
basewidget.cpp
#include "basewidget.h"
BaseWidget::BaseWidget(QWidget *parent,
int width,
int height,
QString title) {
/* Init Widget */
this->setWindowIcon(QIcon(":/gui/images/gui/icon.png"));
this->setParent(parent);
this->resize(width, height);
this->setWindowTitle(title);
}
void BaseWidget::Show(int x, int y, int mode) {
/* Mode == 0 : Normal */
/* Mode == 1 : Maximized */
/* Mode == 2 : FullScreen */
if(!mode) this->move(x, y);
switch(mode) {
case 0: this->showNormal(); break;
case 1: this->showMaximized(); break;
case 2: this->showFullScreen(); break;
}
sendInfo("Widget Has Been Shown");
widgetShow();
}
void BaseWidget::Show(BaseWidget *widget)
{
widget->Hide();
if(widget->isMaximized()) this->Show(0, 0, 1);
else if(widget->isFullScreen()) this->Show(0, 0, 2);
else {
this->resize(widget->width(), widget->height());
this->Show(widget->x(), widget->y());
}
widgetShow();
}
void BaseWidget::Hide() {
this->hide();
sendInfo("Widget Has Been Hidden");
}
void BaseWidget::updateFont(QFont font)
{
setFont(font);
}
void BaseWidget::closeEvent(QCloseEvent *event)
{
GameOver();
}
void BaseWidget::updateLang(int langMode)
{
if(langMode == 1) {
setWindowTitle(CN_TITLE);
} else if(langMode == 2) {
setWindowTitle(TITLE);
}
sendInfo("Lang has been updated");
}
maininterface.h
#ifndef MAININTERFACE_H
#define MAININTERFACE_H
#include "basewidget.h"
class MainInterface : public BaseWidget
{
Q_OBJECT
public:
explicit MainInterface(QWidget *parent = nullptr,
int width = 1280,
int height = 720,
QString title = nullptr);
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void updateFont(QFont font) override;
void updateLang(int langMode) override;
/* Image */
QImage titleImg = QImage(":/gui/images/gui/title.png");
QImage bg = QImage(":/gui/images/gui/bg.png");
int titleWidth = titleImg.width(), titleHeight = titleImg.height();
/* Button */
GameButton *SinglePlayer = new GameButton( ("SinglePlayer"), this);
GameButton *MultiPlayer = new GameButton( ("MultiPlayer"), this);
GameButton *Exit = new GameButton( ("Exit"), this);
GameButton *Option = new GameButton( ("Options..."), this);
/* Text */
QStaticText GameInfoText = QStaticText( ("Minecraft 2D Author: Bili_TianX"));
signals:
void select_world();
void option();
};
#endif // MAININTERFACE_H
maininterface.cpp
#include "maininterface.h"
MainInterface::MainInterface(QWidget *parent, int width, int height, QString title) : BaseWidget(parent, width, height, title)
{
/* Init Buttons */
MultiPlayer->setEnabled(false);
connect(SinglePlayer, &QPushButton::clicked, [=]() {
sendInfo("Player Clicked SinglePlayer Button");
buttonSound->play();
select_world();
});
connect(MultiPlayer, &QPushButton::clicked, [=]() {
sendInfo("Player Clicked MultiPlayer Button");
sendError("MultiPlayer Button is Invalid");
buttonSound->play();
});
connect(Option, &QPushButton::clicked, [=]() {
sendInfo("Player Clicked Option Button");
buttonSound->play();
option();
});
connect(Exit, &QPushButton::clicked, [=]() {
sendInfo("Player Clicked Exit Button");
buttonSound->play();
GameOver();
});
}
void MainInterface::paintEvent(QPaintEvent *event)
{
QPainter p(this);
/* Draw Background */
p.drawImage(QRect(0, 0, width(), height()), bg);
/* Draw Title */
p.drawImage(19 * width() / 160,
35 * height() / 288,
titleImg.scaled(titleWidth * width() / 1280,
titleHeight * height() / 720));
/* Draw Text */
p.setFont(this->font());
p.setPen(QColor(255, 255, 255));
p.drawStaticText(QPoint(10, height() - GameInfoText.size().height() - 5), GameInfoText);
}
void MainInterface::resizeEvent(QResizeEvent *event)
{
/* Update The Buttons Size */
SinglePlayer->resize(400 * width() / 1280, 40 * height() / 720);
MultiPlayer->resize(400 * width() / 1280, 40 * height() / 720);
Option->resize(400 * width() / 1280, 40 * height() / 720);
Exit->resize(400 * width() / 1280, 40 * height() / 720);
/* Update The Buttons Position */
SinglePlayer->move(440 * width() / 1280, 360 * height() / 720); // 360 + 40 * 1
MultiPlayer->move(440 * width() / 1280, 440 * height() / 720); // 360 + 40 * 3
Option->move(440 * width() / 1280, 520 * height() / 720); // 360 + 40 * 5
Exit->move(440 * width() / 1280, 600 * height() / 720); // 360 + 40 * 7
}
void MainInterface::keyPressEvent(QKeyEvent *event)
{
switch(event->key()) {
/* F11 -> Full Screen */
case Qt::Key_F11:
if(!isFullScreen()) this->showFullScreen();
else this->showNormal();
break;
/* ESC -> Exit Game */
case Qt::Key_Escape:
GameOver();
}
}
void MainInterface::updateFont(QFont font