简介:第二部份主要功能是在棋盘上画棋子,使用到几个重要函数把数组下标转换为像素等。1、工程中添加主要文件如下所示
2、global.cpp#include "global.h"
short piece[48]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0xc7,0xc6,0xc8,0xc5,0xc9,0xc4,0xca,0xc3,0xcb,0xa4,0xaa,0x93,0x95,0x97,0x99,0x9b,
0x37,0x36,0x38,0x35,0x39,0x34,0x3a,0x33,0x3b,0x54,0x5a,0x63,0x65,0x67,0x69,0x6b
};
short num(short x,short y,short size) //将像素转化为数组下标
{
short a,b,n;
if(30<=x&&x<=570&&30<=y&&y<=630)
{
x-=size/2;
y-=size/2;
a=x%size;
b=y%size;
x=(x-a)/size;
y=(y-b)/size;
n=(y+3)*16+x+3;
}else
n=0;
return n;
}
short posx(short n,short size) //从数组下标获取像素
{
n=n%16-3;
n=size*n+size/2+2;
return n;
}
short posy(short n,short size)
{
short m=n%16;
n=(n-m)/16-3;
n=size*n+size/2+2;
return n;
}
3、glabal.h#ifndef GLOBAL_H
#define GLOBAL_H
extern short piece[48];
//extern short piece[48];
short num(short x,short y,short size); //将像素转化为数组下标
short posx(short n,short size); //从数组下标获取像素
short posy(short n,short size);
#endif // GLOBAL_H
4、mainwindow.cpp添加MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
size = 60;
this->setWindowTitle(tr("楚河汉界"));
this->setFixedSize(size*10+100,size*10+100);
this->move(400,0);
pic[0]=QPixmap("./pic/redgen.png"); //将棋子对应的图片导入
pic[1]=QPixmap("./pic/redadv.png");
pic[2]=QPixmap("./pic/redadv.png");
pic[3]=QPixmap("./pic/redele.png");
pic[4]=QPixmap("./pic/redele.png");
pic[5]=QPixmap("./pic/redhorse.png");
pic[6]=QPixmap("./pic/redhorse.png");
pic[7]=QPixmap("./pic/redchar.png");
pic[8]=QPixmap("./pic/redchar.png");
pic[9]=QPixmap("./pic/redcan.png");
pic[10]=QPixmap("./pic/redcan.png");
pic[11]=QPixmap("./pic/redsod.png");
pic[12]=QPixmap("./pic/redsod.png");
pic[13]=QPixmap("./pic/redsod.png");
pic[14]=QPixmap("./pic/redsod.png");
pic[15]=QPixmap("./pic/redsod.png");
pic[16]=QPixmap("./pic/blackgen.png");
pic[17]=QPixmap("./pic/blackadv.png");
pic[18]=QPixmap("./pic/blackadv.png");
pic[19]=QPixmap("./pic/blackele.png");
pic[20]=QPixmap("./pic/blackele.png");
pic[21]=QPixmap("./pic/blackhorse.png");
pic[22]=QPixmap("./pic/blackhorse.png");
pic[23]=QPixmap("./pic/blackchar.png");
pic[24]=QPixmap("./pic/blackchar.png");
pic[25]=QPixmap("./pic/blackcan.png");
pic[26]=QPixmap("./pic/blackcan.png");
pic[27]=QPixmap("./pic/blacksod.png");
pic[28]=QPixmap("./pic/blacksod.png");
pic[29]=QPixmap("./pic/blacksod.png");
pic[30]=QPixmap("./pic/blacksod.png");
pic[31]=QPixmap("./pic/blacksod.png");
pic[32]=QPixmap("./pic/redboundary.png");
pic[33]=QPixmap("./pic/blackboundary.png");
//画32个棋子
int i=0;
for(;i<=31;i++) //定义棋子
{
chess[i]=new QLabel(this);
chess[i]->setPixmap(pic[i]);
chess[i]->setGeometry(posx(piece[i+16],size),posy(piece[i+16],size),56,56);
qDebug() << posx(piece[i+16],size) << posy(piece[i+16],size) << endl;
}
}
5、运行结果