歡迎來(lái)到裝配圖網(wǎng)! | 幫助中心 裝配圖網(wǎng)zhuangpeitu.com!
裝配圖網(wǎng)
ImageVerifierCode 換一換
首頁(yè) 裝配圖網(wǎng) > 資源分類(lèi) > DOC文檔下載  

Qt開(kāi)發(fā)源碼(俄羅斯方塊)

  • 資源ID:57373969       資源大?。?span id="cpsht0c" class="font-tahoma">63.50KB        全文頁(yè)數(shù):21頁(yè)
  • 資源格式: DOC        下載積分:0積分
快捷下載 游客一鍵下載
會(huì)員登錄下載
微信登錄下載
三方登錄下載: 微信開(kāi)放平臺(tái)登錄 支付寶登錄   QQ登錄   微博登錄  
二維碼
微信掃一掃登錄
下載資源需要0積分
郵箱/手機(jī):
溫馨提示:
用戶(hù)名和密碼都是您填寫(xiě)的郵箱或者手機(jī)號(hào),方便查詢(xún)和重復(fù)下載(系統(tǒng)自動(dòng)生成)
支付說(shuō)明:
本站最低充值0.01積分,下載本資源后余額將會(huì)存入您的賬戶(hù),您可在我的個(gè)人中心查看。
驗(yàn)證碼:   換一換

 
賬號(hào):
密碼:
驗(yàn)證碼:   換一換
  忘記密碼?
    
友情提示
2、PDF文件下載后,可能會(huì)被瀏覽器默認(rèn)打開(kāi),此種情況可以點(diǎn)擊瀏覽器菜單,保存網(wǎng)頁(yè)到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請(qǐng)使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無(wú)水印,預(yù)覽文檔經(jīng)過(guò)壓縮,下載后原文更清晰。
5、試題試卷類(lèi)文檔,如果標(biāo)題沒(méi)有明確說(shuō)明有答案則都視為沒(méi)有答案,請(qǐng)知曉。

Qt開(kāi)發(fā)源碼(俄羅斯方塊)

真誠(chéng)為您提供優(yōu)質(zhì)參考資料,若有不當(dāng)之處,請(qǐng)指正。俄羅斯方塊游戲Main.cpp主程序代碼:#include <QtGui>#include <stdlib.h>#include "tetrixwindow.h"int main(int argc, char *argv) / 為了能夠正常顯示中文,設(shè)置Tr編碼環(huán)境為GB2312 (詳見(jiàn)wiki) QTextCodec:setCodecForTr(QTextCodec:codecForName("GB2312"); / app這個(gè)對(duì)象用于管理應(yīng)用級(jí)別的資源 QApplication app(argc, argv); app.setStyleSheet("TetrixBoard background-color:lightGray"); TetrixWindow window; window.setWindowIcon(QIcon(":/Chrome.ico"); window.show(); / 當(dāng)前時(shí)間作為隨機(jī)種子 qsrand(QTime(0,0,0).secsTo(QTime:currentTime(); return app.exec(); / 程序的事件循環(huán)Tetrixboard.h頭文件代碼:/ 主游戲區(qū)類(lèi)#ifndef TETRIXBOARD_H#define TETRIXBOARD_H#include "tetrixpiece.h"#include <QBasicTimer>#include <QPointer>#include <QFrame>#include <QSound>/ 前向聲明class QLabel;class TetrixBoard : public QFrame Q_OBJECTpublic: TetrixBoard(QWidget *parent = 0); void setNextPieceLabel(QLabel *label); QSize sizeHint() const;/最適合大小 QSize minimumSizeHint() const; / 最小限制public slots: / 公有槽 void start(); void pause();signals: / 信號(hào):只需聲明,根據(jù)參數(shù)變化來(lái)判斷 void scoreChanged(int score); void levelChanged(int level); void linesRemovedChanged(int numLines);protected: / 著色、鍵盤(pán)、計(jì)時(shí)事件:其中著色事件隨著update()不斷觸發(fā) void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent *event); void timerEvent(QTimerEvent *event);private: enum BoardWidth = 10, BoardHeight = 22 ; / 把主游戲區(qū)寬分成10等份,高分成22等份,也就是說(shuō)每行有10小矩形,總共有22行 TetrixShape &shapeAt(int x, int y) return board(y * BoardWidth) + x; int timeoutTime() return 1000 / (1 + level); / contentsRect():返回當(dāng)前布局(QLayout)的矩形,可訪(fǎng)問(wèn)其長(zhǎng)、寬 (詳見(jiàn)API) / conntentsRect().width()/BoardWidth 把游戲區(qū)矩形的寬分成了BoardWidth份 int squareWidth() return contentsRect().width() / BoardWidth; / 同上,把高分成了BoardHeight份 int squareHeight() return contentsRect().height() / BoardHeight; / 此時(shí)squareWidth(),squareHeight()分別是分割后的小矩形寬和高 void clearBoard(); / 清屏 void dropDown(); / 下落事件 void oneLineDown();/ 下落一行 void pieceDropped(int dropHeight); void removeFullLines(); / 移除填滿(mǎn)的行 void newPiece(); / 新方塊 void showNextPiece(); / 顯示下一個(gè)方塊 bool tryMove(const TetrixPiece &newPiece, int newX, int newY); / 判斷方塊是否可以移動(dòng) void drawSquare(QPainter &painter, int x, int y, TetrixShape shape); / 著色 QBasicTimer timer; / 相當(dāng)于QLabel *nextPieceLabel(QPointer詳見(jiàn)API) QPointer<QLabel> nextPieceLabel; bool isStarted; bool isPaused; bool isWaitingAfterLine; TetrixPiece curPiece; / 當(dāng)前方塊 TetrixPiece nextPiece; / 下一個(gè)方塊 int curX; int curY; int numLinesRemoved; int numPiecesDropped; int score; int level; TetrixShape boardBoardWidth * BoardHeight;#endif / TETRIXBOARD_HTetrixboard.cpp程序代碼:#include <QtGui>#include "tetrixboard.h"TetrixBoard:TetrixBoard(QWidget *parent) : QFrame(parent) / 設(shè)置游戲區(qū)框架風(fēng)格:內(nèi)浮雕 setFrameStyle(QFrame:Panel | QFrame:Sunken); / 增加游戲區(qū)鍵盤(pán)鼠標(biāo)等事件的焦點(diǎn)集中 setFocusPolicy(Qt:StrongFocus); isStarted = false; / 初始化:未開(kāi)始狀態(tài) isPaused = false; clearBoard(); / 初始清屏 nextPiece.setRandomShape(); / 下一方塊獲得一個(gè)隨機(jī)形狀/ tetrixpiece.h : tetrixpiece.cpp中使用void TetrixBoard:setNextPieceLabel(QLabel *label) nextPieceLabel = label;/ 游戲區(qū)合適大小QSize TetrixBoard:sizeHint() const return QSize(BoardWidth*15 + frameWidth()*2, BoardHeight*15 + frameWidth()*2);/ 游戲區(qū)最小大小QSize TetrixBoard:minimumSizeHint() const return QSize(BoardWidth*5 + frameWidth()*2, BoardHeight*5 + frameWidth()*2);/ 開(kāi)始事件:slotsvoid TetrixBoard:start() / 如果已暫停,則啟動(dòng)無(wú)效 if (isPaused) return; isStarted = true; / 標(biāo)記已開(kāi)始 isWaitingAfterLine = false; / 此參數(shù)為判斷是否有方塊正在下落,false為有方塊正在下落中 / 初始各參數(shù) numLinesRemoved = 0; numPiecesDropped = 0; score = 0; level = 1; clearBoard(); / 清屏 / emit 信號(hào)發(fā)射:觸發(fā)對(duì)應(yīng)信號(hào)槽內(nèi)的函數(shù)(相關(guān)connect()在tetrixwindow.cpp中) emit linesRemovedChanged(numLinesRemoved); emit scoreChanged(score); emit levelChanged(level); newPiece(); / 調(diào)用新方塊 timer.start(timeoutTime(), this);/ 游戲開(kāi)始計(jì)時(shí)/ 暫停事件:slotsvoid TetrixBoard:pause() / 如果未開(kāi)始,則暫停無(wú)效 if (!isStarted) return; / 否則,若未暫停,則賦值為暫停,反之,取消暫停,繼續(xù)游戲 isPaused = !isPaused; if (isPaused) timer.stop(); / 游戲計(jì)時(shí)停止 else timer.start(timeoutTime(), this); / 否則繼續(xù)計(jì)時(shí) update(); / 刷新窗口:動(dòng)態(tài)顯示畫(huà)面/ 游戲區(qū)方塊著色/ 重定義繪圖事件,當(dāng)調(diào)用update()時(shí)進(jìn)行重繪void TetrixBoard:paintEvent(QPaintEvent *event) QFrame:paintEvent(event); QPainter painter(this); QRect rect = contentsRect(); / QRect定義了平面上的矩形 (詳見(jiàn)API),是主游戲區(qū) / 暫停的時(shí)候顯示的信息 if (isPaused) painter.drawText(rect, Qt:AlignCenter, tr("游戲暫停"); return; / BoardHeight*squareHeight() 相當(dāng)于 contentsRect().Height(),是小網(wǎng)格的高 / 因?yàn)閟quareHeight() return contentsRect().Width()/BoardWidth(); / 見(jiàn)tetrixboard.h中的定義 int boardTop = rect.bottom() - BoardHeight*squareHeight(); for (int i=0; i<BoardHeight; +i) for (int j=0; j<BoardWidth; +j) / TetrixShape &shapeAt(int x, int y) return board(y * BoardWidth) + x; TetrixShape shape = shapeAt(j, BoardHeight-i-1); if (shape != NoShape) / rect.left() 返回游戲區(qū)矩形左邊的x坐標(biāo),squareWidth()為小網(wǎng)格的寬度 drawSquare(painter, rect.left() + j*squareWidth(), boardTop + i*squareHeight(), shape); / 繪圖 if (curPiece.shape() != NoShape) for (int i=0; i<4; +i) int x = curX + curPiece.x(i); int y = curY - curPiece.y(i); drawSquare(painter, rect.left() + x*squareWidth(), boardTop + (BoardHeight - y - 1 )*squareHeight(), curPiece.shape(); / 鍵盤(pán)事件void TetrixBoard:keyPressEvent(QKeyEvent *event) if (!isStarted | isPaused | curPiece.shape() = NoShape) QFrame:keyPressEvent(event); return; switch (event->key() case Qt:Key_Left: tryMove(curPiece, curX-1, curY); / 左移 break; case Qt:Key_Right: tryMove(curPiece, curX+1, curY); / 右移 break; case Qt:Key_Up: tryMove(curPiece.rotatedLeft(),curX,curY); / 方塊左轉(zhuǎn) break; case Qt:Key_Down: dropDown(); / 快速下落 break; default: QFrame:keyPressEvent(event); / 計(jì)時(shí)時(shí)間void TetrixBoard:timerEvent(QTimerEvent *event) if (event->timerId() = timer.timerId() / 如果還有方塊已下落完畢 if (isWaitingAfterLine) isWaitingAfterLine = false; / 重標(biāo)記為有方塊正在下落 newPiece(); / 添加新方塊 (timeoutTime(), this); else oneLineDown(); /否則進(jìn)行下落動(dòng)作 else QFrame:timerEvent(event); / 清空游戲區(qū)所有繪圖void TetrixBoard:clearBoard() for (int i=0; i<BoardHeight * BoardWidth; +i) boardi = NoShape;/ 直接快速下落操作void TetrixBoard:dropDown() int dropHeight = 0; int newY = curY; / 進(jìn)行下落過(guò)程,并求得方塊還能下落的最大高度 while (newY > 0) if (!tryMove(curPiece,curX,newY-1) break; -newY; +dropHeight; / 把下落高度傳遞給此函數(shù) pieceDropped(dropHeight);/ 正常下落操作void TetrixBoard:oneLineDown() if (!tryMove(curPiece, curX,curY-1) / 如果能移動(dòng),則下落一行 pieceDropped(0);/ 正常下落不幾分/ 進(jìn)行方塊下落后的行為,如繪圖,加分等 參數(shù):下落方塊的高度void TetrixBoard:pieceDropped(int dropHeight) for (int i=0; i<4; +i) int x = curX + curPiece.x(i); int y = curY - curPiece.y(i); shapeAt(x,y) = curPiece.shape(); +numPiecesDropped; / 等級(jí)劃分,加快下落速度 if (numPiecesDropped % 25 = 0) +level; timer.start(timeoutTime(), this); / 加速,游戲時(shí)間加快 emit levelChanged(level); emit scoreChanged(score); / 判斷是否已有滿(mǎn)行 removeFullLines(); if (!isWaitingAfterLine) newPiece();/ 移除整行void TetrixBoard:removeFullLines() int numFullLines = 0; / 循環(huán)判斷有幾行已滿(mǎn) for (int i = BoardHeight-1; i >= 0; -i) bool lineIsFull = true; / 判斷是否已滿(mǎn)一行 for (int j=0; j < BoardWidth; +j) if (shapeAt(j,i)=NoShape) lineIsFull = false; break; / 上面所有行下移 if (lineIsFull) +numFullLines; for(int k = i; k < BoardHeight-1; +k) for (int j = 0; j < BoardWidth; +j) shapeAt(j,k) = shapeAt(j, k+1); / 整行清零 for (int j = 0; j < BoardWidth; +j) shapeAt(j, BoardHeight-1) = NoShape; score += numFullLines-1 ; / 如果已滿(mǎn)行數(shù)大于0,則進(jìn)行加分等操作,并更新窗口 if (numFullLines > 0) numLinesRemoved += numFullLines; score += 10 * numFullLines; / 同時(shí)發(fā)送信號(hào)至相應(yīng)的槽 emit linesRemovedChanged(numLinesRemoved); emit scoreChanged(score); (500,this); isWaitingAfterLine = true; curPiece.setShape(NoShape); update(); / 新方塊void TetrixBoard:newPiece() curPiece = nextPiece; / 預(yù)先隨機(jī)設(shè)置好一下塊方塊 nextPiece.setRandomShape(); showNextPiece(); / 設(shè)置其初始下落的位置,在游戲區(qū)頂部中央 curX = BoardWidth / 2 + 1; curY = BoardHeight-1 + curPiece.minY(); / 判斷其是否還能移動(dòng),如果不能,則停止游戲 if (!tryMove(curPiece, curX, curY) curPiece.setShape(NoShape);/ painter.drawText(rect,tr("游戲結(jié)束"); timer.stop(); isStarted = false; / 展示下一個(gè)方塊void TetrixBoard:showNextPiece() if (!nextPieceLabel) return; int dx = nextPiece.maxX() - nextPiece.minX() + 1; int dy = nextPiece.maxY() - nextPiece.minY() + 1; QPixmap pixmap(dx *squareWidth(), dy*squareHeight(); /映射要顯示方塊像素 QPainter painter(&pixmap); / 開(kāi)始繪制該方塊 painter.fillRect(pixmap.rect(), nextPieceLabel->palette().background(); / 先繪制要顯示方塊的背景色 / 再開(kāi)始繪制方塊本身 for (int i = 0; i < 4; +i) int x = nextPiece.x(i) - nextPiece.minX(); int y = nextPiece.y(i) - nextPiece.minY(); drawSquare(painter, x*squareWidth(), y*squareHeight(), nextPiece.shape(); nextPieceLabel->setPixmap(pixmap);/ 最后加載它/ 判斷是否還能移動(dòng)bool TetrixBoard:tryMove(const TetrixPiece &newPiece, int newX, int newY) for (int i = 0; i < 4; +i) int x = newX + newPiece.x(i); int y = newY - newPiece.y(i); if (x < 0 | x >= BoardWidth | y < 0 | y >= BoardHeight) return false; if (shapeAt(x,y) != NoShape) / 判斷當(dāng)前位置是否有其他方塊 return false; curPiece = newPiece; curX = newX; curY = newY; update(); return true;/ int squareWidth() return contentsRect().width() / BoardWidth; / int squareHeight() return contentsRect().height() / BoardHeight; void TetrixBoard:drawSquare(QPainter &painter, int x,int y,TetrixShape shape) / google色彩 static const QRgb colorTable8 = 0x000000, 0x1851ce, 0xc61800, 0xefba00, 0x1851ce, 0x1ba823, 0xc61800, 0x606060 ; QColor color = colorTableint(shape); / 填充單元網(wǎng)格的顏色 / void fillRect(int x, int y, int width, int height, const QColor & color) painter.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2, color); painter.setPen(color.light(); / 左上角邊框顏色 / void drawLine(int x1, int y1, int x2, int y2) painter.drawLine(x, y + squareHeight() - 1, x, y); painter.drawLine(x, y, x + squareWidth() - 1, y); painter.setPen(color.dark(); / 右下角邊框顏色 painter.drawLine(x + 1, y + squareHeight() - 1, x + squareWidth() - 1, y + squareHeight() - 1); painter.drawLine(x + squareWidth() - 1, y + squareHeight() - 1, x + squareWidth() - 1, y + 1);Tetrixpiece.h頭文件代碼:/ 俄羅斯方塊類(lèi):方塊形狀/旋轉(zhuǎn)情況#ifndef TETRIXPIECE_H#define TETRIXPIECE_H/ 定義一個(gè)枚舉類(lèi)型(0-7):無(wú)形狀、Z字形、S字形、直線(xiàn)形,T字形,田字形形、L字形,翻轉(zhuǎn)L字形enum TetrixShape NoShape, ZShape,SShape,LineShape,TShape,SquareShape, LShape,MirroredLShape ;class TetrixPiecepublic: TetrixPiece() setShape(NoShape); / inline構(gòu)造函數(shù):初始設(shè)置成NoShape void setRandomShape(); / 設(shè)置隨機(jī)規(guī)則 void setShape(TetrixShape shape); / 構(gòu)造方塊形狀的數(shù)據(jù)結(jié)構(gòu) / 通過(guò)inline公有函數(shù)成員shape()訪(fǎng)問(wèn)私有數(shù)據(jù)成員pieceShape TetrixShape shape() const return pieceShape; int x(int index) const return coordsindex0; int y(int index) const return coordsindex1; int minX() const; int maxX() const; int minY() const; int maxY() const; TetrixPiece rotatedLeft() const; / 左轉(zhuǎn)private: void setX(int index, int x) coordsindex0 = x; void setY(int index, int y) coordsindex1 = y; TetrixShape pieceShape; / 枚舉類(lèi)型創(chuàng)建一個(gè)該枚舉類(lèi)型對(duì)象 int coords42;#endif / TETRIXPIECE_HTetrixpiece.cpp程序代碼:#include <QtCore>#include "tetrixpiece.h"#include <QObject>#include <stdlib.h>#include <QTime>/ 偽隨機(jī)函數(shù):利用當(dāng)前系統(tǒng)時(shí)間增加隨機(jī)性void TetrixPiece:setRandomShape() /QTime time = QTime:currentTime(); /qsrand( time.msec() + time.second()*1000 );/ 重設(shè)隨機(jī)種子:當(dāng)前時(shí)間為隨機(jī)變量 setShape(TetrixShape(qrand()%7 + 1); / 共六種方塊形狀void TetrixPiece:setShape(TetrixShape shape) / 按TetrixShape枚舉順序構(gòu)建: / 除NoShape外,每個(gè)形狀由4個(gè)小方塊組成,這里每行的四個(gè)坐標(biāo)即4個(gè)小方塊的坐標(biāo),其中橫向?yàn)閄,縱向?yàn)閅 / ZShape SShape LineShape TShape SquareShape LShape MirroredLShape/ -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2/ -1 * -1 * -1 * -1 -1 -1 * * -1 * */ 0 * * 0 * * 0 * 0 * * * 0 * * 0 * 0 */ 1 * 1 * 1 * 1 * 1 * * 1 * 1 */ 2 2 2 * 2 2 2 2 static const int coordsTable842 = 0, 0 , 0, 0 , 0, 0 , 0, 0 ,/ NoShape 0, -1 , 0, 0 , -1, 0 , -1, 1 , 0, -1 , 0, 0 , 1, 0 , 1, 1 , 0, -1 , 0, 0 , 0, 1 , 0, 2 , -1, 0 , 0, 0 , 1, 0 , 0, 1 , 0, 0 , 1, 0 , 0, 1 , 1, 1 , -1, -1 , 0, -1 , 0, 0 , 0, 1 , 1, -1 , 0, -1 , 0, 0 , 0, 1 ; for (int i=0; i<4; i+) for (int j=0; j<2; +j) coordsij = coordsTableshapeij; pieceShape = shape;/ 獲取最小X坐標(biāo)值,QtGlobal:qMinint TetrixPiece:minX() const int min = coords00; for (int i = 1; i < 4; +i) min = qMin(min, coordsi0); return min;/ 獲取最大X坐標(biāo)值,QtGlobal:qMaxint TetrixPiece:maxX() const int max = coords00; for (int i = 1; i < 4; +i) max = qMax(max, coordsi0); return max;/ 獲取最小Y坐標(biāo)值int TetrixPiece:minY() const int min = coords01; for (int i = 1; i < 4; +i) min = qMin(min, coordsi1); return min;/ 獲取最大Y坐標(biāo)值int TetrixPiece:maxY() const int max = coords01; for (int i = 0; i < 4; +i) max = qMax(max, coordsi1); return max;/ 按順時(shí)針?lè)较蜃筠D(zhuǎn)90度:/ x = -y;/ y = x;TetrixPiece TetrixPiece:rotatedLeft() const if (pieceShape = SquareShape) return *this; TetrixPiece result; result.pieceShape = pieceShape; for (int i = 0; i < 4; +i) result.setX(i,-y(i); result.setY(i,x(i); return result;Tetrixwindow.h頭文件代碼:/ 主窗口類(lèi):控制區(qū)/游戲區(qū)#ifndef TETRIXWINDOW_H#define TETRIXWINDOW_H#include <QFrame>#include <QWidget>/* 前向聲明: 因?yàn)榇颂幹皇褂眠@些類(lèi)的指針,沒(méi)有涉及相關(guān)函數(shù), 并不需要include它們的頭文件,那樣會(huì)減慢編譯速度 */QT_BEGIN_NAMESPACEclass QLCDNumber;class QLabel;class QPushButton;QT_END_NAMESPACEclass TetrixBoard;class TetrixWindow : public QWidget / 這個(gè)宏定義涉及到Qt高級(jí)部分,我們現(xiàn)在只需知道 / tr()connecet()slotssignals與之有關(guān) Q_OBJECTpublic: TetrixWindow();public slots: void About();private: /* 聲明需要用到的一些部件的指針,包括:開(kāi)始、暫停、退出按鈕, 分?jǐn)?shù)、等級(jí)、消去行數(shù)、下一個(gè)方塊顯示區(qū)等*/ QLabel *createLabel(const QString &text); TetrixBoard *board; / 自定義游戲邏輯類(lèi)對(duì)象指針 QLabel *nextPieceLabel; QLCDNumber *scoreLcd; QLCDNumber *levelLcd; QLCDNumber *linesLcd; QPushButton *startButton; QPushButton *quitButton; QPushButton *pauseButton; QPushButton *aboutButton;#endif /TETRIXWINDOW_HTetrixwindow.cpp程序代碼:#include <QtGui>#include "tetrixboard.h"#include "tetrixwindow.h"/ 構(gòu)造函數(shù):將聲明的抽象部件實(shí)例化TetrixWindow:TetrixWindow() board = new TetrixBoard; / board為主游戲區(qū) nextPieceLabel = new QLabel; / Raised:浮雕 Box:draw a box around its contents /nextPieceLabel->setFrameStyle(QFrame:Box | QFrame:Raised); nextPieceLabel->setAlignment(Qt:AlignCenter); / 居中顯示 / 通過(guò)該函數(shù)將nextPieceLabel賦值給board對(duì)象私有變量nextPieceLabel: / 不是友元或繼承關(guān)系無(wú)法直接調(diào)用其私有成員 board->setNextPieceLabel(nextPieceLabel); / Lcd數(shù)字顯示器 scoreLcd = new QLCDNumber(5); / 最大為5位數(shù) scoreLcd->setSegmentStyle(QLCDNumber:Filled); / 浮雕顯示數(shù)字 (詳見(jiàn)API) levelLcd = new QLCDNumber(5); levelLcd->setSegmentStyle(QLCDNumber:Filled); linesLcd = new QLCDNumber(5); linesLcd->setSegmentStyle(QLCDNumber:Filled); / 按鈕實(shí)例化:設(shè)置為NoFocus,避免與游戲按鍵沖突 startButton = new QPushButton(tr("&開(kāi)始"); startButton->setFocusPolicy(Qt:NoFocus); quitButton = new QPushButton(tr("&退出"); quitButton->setFocusPolicy(Qt:NoFocus); pauseButton = new QPushButton(tr("&暫停"); pauseButton->setFocusPolicy(Qt:NoFocus); aboutButton = new QPushButton(tr("&關(guān)于"); aboutButton->setFocusPolicy(Qt:NoFocus); / 相應(yīng)信號(hào)槽連接Qt核心:QObject:connect(信號(hào)發(fā)射對(duì)象,SIGNAL(信號(hào)),接收對(duì)象,SLOT(觸發(fā)槽) / 信號(hào)(signals):可以只聲明不定義; 觸發(fā)槽(slots):信號(hào)響應(yīng)函數(shù) connect(startButton, SIGNAL(clicked(), board, SLOT(start(); connect(quitButton, SIGNAL(clicked(), qApp, SLOT(quit(); connect(pauseButton, SIGNAL(clicked(), board, SLOT(pause(); connect(aboutButton, SIGNAL(clicked(), this, SLOT(About(); connect(board, SIGNAL(scoreChanged(int), scoreLcd, SLOT(display(int); connect(board, SIGNAL(levelChanged(int), levelLcd, SLOT(display(int); connect(board, SIGNAL(linesRemovedChanged(int), linesLcd, SLOT(display(int); / 將各部件(Widget)按網(wǎng)格排列 QGridLayout *layout = new QGridLayout; layout->addWidget(createLabel(tr("下一個(gè)"),0,0); layout->addWidget(nextPieceLabel,1,0,3,1); layout->addWidget(createLabel(tr("關(guān)卡"),4,0); layout->addWidget(levelLcd,5,0); / 主游戲區(qū) layout->addWidget(board,0,1,14,2); layout->addWidget(createLabel(tr("分?jǐn)?shù)"),6,0); layout->addWidget(scoreLcd,7,0); layout->addWidget(createLabel(tr("消行"),8,0); layout->addWidget(linesLcd,9,0); layout->addWidget(startButton,10,0); layout->addWidget(pauseButton,11,0); layout->addWidget(aboutButton,12,0); layout->addWidget(quitButton,13,0); setLayout(layout); / 設(shè)置該布局,使之有效 / QWidget: setWindowTitle(tr("俄羅斯方塊"); / 設(shè)置主窗口標(biāo)題 int w = 423; int h = 530; resize(w,h); / 設(shè)置主窗口默認(rèn)大小 寬*高 setMaximumSize(w,h); setMinimumSize(w,h);/ 重定義QLabel類(lèi)對(duì)象:使之居中,置底顯示QLabel *TetrixWindow:createLabel(const QString &text) QLabel *lbl = new QLabel(text); lbl->setAlignment(Qt:AlignHCenter | Qt:AlignBottom); return lbl;void TetrixWindow:About() QMessageBox:information(NULL, tr("關(guān)于"), tr(" 作 者 : 曾奇凡 <h3>Thanks Qt4.7</h3>n <p>版本: 1.0.0t日期: 2012.5.15</p><a href="21 / 21

注意事項(xiàng)

本文(Qt開(kāi)發(fā)源碼(俄羅斯方塊))為本站會(huì)員(優(yōu)***)主動(dòng)上傳,裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。 若此文所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng)(點(diǎn)擊聯(lián)系客服),我們立即給予刪除!

溫馨提示:如果因?yàn)榫W(wǎng)速或其他原因下載失敗請(qǐng)重新下載,重復(fù)下載不扣分。




關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話(huà):18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶(hù)上傳的文檔直接被用戶(hù)下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!