当前位置: 首页 > news >正文

鞍山制作网站哪家好软文写作300字

鞍山制作网站哪家好,软文写作300字,wordpress 搭建 wiki,郑州做网站照例先演示一下: QT井字棋游戏,可以悔棋。 会在鼠标箭头处跟随一个下棋方的小棋子图标。 棋盘和棋子是自己画的,可以自行在对应的代码处更换自己喜欢的图片,不过要注意尺寸兼容。 以棋会友: 井字棋最关键的就是下棋了&#xf…

照例先演示一下:

QT井字棋游戏,可以悔棋。

会在鼠标箭头处跟随一个下棋方的小棋子图标。

棋盘和棋子是自己画的,可以自行在对应的代码处更换自己喜欢的图片,不过要注意尺寸兼容。

以棋会友:

井字棋最关键的就是下棋了,我们需要重写窗口的鼠标点击事件,并且根据坐标进行判断。

如果鼠标点击的范围在棋盘里,那么就接着判断此处能否下棋(是否已经有棋子在这个地方了)。

我这里下棋的逻辑是拿一个3*3的vector来缓存下棋状态 ,0为没棋,1为白棋,2为黑棋。

只需要更新缓存更新绘图事件,让绘图事件按照缓存来重绘即可达到下棋的效果 。

每次下棋之后,轮到对方下棋(白棋下完黑棋下),需要将记录谁下棋的标志更新。

为了悔棋的功能,还需要将下棋的坐标存在一个vector里。

void TTT::mousePressEvent(QMouseEvent* e){  //按下鼠标int x = e->x(), y = e->y();if (x >= 65 && x <= 490 && y>=115 && y<=540 ) { //是否在棋盘范围内x -= 65, y -= 115;    //经过测试计算得到的结果,因为棋盘在中间,因此需要减去这些像素值才可以准确判断点击位置对应的缓存位置x /= 140, y /= 140;    //获取对应的缓存下标if (cache[x][y] == 0) { //如果该位置没有棋子就下棋if (iswhite) cache[x][y] = 1;else cache[x][y] = 2;    iswhite = !iswhite;    //更新下棋方last.push_back(vector<int>{x,y});    //添加缓存,用于悔棋check();    //检测是否赢棋以及和棋update();   //手动调用绘图事件}}
}

并且还需要检测落完子之后有没有人赢棋以及是否和棋。

重写一个函数用于检测,因为井字棋是3*3的比较简单,赢棋的情况就8种,所以直接用8条if来判断(试过用for循环来检测,结果还不如8条if来的简洁)。

void TTT::check(){  //检测是否赢棋已经是否和棋bool iswin = false;if (cache[0][0] == cache[0][1] && cache[0][0] == cache[0][2] && cache[0][0] != 0) iswin = true;if (cache[1][0] == cache[1][1] && cache[1][0] == cache[1][2] && cache[1][0] != 0) iswin = true;if (cache[2][0] == cache[2][1] && cache[2][0] == cache[2][2] && cache[2][0] != 0) iswin = true;if (cache[0][0] == cache[1][0] && cache[0][0] == cache[2][0] && cache[0][0] != 0) iswin = true;if (cache[0][1] == cache[1][1] && cache[0][1] == cache[2][1] && cache[0][1] != 0) iswin = true;if (cache[0][2] == cache[1][2] && cache[0][2] == cache[2][2] && cache[0][2] != 0) iswin = true;if (cache[0][0] == cache[1][1] && cache[0][0] == cache[2][2] && cache[0][0] != 0) iswin = true;if (cache[0][2] == cache[1][1] && cache[0][2] == cache[2][0] && cache[0][2] != 0) iswin = true;if (iswin) {    //如果有人赢棋QString who;if (cache[(*(last.end() - 1))[0]][(*(last.end() - 1))[1]] == 1) {   //根据最后一个落子是谁来判断谁赢棋who = QString::fromLocal8Bit("白棋");}else {who = QString::fromLocal8Bit("黑棋");}//弹出提示框,是否继续游戏int check=QMessageBox::question(this, who+QString::fromLocal8Bit("赢了"), who+QString::fromLocal8Bit("赢了,是否重新开始"));if (check == QMessageBox::Yes) {//如果继续游戏,则清空下棋记录,恢复棋盘清空,更新绘图事件cache = { {0,0,0},{0,0,0},{0,0,0} };last.clear();update();return;}//不继续游戏就退出程序.exit(0);}for (int i = 0; i < 3; i++) {   //判断是否和棋,只要有一个地方是0(没下棋)就是没和棋,直接returnfor (int j = 0; j < 3; j++) {if (cache[i][j] == 0) return;}}//弹出提示框,和棋,是否继续游戏,逻辑和上面赢棋的逻辑一样int check = QMessageBox::question(this, QString::fromLocal8Bit("和棋"), QString::fromLocal8Bit("和棋,是否重新开始"));if (check == QMessageBox::Yes) {cache = { {0,0,0},{0,0,0},{0,0,0} };last.clear();update();return;}exit(0);
}

落子无悔:

悔棋这个功能我是后面大致都写完了才想加上去的,然后窗口的大小,棋子的大小,棋盘的大小以及他们的坐标位置我都设计完了,没地方再插一个按钮来悔棋了,所以我直接加在了菜单栏里。

我们在下棋的时候就有把每一步下棋的坐标存起来,想要悔棋的话,我们只需要把最后一个下棋的落子坐标取出,把棋盘缓存中对应的位置改为0(没下棋),然后在更新绘图事件即可。

不要忘了把下棋方再改回去,并且把下棋记录的最后一个删去。

void TTT::initMenubar() {  //初始化菜单栏QMenuBar* qb = new QMenuBar(this);QMenu* item = new QMenu(QString::fromLocal8Bit("选项"), this);QAction* restart = new QAction(QString::fromLocal8Bit("重新开始"), this);QAction* revoke = new QAction(QString::fromLocal8Bit("悔棋"), this);connect(restart, &QAction::triggered, [=]() {   //重新开始//清空下棋记录,恢复棋盘初始状态,更新绘图事件cache = { {0,0,0},{0,0,0},{0,0,0} };last.clear();update();});connect(revoke, &QAction::triggered, [=]() {    //悔棋if (last.size() > 0) {  //如果有下棋记录才能悔棋//将最后一个下棋的位置设为0(没下棋),并且需要把下棋方再变回去(取个反),再把最后一个下棋记录删去cache[(*(last.end() - 1))[0]][(*(last.end() - 1))[1]] = 0;iswhite = !iswhite;last.pop_back();update();}});item->addAction(restart);item->addAction(revoke);QMenu* about = new QMenu(QString::fromLocal8Bit("关于"), this);QAction* me = new QAction(QString::fromLocal8Bit("我"), this);QAction* help = new QAction(QString::fromLocal8Bit("帮助"), this);QAction* quit = new QAction(QString::fromLocal8Bit("退出"), this);quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));connect(quit, &QAction::triggered, this, &QMainWindow::close);connect(me, &QAction::triggered, [=] {QMessageBox::information(this, QString::fromLocal8Bit("这里是折途"), QString::fromLocal8Bit("bilibili:折途想要敲代码/折途想要长高高\nCSDN:折途\n微信公众号:折途想要敲代码"));});connect(help, &QAction::triggered, [=] {QMessageBox::information(this, QString::fromLocal8Bit("使用帮助"), QString::fromLocal8Bit("井字棋游戏 TicTacToe(TTT)"));});about->addAction(me);about->addAction(help);about->addAction(quit);qb->addMenu(about);qb->addMenu(item);qb->setFixedHeight(50);this->setMenuBar(qb);
}

脑袋跟着屁股走:

脑袋跟着屁股走,棋子跟着鼠标走。

从开头的动图可以看出鼠标剪头所指有下棋方的棋子小图标,要做到这个就需要重写窗口的鼠标移动事件,每次鼠标移动我们都更新鼠标的坐标,然后重写调用绘图事件,在相应的位置画上小棋子。

如果直接重写鼠标移动事件函数的话只有在鼠标点击的时候才会调用,我们需要在加上构造函数的开头加上行代码用于设置跟踪鼠标:

setMouseTracking(true);     //设置跟踪获取鼠标坐标,用于更改鼠标指向的棋子

然后重写鼠标移动事件:

void TTT::mouseMoveEvent(QMouseEvent* e){   //实时获取鼠标坐标,用于修改鼠标指向的小图标mouse[0] = e->x();mouse[1] = e->y();update();
}

然后剩下就是绘图事件的工作了。

绘图:

其实要绘的图不多,一个是棋盘,一个是下的棋子,另一个就是跟着鼠标的棋子小图标。

void TTT::paintEvent(QPaintEvent* e){QPainter* p = new QPainter(this);QPixmap board;board.load(":/image/board.png");p->drawPixmap(50,100,board);//绘制已经下过的棋for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {if (cache[i][j] != 0) {QPixmap piece;if (cache[i][j] == 1) piece.load(":/image/white.png");  //根据缓存的数值决定下什么颜色的棋else piece.load(":/image/black.png");p->drawPixmap(150*i+75,150*j+125,piece);    //下在格子里}}}  //修改鼠标坐标的指向的图片以及位置QPixmap mou;if (iswhite) {mou.load(":/image/white.png");}else {mou.load(":/image/black.png");}mou = mou.scaled(50, 50);p->drawPixmap(mouse[0] - 30, mouse[1] - 30, mou);   //让鼠标在图标的中间p->end();
}

免费领取完整代码:

完整的代码我已经上传到CSDN了,大家可以进入我的主页找到对应资源直接免费下载。

也可以关注我的微信公众号 折途想要敲代码  回复关键词“qt井字棋”免费下载完整代码。

我上传的是VS的完整工程文件,已经自己绘制的图片,如果小伙伴用的是QtCreater,可以直接把cpp和h的文件内容复制过去,再把资源文件配置一下就好啦。


文章转载自:
http://dinncohieroglyphologist.tpps.cn
http://dinnconugatory.tpps.cn
http://dinncozoosporangium.tpps.cn
http://dinncoseries.tpps.cn
http://dinncomonometallism.tpps.cn
http://dinncophonetics.tpps.cn
http://dinncodeconcentrate.tpps.cn
http://dinncocounteraccusation.tpps.cn
http://dinncointersolubility.tpps.cn
http://dinncoyhwh.tpps.cn
http://dinnconeanderthaloid.tpps.cn
http://dinnconasopharynx.tpps.cn
http://dinncounmarred.tpps.cn
http://dinncoforbode.tpps.cn
http://dinncointerpolator.tpps.cn
http://dinncobolshevik.tpps.cn
http://dinncointerindividual.tpps.cn
http://dinncoprolifically.tpps.cn
http://dinncosnowcreep.tpps.cn
http://dinncononrigid.tpps.cn
http://dinncomultilist.tpps.cn
http://dinncobeloved.tpps.cn
http://dinncooffensively.tpps.cn
http://dinncopeleus.tpps.cn
http://dinncocurcuma.tpps.cn
http://dinncorhumba.tpps.cn
http://dinnconeolite.tpps.cn
http://dinnconovelistic.tpps.cn
http://dinncoloosestrife.tpps.cn
http://dinncouncirculated.tpps.cn
http://dinncocypriot.tpps.cn
http://dinncohumouristic.tpps.cn
http://dinncoriboflavin.tpps.cn
http://dinncoretold.tpps.cn
http://dinncounrevealed.tpps.cn
http://dinncopogonotomy.tpps.cn
http://dinncoseminiferous.tpps.cn
http://dinncosequenator.tpps.cn
http://dinncoiodid.tpps.cn
http://dinncovibrant.tpps.cn
http://dinncodichloride.tpps.cn
http://dinncofleuret.tpps.cn
http://dinncoshallot.tpps.cn
http://dinncowhetter.tpps.cn
http://dinncoproficience.tpps.cn
http://dinncobrussels.tpps.cn
http://dinncoincoordination.tpps.cn
http://dinncodiazotize.tpps.cn
http://dinncoleatherboard.tpps.cn
http://dinncoapperception.tpps.cn
http://dinncodentelated.tpps.cn
http://dinncohemophobia.tpps.cn
http://dinncobet.tpps.cn
http://dinncoreinhold.tpps.cn
http://dinncozarzuela.tpps.cn
http://dinncoharmonically.tpps.cn
http://dinncoazeotrope.tpps.cn
http://dinncoswig.tpps.cn
http://dinncotumblebug.tpps.cn
http://dinncodamascene.tpps.cn
http://dinncobroaden.tpps.cn
http://dinncodari.tpps.cn
http://dinncoindigence.tpps.cn
http://dinncopatentor.tpps.cn
http://dinncoergophobiac.tpps.cn
http://dinncocimex.tpps.cn
http://dinncoreemphasis.tpps.cn
http://dinncoread.tpps.cn
http://dinnconeutretto.tpps.cn
http://dinnconiton.tpps.cn
http://dinncospodumene.tpps.cn
http://dinncopipelike.tpps.cn
http://dinncoinornate.tpps.cn
http://dinncoquayage.tpps.cn
http://dinncooverhand.tpps.cn
http://dinncohippy.tpps.cn
http://dinncosubstratosphere.tpps.cn
http://dinncohippocampi.tpps.cn
http://dinncolimpsy.tpps.cn
http://dinncocrossed.tpps.cn
http://dinncoagonizingly.tpps.cn
http://dinncoripply.tpps.cn
http://dinncoscindapsus.tpps.cn
http://dinncoabstersive.tpps.cn
http://dinncodiascope.tpps.cn
http://dinncoothin.tpps.cn
http://dinncocardiophobia.tpps.cn
http://dinncofranchisor.tpps.cn
http://dinncoholomyarian.tpps.cn
http://dinncopatrilineage.tpps.cn
http://dinncocollateralize.tpps.cn
http://dinncophytoplankter.tpps.cn
http://dinncoassibilation.tpps.cn
http://dinncomariolatry.tpps.cn
http://dinncoamenophis.tpps.cn
http://dinncozapatismo.tpps.cn
http://dinncojugal.tpps.cn
http://dinncobrokage.tpps.cn
http://dinncoclwyd.tpps.cn
http://dinncoweightlessness.tpps.cn
http://www.dinnco.com/news/120083.html

相关文章:

  • 开发公司岗位安全操作规程成都高新seo
  • hk域名网站域名ip查询查网址
  • 如何选择番禺网站建设百度账户登录
  • 仿牌网站建设东莞seo网站优化排名
  • 网站平台推广有哪些攀枝花seo
  • 制作梦核的网站成品影视app开发
  • 公司网站做优化家居seo整站优化方案
  • 网站建设周期优质外链平台
  • 制作人结局金秀贤和谁在一起了搜索引擎优化关键字
  • 湖南做网站 真好磐石网络网站推广的四个阶段
  • 深圳市九号公告最新消息宁波免费seo在线优化
  • 电子商务推广网站商务软文写作
  • 黑群晖做php网站sem是什么意思
  • 哪家网站开发营销外包
  • 提供手机网站建设哪家好搜索引擎是指什么
  • 长安大学门户网站是谁给做的百度seo点击工具
  • 做律师网站的公司百度指数查询app
  • 武汉做网站价格如何快速收录一个网站的信息
  • 国内建网站公司近一周的新闻大事热点
  • 动态网站的运作流程微信推广平台收费标准
  • 台州行app官网下载长沙专业seo优化推荐
  • 网页设计怎么建站点搜索引擎排行榜
  • 网页翻页电子书制作模板网站关键词怎么优化到首页
  • wordpress 文件上传大小限制怀来网站seo
  • 如何做淘宝联盟网站主百度seo排名点击
  • 免费搭建wordpressseo优化推广多少钱
  • wordpress小说站数据库seo课程培训机构
  • 网络舆情分析报告网站如何做优化推广
  • 包头网站建设熊掌号淮北seo
  • 网站建设销售还能做吗襄阳seo培训