学习Qt有一个月了,想要实现一个推箱子的游戏来检验一下自己。今天先设计一下将要完成的具体表现,因为是第一次做,所以设计的稍微简单点,以后逐渐修改。首先要创建菜单栏,分为三个主菜单:
①游戏
②选择关卡
③关于
(1)游戏主菜单下分为:
①开始游戏
②重新开始
③结束游戏
(2)选择关卡分为
①前一关卡
②后一关卡
③选择关卡,
(3)关于则弹出游戏的信息
主要使用以下几个类:
1、QPainter绘制地图
2、QMenu、QAction制作菜单栏
3、QKeyEvent接收键盘事件 控制人物行走及推箱子
①界面设计
QpushBox::QpushBox(QWidget *parent)
: QMainWindow(parent)
{
//ui.setupUi(this);
createMenu();
createAction();
widget=new QWidget;
setCentralWidget(widget);
levelLabel=new QLabel(QStringLiteral("当前关卡"));
stepLabel=new QLabel(QStringLiteral("当前步数"));
levelLCD=new QLCDNumber;
stepLCD=new QLCDNumber;
restartButton=new QPushButton(QStringLiteral("重新开始"));
connect(restartButton,SIGNAL(clicked()),this,SLOT(restartGameSlot()));
restartButton->setEnabled(false);
QVBoxLayout *rightLayout=new QVBoxLayout;
rightLayout->addWidget(levelLabel);
rightLayout->addWidget(levelLCD);
rightLayout->addWidget(stepLabel);
rightLayout->addWidget(stepLCD);
rightLayout->addWidget(restartButton);
rightLayout->addSpacing(210);
mainmap=new mappaint;
QHBoxLayout *mainLayout=new QHBoxLayout;
mainLayout->addWidget(mainmap);
mainLayout->addLayout(rightLayout);
widget->setLayout(mainLayout);
mainmap->levelNow=0;
mainmap->stepNow=0;
mainmap->man_x=0;
mainmap->man_y=0;
mainmap->BeginOrEnd=1;
mainmap->update();
boxNum=0;
levelLCD->display(mainmap->levelNow);
stepLCD->display(mainmap->stepNow);
createMap();
//**************程序整体设定****************
setWindowTitle(QStringLiteral("推箱子小游戏----Dpt设计"));
widget->setFocus();
statusBar()->showMessage(QStringLiteral("欢迎进入推箱子小游戏。"));
}
QpushBox::~QpushBox()
{
}
void QpushBox::createMenu()
{
startGameAction = new QAction(QStringLiteral("开始游戏"),this);
startGameAction->setShortcut(QKeySequence("Ctrl+S"));
startGameAction->setStatusTip(QStringLiteral("开始一场新游戏."));
connect(startGameAction,SIGNAL(triggered()),this,SLOT(startGameSlot()));
restartGameAction = new QAction(QStringLiteral("重新开始"),this);
restartGameAction->setShortcut(QKeySequence("Ctrl+R"));
restartGameAction->setStatusTip(QStringLiteral("重新开始本局."));
restartGameAction->setEnabled(false);
connect(restartGameAction,SIGNAL(triggered()),this,SLOT(restartGameSlot()));
endGameAction = new QAction(QStringLiteral("结束游戏"),this);
endGameAction->setShortcut(QKeySequence("Ctrl+E"));
endGameAction->setStatusTip(QStringLiteral("结束当前游戏."));
endGameAction->setEnabled(false);
connect(endGameAction,SIGNAL(triggered()),this,SLOT(endGameSlot()));
previousGatesAction = new QAction(QStringLiteral("前一关卡"),this);
previousGatesAction->setShortcut(QKeySequence("Ctrl+P"));
previousGatesAction->setStatusTip("选择前一关卡");
previousGatesAction->setEnabled(false);
connect(previousGatesAction,SIGNAL(triggered()),this,SLOT(previousSlot()));
nextGatesAction = new QAction(QStringLiteral("后一关卡"),this);
nextGatesAction->setShortcut(QKeySequence("Ctrl+N"));
nextGatesAction->setStatusTip(QStringLiteral("选择后一关卡"));
nextGatesAction->setEnabled(false);
connect(nextGatesAction,SIGNAL(triggered()),this,SLOT(nextGatesSlot()));
customGatesAction = new QAction(QStringLiteral("选择关卡"),this);
customGatesAction->setShortcut(QKeySequence("Ctrl+C"));
customGatesAction->setStatusTip(QStringLiteral("选择任意关卡"));
connect(customGatesAction,SIGNAL(triggered()),this,SLOT(customGatesSlot()));
aboutAction = new QAction(QStringLiteral("关于"),this);
aboutAction->setShortcut(QKeySequence("Ctrl+A"));
aboutAction->setStatusTip(QStringLiteral("本游戏信息"));
connect(aboutAction,SIGNAL(triggered),this,SLOT(aboutSlot()));
}
void QpushBox::createAction()
{
gameMenu = menuBar()->addMenu(QStringLiteral("游戏"));
gameMenu->addAction(startGameAction);
gameMenu->addAction(restartGameAction);
gameMenu->addAction(endGameAction);
gatesMenu = menuBar()->addMenu(QStringLiteral("选择关卡"));
gatesMenu->addAction(previousGatesAction);
gatesMenu->addAction(nextGatesAction);
gatesMenu->addAction(customGatesAction);
helpMenu = menuBar()->addMenu(QStringLiteral("帮助"));
helpMenu->addAction(aboutAction);
}