金币游戏知识总结"/>
QT 翻金币游戏知识总结
QT 翻金币游戏知识总结
QT翻金币游戏的资料,下载于b站的黑马程序员QT课程,游戏目标为将金币都翻转为同一个颜色(金色)。
资料下载:
提取码:1256
1、void QWidget::setFixedSize(int w, int h)
Sets the width of the widget to w and the height to h.给定窗口固定的宽度和高度2、void QWidget::setFixedSize(const QSize &s)
Sets both the minimum and maximum sizes of the widget to s, thereby preventing it from ever growing or shrinking.
可以设置窗口最大与最小尺寸3、 connect(ui->actionQuit,&QAction::triggered,[=](){this->close();});//此处首先省略了receiver: this, [=](){this->close();} 用了lambda表达式实现槽函数,lambda表达式知识链接如下,.html4、MypushButton *startBtn =new MypushButton(":/res/MenuSceneStartButton.png",""); //给按钮添加图片5、startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);//其将图片的左侧放置于this->width(),this->height()*0.7 位置,所以需要-startBtn->width()*0.5,才可把开始按钮放在中心
6、 QSound *startSound = new QSound(":/res/TapButtonSound.wav",this);startSound->play();
//添加及播放音频7、void MypushButton::zoom1()
{QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");animation->setDuration(200);animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));animation->setEasingCurve(QEasingCurve::OutBounce);animation->start();} //动画8、 QTimer::singleShot(500,this,[=]() //延时500ms 调用槽函数 此处的槽函数使用lambda表达 式。{this->hide();chooseScene->show();});9、 connect(chooseScene,&ChooseLevelSence::chooseSceneBack,[=](){this->show();});
signals:void chooseSceneBack();//自己定义一个信号 在适当的时机发射信号 发射调用emit函数使用时:QTimer::singleShot(500,this,[=](){this->hide();emit this->chooseSceneBack()
10、两个界面的切换:点击startButn时,进入chooseScenceBack界面,在chooseScence点击返回 返回到当前界面
connect(startBtn,&QPushButton::clicked,[=](){startSound->play();startBtn->zoom1();startBtn->zoom2();QTimer::singleShot(500,this,[=](){this->hide();chooseScene->show();});});connect(chooseScene,&ChooseLevelSence::chooseSceneBack,[=](){this->show();});///connect(closeBtn,&MypushButton::clicked,[=](){backSound->play();QTimer::singleShot(500,this,[=](){this->hide(); //隐藏某个窗口emit this->chooseSceneBack();});
11、Qpixmap 类的使用
QPainter painter(this);QPixmap pix;pix.load(":/res/PlayLevelSceneBg.png"); // 1#painter.drawPixmap(0,0,this->width(),this->height(),pix); //1#
//void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
Draws the rectangular portion source of the given pixmap into the given target in the paint device. piamap加载一张图片,将该图片位于source规定矩形内部的内容,绘制target规定的区域。
12、引入了不完全对象可能是没有添加头文件导致的
13、不同类的创建和相互调用。
如果继承QPushButton创建了自己的按钮类,则步骤为新建Qt Qt source File ,基类选择Qwidget,然后修改其父类为QpushButton。
例如:
class MyPushButton:public QpushButton
{MypushBUtton();~MypushBUtton();
}
创建好后,若在其他类中使用该按键,则添加该类的头文件,然后调用MypushButton 类在该类中新建一个对象。
14、为每一个选择关卡的按键绑定了触发后的槽函数
for( int i = 0 ; i < 20 ;i ++){MypushButton * menuBtn = new MypushButton(":/res/LevelIcon.png","");menuBtn->setParent(this);menuBtn->move( 25 + i%4 * 70 , 130 + i/4 * 70 );//监听每个按钮的点击事件connect(menuBtn,&MypushButton::clicked,[=](){chooseSound->play();if(pSence==NULL){this->hide();pSence= new playSence(i+1);pSence->show();connect(pSence,&playSence::chooseSceneBack,[=](){this->show();delete pSence;pSence=NULL;});}});QLabel * label = new QLabel;label->setParent(this);label->setFixedSize(menuBtn->width(),menuBtn->height());label->setText(QString::number(i+1));label->move(25 + i%4 * 70 , 130 + i/4 * 70 );//设置 label上的文字对齐方式 水平居中和 垂直居中label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//设置让鼠标进行穿透 51号属性label->setAttribute(Qt::WA_TransparentForMouseEvents);}
15、在lable上显示某一字号和尺寸的字体:
QFont font;font.setFamily("华文新魏");font.setPointSize(20);lable->setFont(font);
16、翻金币功能实现:
for(int i=0;i<4;i++){for(int j=0;j<4;j++){gameArray[i][j]=config.mData[this->levelIndex][i][j];QString img;if(gameArray[i][j]==1){img=":/res/Coin0001.png";}else{img=":/res/Coin0008.png";}MyCoin *coin =new MyCoin(img); //白的是0coin->setParent(this);coin->move(59+i*50,204+j*50);coin->posX=i;coin->posY=j;coin->flag=gameArray[i][j];coinBtn[i][j]=coin;//翻金币按键按下connect(coin,&MyCoin::clicked,[=](){flipSound->play();coin->changFlag();gameArray[i][j]=gameArray[i][j]==0?1:0;//延时300ms 翻金币QTimer::singleShot(300,this,[=](){if(coin->posX+1<=3){coinBtn[coin->posX+1][coin->posY]->changFlag();gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;}if(coin->posX-1>=0){coinBtn[coin->posX-1][coin->posY]->changFlag();gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]== 0 ? 1 : 0;}if(coin->posY+1<=3){coinBtn[coin->posX][coin->posY+1]->changFlag();gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;}if(coin->posY-1>=0){coinBtn[coin->posX][coin->posY-1]->changFlag();gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;}this->isWin=true;for(int i=0;i<4;i++){for(int j=0;j<4;j++){if(coinBtn[i][j]->flag==false){this->isWin=false;break;}}}if(this->isWin){qDebug()<<"胜利";winSound->play();QPropertyAnimation *animation1=new QPropertyAnimation(winLabel,"geometry");animation1->setDuration(1000);animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));animation1->setEasingCurve(QEasingCurve::OutBounce);animation1->start();for (int i=0;i<4;i++){for(int j=0;j<4;j++){coinBtn[i][j]->isWin=true;}}}});});}
1)根据this->levelIndex 关卡不同,选择不同矩阵,完成金币正反面显示。
2)总共20个关卡,关卡数有playSence(i+1)传入。
3 ) mData是Qmap类型,this->levelIndex作为其索引值,索引的值是一个二维数组,由mData的初始化过程可知,将二维数组Qvector表 示。
4) 将 connect(coin,&MyCoin::clicked,=写在for循环中,保证每一个按键被按下都会触发翻金币功能,
QTimer::singleShot(300,this,= 该处是延时300ms,在给定范围内翻按下金币的上下左右金币。
5)coinBtn 记录金币当前的状态,如果经过遍历所有金币的flag都为1,即翻完了金币,则触发胜利图标展示。
gameArray[i][j]=config.mData[this->levelIndex][i][j];//QMap<int, QVector< QVector<int> > >mData;
//int array1[4][4] = {{1, 1, 1, 1},{1, 1, 0, 1},{1, 0, 0, 0},{1, 1, 0, 1} } ;QVector< QVector<int>> v;for(int i = 0 ; i < 4;i++){QVector<int>v1;for(int j = 0 ; j < 4;j++){v1.push_back(array1[i][j]);}v.push_back(v1);}mData.insert(1,v);/
更多推荐
QT 翻金币游戏知识总结
发布评论