Skip to content

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
daniao2017 committed Sep 1, 2020
1 parent 87d9463 commit e5e6505
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions code/CoinFlip/doc/2020-08-30-循环之妙用.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
## for循环之妙用
### 关卡图标的绘制
如下图所示图标,是如何绘制出来的呢?

![图标绘制](./asserts/coin.png)

#### 代码理解
for循环的妙用,将图片按照一定的规律给递归分布,后将序列号写上。
```
for(int i = 0,i<20,i++){
menuBtn ->move(25+(i%4)*70,130+(i/4)*70);
label ->setText(QString::number(i+1));
}
```
完整详细代码见下
```
//创建关卡按钮
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);
//按钮上显示的文字
QLabel * label = new QLabel;
label ->setParent(this);
label ->setFixedSize(menuBtn->width(),menuBtn->height());
label ->setText(QString::number(i+1));
//设置居中
label ->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//移动
label->move(25 + (i%4)*70 , 130+ (i/4)*70);
//鼠标事件穿透
label->setAttribute(Qt::WA_TransparentForMouseEvents,true);
//监听选择关卡按钮的信号槽
connect(menuBtn,&MyPushButton::clicked,[=](){
if(pScene ==NULL){
this ->hide();
pScene = new PlayScene(i+1);
pScene ->setGeometry(this->geometry());
chooseSound->play();
pScene ->show();
//监听信号
connect(pScene,&PlayScene::chooseSceneBack,[=](){
this->setGeometry(pScene->geometry());
this->show();
backSound->play();
delete pScene;
pScene = NULL;
});
}
});
```
### 金币方格的绘制

如下图所示图标,是如何绘制出来的呢?

![图标绘制](./asserts/background.png)

#### 代码理解

```
//创建金币的背景图片
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
//绘制背景图片
QPixmap pix = QPixmap(":/res/BoardNode.png");
QLabel* label = new QLabel;
label->setGeometry(0,0,pix.width(),pix.height());
label->setPixmap(pix);
label->setParent(this);
label->move(57 + i*50,200+j*50);
}
}
```

### 总结

以为两个小例子分别用for 循环完成了多场景的创建,值得借鉴。
Binary file added code/CoinFlip/doc/asserts/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added code/CoinFlip/doc/asserts/coin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e5e6505

Please sign in to comment.