Qt实现推箱子小游戏

本文记录了使用Qt框架开发推箱子小游戏的过程,包括创建菜单栏、实现游戏逻辑和键盘事件处理。游戏设有多个关卡,并通过QPainter绘制地图,QMenu、QAction创建菜单项,利用QKeyEvent处理玩家输入。代码中详细展示了各个关键功能的实现,如人物行走、推箱子、关卡切换等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习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);
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值