基于QT实现的简易3*3拼图游戏
来源:互联网 发布:php 判断大小 编辑:程序博客网 时间:2024/06/15 16:16
- 先建立游戏界面,将图片分割分别添加到相应的Label中,之后通过随机函数将Label顺序打乱,同时将游戏信息初始化。编写一个鼠标按压事件,当鼠标点击后,判断鼠标是否点击了空白格子旁边可移动的Label,若是,移动后,检测是否Label顺序正确,基本的逻辑就这样。
//playwindow.h#ifndef PLAYWINDOW_H#define PLAYWINDOW_H#include <QMainWindow>#include <QPushButton>#include <QLabel>#include <QImage>namespace Ui {class PlayWindow;}class PlayWindow : public QMainWindow{ Q_OBJECTpublic: explicit PlayWindow(QWidget *parent = 0); ~PlayWindow(); void mousePressEvent(QMouseEvent *event); //鼠标点击事件private: Ui::PlayWindow *ui; QLabel *picLabel[9]; //定义盛放被分割的9个图片块的Label QLabel *numLabel; //显示步数的Label QLabel *referenceLabel; //显示全图的Label int picFlag[3][3]; //定义二维数组用来标志每个位置放的第几个图 QImage labelImg[3][3]; //定义9个被分割的9个图片 void createPic(); //创建界面 void movePic(); //移动图片 void RandomPic(); //打乱图片 void checkOk(); //检测图片是否拼成功 int stepNum; //步数变量 QPushButton *exitBt; //退出按钮private slots: void exit(); //退出槽函数};#endif // PLAYWINDOW_H
//playwindow.c#include "playwindow.h"#include "ui_playwindow.h"#include <QImage>#include <QPixmap>#include <QTime>#include <QPoint>#include <QMouseEvent>#define PICTURESIZE 100 //分割后单个图片边长#define BASEX 2 //图片X间隔#define BASEY 2 //图片Y间隔PlayWindow::PlayWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::PlayWindow){ ui->setupUi(this); //设置窗体外观 this->setMinimumSize(308,410); this->setMaximumSize(308,410); this->setStyleSheet(tr("background-image: url(:/new/prefix1/background.jpg);")); //步数初始化 stepNum = 0; //实例参考图片的Label referenceLabel = new QLabel(this); referenceLabel->setGeometry(129,333,50,50); referenceLabel->setFrameShape(QFrame::Box); //实例显示步数的Label numLabel = new QLabel(this); numLabel->setGeometry(20,344,70,25); numLabel->setText(QString::number(stepNum)); numLabel->setFrameShape(QFrame::Box); //实例退出按钮 exitBt = new QPushButton(this); exitBt->setGeometry(QRect(230,344,50,25)); exitBt->setText("退出"); connect(exitBt,SIGNAL(clicked()),this,SLOT(exit())); //初始化界面 createPic();}void PlayWindow::createPic(){ QImage sourceImg; //加载操作图片 sourceImg.load(":/new/prefix1/sourcePic .jpg"); sourceImg = sourceImg.scaled(PICTURESIZE*3,PICTURESIZE*3); for( int i=0 ; i<3 ; i++){ for( int j=0 ; j<3 ; j++ ){ picLabel[i*3+j] = new QLabel(this); picLabel[i*3+j]->setGeometry(0,0,PICTURESIZE,PICTURESIZE); picLabel[i*3+j]->move((BASEX+PICTURESIZE)*j+BASEX,(BASEY+PICTURESIZE)*i+BASEY); picLabel[i*3+j]->setFrameShape(QFrame::Box); picFlag[i][j] = i*3+j; labelImg[i][j] = sourceImg.copy(PICTURESIZE*j,PICTURESIZE*i,PICTURESIZE,PICTURESIZE); picLabel[i*3+j]->setPixmap(QPixmap::fromImage(labelImg[i][j])); } } sourceImg = sourceImg.scaled(50,50); referenceLabel->setPixmap(QPixmap::fromImage(sourceImg)); //加载移动图片 sourceImg.load(":/new/prefix1/blank.jpg"); picLabel[8]->setPixmap(QPixmap::fromImage(sourceImg)); RandomPic();}void PlayWindow::movePic(){ for( int i=0 ; i<3 ; i++){ for( int j=0 ; j<3 ; j++ ){ int index = picFlag[i][j]; picLabel[index]->move((BASEX+PICTURESIZE)*j+BASEX,(BASEY+PICTURESIZE)*i+BASEY); } }}void PlayWindow::RandomPic(){ int x = 2; int y = 2; qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); for( int i=0 ; i<1000 ; i++ ){ int direction = qrand()%4; switch (direction){ case 0://上 if( x>0 ){ picFlag[x][y] = picFlag[x-1][y]; picFlag[x-1][y] = 8; x--; }break; case 1://下 if( x<2 ){ picFlag[x][y] = picFlag[x+1][y]; picFlag[x+1][y] = 8; x++; }break; case 2://左 if( y>0 ){ picFlag[x][y] = picFlag[x][y-1]; picFlag[x][y-1] = 8; y--; }break; case 3://右 if( y<2 ){ picFlag[x][y] = picFlag[x][y+1]; picFlag[x][y+1] = 8; y++; }break; default: break; } } movePic();}void PlayWindow::mousePressEvent(QMouseEvent *event){ int x = -1; int y = -1; if( event->button() == Qt::LeftButton || \ event->button() == Qt::RightButton ){ QPoint mousePoint = event->pos(); if( mousePoint.x() > BASEX && mousePoint.x() < (BASEX+PICTURESIZE)*3 && mousePoint.y() > BASEY && mousePoint.y() < (BASEY+PICTURESIZE)*3 ){ for( int i=0 ; i<3 ; i++){ for( int j=0 ; j<3 ; j++ ){ if( picFlag[i][j] == 8 ){ x = i; y = j; break; } } } int xPos = x*(BASEX+PICTURESIZE)+BASEX; int yPos = y*(BASEY+PICTURESIZE)+BASEY; if( mousePoint.x() < (yPos-BASEY) && mousePoint.x() > (yPos-BASEY-PICTURESIZE) && mousePoint.y() > (xPos) && mousePoint.y() < (xPos+PICTURESIZE)){//左 picFlag[x][y] = picFlag[x][y-1]; picFlag[x][y-1] = 8; stepNum++; movePic(); }else if( mousePoint.x() < (yPos+BASEY+2*PICTURESIZE) && mousePoint.x() > (yPos+BASEY+PICTURESIZE) && mousePoint.y() > (xPos) && mousePoint.y() < (xPos+PICTURESIZE)){//右 picFlag[x][y] = picFlag[x][y+1]; picFlag[x][y+1] = 8; stepNum++; movePic(); }else if( mousePoint.y() < (xPos-BASEX) && mousePoint.y() > (xPos-BASEX-PICTURESIZE) && mousePoint.x() > (yPos) && mousePoint.x() < (yPos+PICTURESIZE)){//上 picFlag[x][y] = picFlag[x-1][y]; picFlag[x-1][y] = 8; stepNum++; movePic(); }else if( mousePoint.y() < (xPos+BASEX+2*PICTURESIZE) && mousePoint.y() > (xPos+BASEX+PICTURESIZE) && mousePoint.x() > (yPos) && mousePoint.x() < (yPos+PICTURESIZE)){//下 picFlag[x][y] = picFlag[x+1][y]; picFlag[x+1][y] = 8; stepNum++; movePic(); } } numLabel->setText(QString::number(stepNum)); checkOk(); }}void PlayWindow::checkOk(){ for( int i=0 ; i<3 ; i++){ for( int j=0 ; j<3 ; j++ ){ if( picFlag[i][j] != i*3+j ){ return; } } } referenceLabel->setText("OK");}void PlayWindow::exit(){ this->close();}PlayWindow::~PlayWindow(){ delete ui;}
//main.c#include "playwindow.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); PlayWindow w; w.show(); return a.exec();}
参考博客链接如下:
http://blog.csdn.net/qq_34886018/article/details/72866807
阅读全文
0 0
- 基于QT实现的简易3*3拼图游戏
- 简易的Java拼图游戏
- 基于Java的拼图游戏
- QT环境下实现UI界面的“拼图游戏”
- as3实现的拼图游戏
- 基于Qt-Creator的简易浏览器
- 基于Qt的简易通用开发框架
- Dreamweaver制作简易的拼图游戏教程
- qt拼图游戏
- Qt5实现简单的拼图游戏
- qt编程实现的简易记事本(范例)
- Qt简易计算器的代码实现
- Qt 实现的简易本地图片浏览器
- Qt 实现简易的学生信息管理文档
- 基于websocket的简易聊天室的实现
- 简易内核调度设计 --基于TQ2440(ARM9)的实现(3)
- 基于unity3d和leap motion的拼图游戏
- 基于广度优先搜索的拼图游戏算法
- java 查找本机ip地址
- centos安装mongodb
- PAT 1005 The Largest Generation (25)
- SpringBoot集成MyBatis时要配置的Mapper中的xml
- ug472--xilinx 时钟资源学习总结(1)
- 基于QT实现的简易3*3拼图游戏
- redis学习笔记五(单机多节点集群)
- List集合去除重复对象及equals()、hashCode()方法的作用
- python:大文件迭代器,GB级别,read(),readline(),readlines()以及语法糖with open()
- Aptana插件版Zip包下载方法
- Java基础之模拟购物系统案例
- 没有树莓派显示器,键盘,鼠标,串口, 树莓派怎么无线上网?
- C++通过特定时间戳转日期
- 【angular】解决跨域问题