昨天讲了Qt如何检测游戏手柄,今天我们就模仿“小鸡模拟器”,打造自己的游戏库,
首先安装“小鸡模拟器”,下载自己喜欢的FC游戏,
1.俄罗斯方块 2.魂斗罗 3.坦克大战
保证游戏运行后,我们开始编写自己的代码:
#include <QPixmap>
#include <QDebug>
//#include <TlHelp32.h>
//#include <windef.h>
#include <QGamepad>
#include <QProcess>
#include <QGamepadManager>
//class QGamepad;
#pragma comment(lib,"user32.lib")
/
界面ui:
listWidget,添加
1.俄罗斯方块
2.魂斗罗
3.坦克大战
设置属性:currentRow 为0
添加,label_2
/
添加资源文件:
/new/prefix1/res/1.png
/new/prefix1/res/2.jpeg
/new/prefix1/res/3.jpeg
/
h文件
public:
QProcess *process;//定义进程指针
private slots:
void stateChanged(QProcess::ProcessState state);//检测进程状态
cpp文件
process = new QProcess();//创建进程
//进程状态监控
connect(process,SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(stateChanged(QProcess::ProcessState)));
//连接设备
QGamepad *m_gamepad = new QGamepad(0, this);
connect(m_gamepad, &QGamepad::buttonStartChanged, this, [=](bool pressed){
qDebug() << "Button Start" << pressed;
if(pressed==true)
{
if(ui->listWidget->currentRow()==0)
process->start(tr("D:\\Program Files (x86)\\xiaoji\\Emulator\\FC\\fceux\\fceux.exe"),QStringList("H:\\Games\\FC\\7100638\\7100638.zip"));
//process->startDetached(tr("D:\\Program Files (x86)\\xiaoji\\Emulator\\FC\\fceux\\fceux1.exe"),QStringList("H:\\Games\\FC\\7100060\\7100060.zip"));
if(ui->listWidget->currentRow()==1)
process->start(tr("D:\\Program Files (x86)\\xiaoji\\Emulator\\FC\\fceux\\fceux.exe"),QStringList("H:\\Games\\FC\\7100060\\7100060.zip"));
if(ui->listWidget->currentRow()==2)
process->start(tr("D:\\Program Files (x86)\\xiaoji\\Emulator\\FC\\fceux\\fceux.exe"),QStringList("H:\\Games\\FC\\7000203\\7000203.zip"));
}
});
connect(m_gamepad, &QGamepad::buttonUpChanged, this, [=](bool pressed){
qDebug() << "buttonUp" << pressed;
qDebug() << ui->listWidget->currentRow();
if(pressed==true)
ui->listWidget->setCurrentRow(ui->listWidget->currentRow()-1);
});
connect(m_gamepad, &QGamepad::buttonDownChanged, this, [=](bool pressed){
qDebug() << "buttonDown" << pressed;
qDebug() << ui->listWidget->currentRow();
if(pressed==true)
ui->listWidget->setCurrentRow(ui->listWidget->currentRow()+1);
});
void MainWindow::on_listWidget_currentRowChanged(int currentRow)
{
QPixmap img;
if(currentRow==0)
img.load(":/new/prefix1/res/1.png");
if(currentRow==1)
img.load(":/new/prefix1/res/2.jpeg");
if(currentRow==2)
img.load(":/new/prefix1/res/3.jpeg");
ui->label_2->clear();
ui->label_2->setPixmap(img);
}
void MainWindow::stateChanged(QProcess::ProcessState state)
{
qDebug()<<"show state:";
switch(state)
{
case QProcess::NotRunning:
qDebug()<<"Not Running";
break;
case QProcess::Starting:
qDebug()<<"Starting";
break;
case QProcess::Running:
qDebug()<<"Running";
break;
default:
qDebug()<<"otherState";
break;
}
}
|