QT宝石迷阵
寶石迷陣游戲是一種用戶通過交換相鄰元素的位置形成相同元素的相鄰從而在一定規(guī)則下進(jìn)行消除的游戲,游戲進(jìn)行過程中有一定的時(shí)間限制,在一定的時(shí)間限制之內(nèi)用戶達(dá)到目標(biāo)分?jǐn)?shù)則判定用戶勝利并可進(jìn)入下一關(guān)卡。游戲在一個(gè)二維的圖中進(jìn)行,用戶需先進(jìn)行注冊(cè)登錄后開始游戲,游戲共分為三種模式和三個(gè)關(guān)卡,整個(gè)地圖盤的大小及形狀不同決定游戲的難易程度,不同的界面元素組成不同的生動(dòng)有趣的模式。可選擇單人游戲或聯(lián)網(wǎng)PK。在游戲過程中,用戶可以使用重置功能,同時(shí)游戲進(jìn)行過程中可以暫停游戲。
全部源碼:點(diǎn)贊關(guān)注私信
1.互換:玩家選中相鄰(橫、豎)的兩個(gè)寶石位置發(fā)生互換,如果互換成功則消去寶石,否則取消位置互換。
2.消去:玩家選擇兩個(gè)寶石進(jìn)行位置互換,互換后如果橫排或豎排的有3個(gè)或3個(gè)以上的相同寶石,則消去這幾個(gè)相同寶石,如果互換后沒有可以消去的寶石,則選中的兩個(gè)寶石換回原來的位置。消去后的空格由上面的寶石掉下來補(bǔ)齊。
3.連鎖(Cascade):玩家消去寶石后,上面的寶石掉下來補(bǔ)充空格。如果這時(shí)游戲池中有連續(xù)擺放(橫、豎)的3個(gè)或3個(gè)以上相同的寶石,則可以消去這些寶石,這就是一次連鎖。空格被新的寶石填充,又可以進(jìn)行下一次連鎖。
4.計(jì)分:消除3個(gè)加10分,消除4個(gè)加50分,消除5個(gè)加250分。
5.特殊寶石:消除這些看起來與眾不同的寶石,你將獲得雙倍分?jǐn)?shù)!
6.點(diǎn)擊時(shí)變色,消除有特殊音效
7.進(jìn)度條、定時(shí)間:時(shí)間到結(jié)束游戲
CGameDlg.cpp代碼
#include "CGameDlg.h" #include "ui_CGameDlg.h" #include <QDebug> #include <iostream> #include <qsqlquery.h> #include "ui_CMenuDlg.h" #include "login.h"void SaveScore(QString account ,int score){QString str = QString("update Sheet1$ set score = %1 where 賬號(hào) = %2").arg(login::scoreBegin+score).arg(account);QSqlQuery query;query.exec(str); }CGameDlg::CGameDlg(int id, QWidget *parent) :QDialog(parent),ui(new Ui::CGameDlg) {this->id = id;configIni = new QSettings("../BejeweledSln/config.ini", QSettings::IniFormat);clickjewel = new QSound("../BejeweledSln/sound/clickjewel.wav");xiaoqu = new QSound("../BejeweledSln/sound/xiaoqu.wav");perfect = new QMediaPlayer;perfect->setMedia(QUrl::fromLocalFile("../BejeweledSln/sound/perfect.wav"));perfect->setVolume(70);timeup = new QMediaPlayer;timeup->setMedia(QUrl::fromLocalFile("../BejeweledSln/sound/timeup.wav"));timeup->setVolume(100);player = new QMediaPlayer;player->setMedia(QUrl::fromLocalFile("../BejeweledSln/backgroundMusic/lightning.wav"));player->setVolume(configIni->value("Music/volume").toInt());player->play();if(configIni->value("Switch/BgMusic").toString() == "0")player->setVolume(0);if(configIni->value("Switch/Sound").toString() == "0"){clickjewel = new QSound("../BejeweledSln/sound/wu.wav");xiaoqu = new QSound("../BejeweledSln/sound/wu.wav");perfect->setVolume(0);timeup->setVolume(0);}style = configIni->value("Picture/Style").toString();ui->setupUi(this);ui->background->setPixmap(QPixmap(configIni->value("Picture/BgPic").toString()));ui->background->setScaledContents(true);ui->scoreshow->setText("0");ui->menu->setFocusPolicy(Qt::NoFocus);gamelogic = new CGameLogic(6);gamelogic->init(matrix);std::string path;int x[8] = {50, 100, 150, 200, 250, 300, 350, 400};int y[8] = {20, 70, 120, 170, 220, 270, 320, 370};delete configIni;for(int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){jewel[i][j] = new Mylabel(i, j, x[j], y[i], path, this);if(style == "1"){switch(matrix[i][j]){case 1: path = "../BejeweledSln/image/blue.png";break;case 2: path = "../BejeweledSln/image/green.png";break;case 3: path = "../BejeweledSln/image/lightblue.png";break;case 4: path = "../BejeweledSln/image/orange.png";break;case 5: path = "../BejeweledSln/image/purple.png";break;case 6: path = "../BejeweledSln/image/red.png";break;case 7: path = "../BejeweledSln/image/boom.png";break;}}else{switch(matrix[i][j]){case 1: path = "../BejeweledSln/image/blue1.png";break;case 2: path = "../BejeweledSln/image/green1.png";break;case 3: path = "../BejeweledSln/image/yellow1.png";break;case 4: path = "../BejeweledSln/image/orange1.png";break;case 5: path = "../BejeweledSln/image/purple1.png";break;case 6: path = "../BejeweledSln/image/red1.png";break;case 7: path = "../BejeweledSln/image/boom1.png";break;}}QPixmap pix(path.c_str());jewel[i][j]->setPixmap(pix);jewel[i][j]->path = path;jewel[i][j]->setScaledContents(true);jewel[i][j]->setGeometry(x[j], y[i], 50, 50);jewel[i][j]->installEventFilter(this);jewel[i][j]->show();}}ui->pause->hide();ui->timeout->hide();ui->allcannot->hide();isPause = false;clickflag = 0;kuang = new QLabel(this);kuang->hide();score = 0;ui->progressBar->setRange(0, 120);ui->progressBar->setValue(120);timer = new QTimer(this);timer->setInterval(1000);connect(timer, SIGNAL(timeout()), this, SLOT(updateProgress()));timer->start(); }void CGameDlg::keyPressEvent(QKeyEvent *ev) {if(ev->key() == Qt::Key_B && !isPause){ui->pause->show();ui->pause->raise();isPause = true;timer->stop();ui->pauseorcontinue->setText("Start");return;}else if(ev->key() == Qt::Key_B && isPause){ui->pause->hide();isPause = false;timer->start();ui->pauseorcontinue->setText("Pause");return;}QWidget::keyPressEvent(ev); }void CGameDlg::keyReleaseEvent(QKeyEvent *ev) {QWidget::keyReleaseEvent(ev); }void CGameDlg::closeEvent(QCloseEvent *event) {player->stop();this->close();sleep(1500);event->accept(); }CGameDlg::~CGameDlg() {delete ui; }void CGameDlg::menu() {CMenuDlg* w = new CMenuDlg(this);isPause=false;pauseorcontinue();connect(w->ui->backtohome, SIGNAL(clicked()), this, SLOT(backtohome()));w->show();w->exec(); }void CGameDlg::backtohome() {this->close();player->stop();CBejeweledDlg* w = new CBejeweledDlg(id, this);w->show();w->exec(); }void CGameDlg::pauseorcontinue() {if(isPause){ui->pause->hide();isPause = false;timer->start();ui->pauseorcontinue->setText("Pause");}else{ui->pause->show();ui->pause->raise();isPause = true;timer->stop();ui->pauseorcontinue->setText("Start");} }void CGameDlg::boom(int x,int y) {int xiaoqucount = 0;gamelogic->boom(matrix,x,y);do{xiaoqu->play();drawJewel();sleep(500);gamelogic->xiayi(matrix);drawJewel();score += 300;ui->scoreshow->setText(QString::number(score));sleep(700);}while((xiaoqucount = gamelogic->xiaoqu2(matrix)) != 0);if(gamelogic->all_cannot(matrix)){timer->stop();ui->allcannot->show();ui->allcannot->raise();} }void CGameDlg::tishislot() {Z z1, z2;gamelogic->tishi(matrix, z1, z2);int xpos1 = jewel[z1.x-1][z1.y-1]->xpos;int ypos1 = jewel[z1.x-1][z1.y-1]->ypos;int xpos2 = jewel[z2.x-1][z2.y-1]->xpos;int ypos2 = jewel[z2.x-1][z2.y-1]->ypos;QPropertyAnimation* animation1 = new QPropertyAnimation(jewel[z1.x-1][z1.y-1], "pos");QPropertyAnimation* animation2 = new QPropertyAnimation(jewel[z2.x-1][z2.y-1], "pos");animation1->setDuration(500);animation2->setDuration(500);animation1->setStartValue(QPoint(xpos1, ypos1));animation1->setEndValue(QPoint(xpos2, ypos2));animation2->setStartValue(QPoint(xpos2, ypos2));animation2->setEndValue(QPoint(xpos1, ypos1));animation1->start(QAbstractAnimation::DeleteWhenStopped);animation2->start(QAbstractAnimation::DeleteWhenStopped);sleep(700);int xiaoqucount = 0;xiaoqucount = gamelogic->jiaohuan1(matrix, z1, z2);do{xiaoqu->play();drawJewel();sleep(700);gamelogic->xiayi(matrix);drawJewel();score -= 20*xiaoqucount;ui->scoreshow->setText(QString::number(score));sleep(500);}while((xiaoqucount = gamelogic->xiaoqu2(matrix)) != 0);if(gamelogic->all_cannot(matrix)){timer->stop();ui->allcannot->show();ui->allcannot->raise();}}void CGameDlg::updateProgress() {int nCurrentValue = ui->progressBar->value();nCurrentValue--;if(nCurrentValue <= 0){timer->stop();ui->timeout->raise();ui->timeout->show();ui->pauseorcontinue->setEnabled(false);ui->tishi->setEnabled(false);timeup->play();//SaveScore(login::account,score);CTimeup* w = new CTimeup(id, this);this->close();w->setWindowModality(Qt::ApplicationModal);w->show();w->exec();}ui->progressBar->setValue(nCurrentValue); }void CGameDlg::sleep(unsigned int msec){QTime reachTime=QTime::currentTime().addMSecs(msec);while(QTime::currentTime()<reachTime){QCoreApplication::processEvents(QEventLoop::AllEvents,100);} }void CGameDlg::drawJewel() {for(int i = 0; i < 8; i++)for(int j = 0; j < 8; j++)jewel[i][j]->close();std::string path;int x[8] = {50, 100, 150, 200, 250, 300, 350, 400};int y[8] = {20, 70, 120, 170, 220, 270, 320, 370};QMovie* movie = new QMovie("../BejeweledSln/image/boom.gif");bool isContinue = false;for(int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){jewel[i][j] = new Mylabel(i, j, x[j], y[i], path, this);if(style == "1"){switch(matrix[i][j]){case 1: path = "../BejeweledSln/image/blue.png";break;case 2: path = "../BejeweledSln/image/green.png";break;case 3: path = "../BejeweledSln/image/lightblue.png";break;case 4: path = "../BejeweledSln/image/orange.png";break;case 5: path = "../BejeweledSln/image/purple.png";break;case 6: path = "../BejeweledSln/image/red.png";break;case 7: path = "../BejeweledSln/image/boom.png";break;case 0:jewel[i][j]->setStyleSheet("");jewel[i][j]->setMovie(movie);jewel[i][j]->setGeometry(x[j], y[i], 50, 50);jewel[i][j]->setScaledContents(true);jewel[i][j]->show();movie->start();isContinue = true;}}else{switch(matrix[i][j]){case 1: path = "../BejeweledSln/image/blue1.png";break;case 2: path = "../BejeweledSln/image/green1.png";break;case 3: path = "../BejeweledSln/image/yellow1.png";break;case 4: path = "../BejeweledSln/image/orange1.png";break;case 5: path = "../BejeweledSln/image/purple1.png";break;case 6: path = "../BejeweledSln/image/red1.png";break;case 7: path = "../BejeweledSln/image/boom1.png";break;case 0:jewel[i][j]->setStyleSheet("");jewel[i][j]->setMovie(movie);jewel[i][j]->setGeometry(x[j], y[i], 50, 50);jewel[i][j]->setScaledContents(true);jewel[i][j]->show();movie->start();isContinue = true;}}if(isContinue){isContinue = false;continue;}QPixmap pix(path.c_str());jewel[i][j]->setPixmap(pix);jewel[i][j]->path = path;jewel[i][j]->setScaledContents(true);jewel[i][j]->setGeometry(x[j], y[i], 50, 50);jewel[i][j]->installEventFilter(this);jewel[i][j]->show();}} }bool CGameDlg::eventFilter(QObject*obj,QEvent* e) {int xiaoqucount = 0;if(e->type() == QEvent::MouseButtonPress){if(clickflag == 0){for(int i = 0; i < 8; i++){for(int j = 0; j < 8; j++){if(obj == jewel[i][j]){if(matrix[i][j]==7){boom(i,j);return false;}clickjewel->play();jewel1I = i;jewel1J = j;clickflag = 1;QPixmap pix("../BejeweledSln/image/kuang.png");kuang->setPixmap(pix);kuang->setGeometry(jewel[i][j]->xpos, jewel[i][j]->ypos, 50, 50);kuang->setScaledContents(true);kuang->show();return false;}}}}else if(clickflag == 1){for(int i = 0; i < 8; i++){for(int j = 0; j < 8; j++){if(obj == jewel[i][j]){if(matrix[i][j]==7){boom(i,j);return false;}clickjewel->play();kuang->hide();if((i == jewel1I-1 && j == jewel1J)|| (i == jewel1I+1 && j == jewel1J)|| (j == jewel1J -1 && i == jewel1I) || (j == jewel1J+1 && i == jewel1I)){//two jewel switch animationint xpos1 = jewel[jewel1I][jewel1J]->xpos;int ypos1 = jewel[jewel1I][jewel1J]->ypos;int xpos2 = jewel[i][j]->xpos;int ypos2 = jewel[i][j]->ypos;QPropertyAnimation* animation1 = new QPropertyAnimation(jewel[jewel1I][jewel1J], "pos");QPropertyAnimation* animation2 = new QPropertyAnimation(jewel[i][j], "pos");animation1->setDuration(500);animation2->setDuration(500);animation1->setStartValue(QPoint(xpos1, ypos1));animation1->setEndValue(QPoint(xpos2, ypos2));animation2->setStartValue(QPoint(xpos2, ypos2));animation2->setEndValue(QPoint(xpos1, ypos1));animation1->start(QAbstractAnimation::DeleteWhenStopped);animation2->start(QAbstractAnimation::DeleteWhenStopped);sleep(700);Z z1, z2;z1.x = jewel[jewel1I][jewel1J]->x+1;z1.y = jewel[jewel1I][jewel1J]->y+1;z2.x = jewel[i][j]->x+1;z2.y = jewel[i][j]->y+1;if((xiaoqucount = gamelogic->jiaohuan1(matrix, z1, z2)) == 0){QPropertyAnimation* animation1 = new QPropertyAnimation(jewel[jewel1I][jewel1J], "pos");QPropertyAnimation* animation2 = new QPropertyAnimation(jewel[i][j], "pos");animation1->setDuration(500);animation2->setDuration(500);animation1->setStartValue(QPoint(xpos2, ypos2));animation1->setEndValue(QPoint(xpos1, ypos1));animation2->setStartValue(QPoint(xpos1, ypos1));animation2->setEndValue(QPoint(xpos2, ypos2));animation1->start(QAbstractAnimation::DeleteWhenStopped);animation2->start(QAbstractAnimation::DeleteWhenStopped);sleep(700);clickflag = 0;return false;}else{do{xiaoqu->play();drawJewel();sleep(500);gamelogic->xiayi(matrix);drawJewel();score += 100*xiaoqucount;if(xiaoqucount >= 6)perfect->play();ui->scoreshow->setText(QString::number(score));sleep(400);}while((xiaoqucount = gamelogic->xiaoqu2(matrix)) != 0);}if(gamelogic->all_cannot(matrix)){timer->stop();ui->allcannot->show();ui->allcannot->raise();}}clickflag = 0;return false;}}}}}return false; }總結(jié)
- 上一篇: 前端——获取手机验证码案例
- 下一篇: 社保详解