版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上2018-2019學年下學期嵌入式Linux應用程序開發(fā)期末大作業(yè)專 業(yè): 軟件工程 班 級: 1603 學 號: 5姓 名: 趙亮 任課教師: 薛正元 成 績: 題目內容:在Linux下,用qt編程實現(xiàn)一個小游戲,2048.整體的代碼結構如圖1: 圖1完成后預覽如圖2: 圖2游戲主邏輯說明:1初始生成兩個值,要么2,要么42 移動(上下左右四個方向):首先,在行/列上找到當前行第一個為空的值,記錄下該位置,再往后找到第一個不為空的值,最后將這兩個位置交換。3 合并:1:在 2.移動 中,邊界值為空 當交換后的位置與交換后的位置的前一個位置(簡稱前一個位置)的值相等,
2、前一個位置值*2,刪除要移動的值。2:在 2.移動 中,邊界值不為空 判斷邊界值是否與后面第一個不為空的值相等3: 相等,邊界值*2,刪除第一個不為空的值4:不相等,不做任何操作4:游戲結束:如果出現(xiàn)2048,贏,游戲結束,當方格填滿,沒有合并項,失敗,游戲結束1.注:要記錄下該位置在同一回合中是否合并過,避免同一回合多次合并核心步驟:1設定背景樣式:void BGWidget:paintEvent(QPaintEvent *event) QStylePainter painter(this); /用style畫背景 (會使用setstylesheet中的內容) QStyleOption op
3、t; opt.initFrom(this); opt.rect=rect(); painter.drawPrimitive(QStyle:PE_Widget, opt); painter.setPen(QColor(204,192,180); painter.setBrush(QColor(204,192,180); /4*4的背景矩陣 const int colWidth = 75; const int rowHeight = 75; const int xOffset = 10; const int yOffset = 10; for(int row = 0; row < 4;+ro
4、w) for(int col = 0; col < 4; +col) /背景方框 int x = col * colWidth + xOffset; int y = row * rowHeight +yOffset; painter.drawRoundRect(x,y,65,65,10,10); QWidget:paintEvent(event);2 Label類構造:MyLabel:MyLabel(int text) this->setText(QString:number(text); this->setAlignment(Qt:Alignment(Qt:AlignCen
5、ter); this->setFont(QFont("Gadugi", 20, QFont:Bold); /初始化樣式 int index = log_2(text) - 1; /計算背景數(shù)組索引值 QString fontColor = "color:rgb(255,255,255);" if(index < 8) fontColor = "color:rgb(119,110,101);" QString bgColor = QString("QLabelbackground-color:%1;border-r
6、adius:5px;%2").arg(digitBkgindex).arg(fontColor); this->setStyleSheet(bgColor); /透明度 QGraphicsOpacityEffect *m_pGraphicsOpacityEffect = new QGraphicsOpacityEffect(this); m_pGraphicsOpacityEffect->setOpacity(1); this->setGraphicsEffect(m_pGraphicsOpacityEffect); /動畫讓label慢慢出現(xiàn) QPropertyA
7、nimation *animation = new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",this); animation->setEasingCurve(QEasingCurve:Linear); animation->setDuration(400); animation->setStartValue(0); animation->setEndValue(1); animation->start(QAbstractAnimation:KeepWhenStopped);vo
8、id MyLabel:reSetText(int text) this->setText(QString:number(text); int index = log_2(text) - 1; /計算背景數(shù)組索引值 QString fontColor = "color:rgb(255,255,255);" if(index < 8) fontColor = "color:rgb(119,110,101);" QString bgColor = QString("QLabelbackground-color:%1;border-radi
9、us:5px;%2").arg(digitBkgindex).arg(fontColor); this->setStyleSheet(bgColor); this->show(); this->repaint();這里,ui就不貼出了,見源代碼。3 游戲主邏輯的設計與實現(xiàn)/初始化void GameWidget:initGame() /初始化分數(shù)為0 m_score = 0; m_highScore = 0; /初始化 for(int row = 0;row < 4;+row) for(int col = 0;col <4;+col) labelsrowc
10、ol = NULL; /讀取文件,最高分設置 /ReadOnly文件不存在,打開失敗 /WriteOnly文件不存在,會自動創(chuàng)建文件 /ReadWrite文件不存在,會自動創(chuàng)建文件 /Append文件不存在,會自動創(chuàng)建文件 /Truncate文件不存在,打開失敗 /Text文件不存在,打開失敗 /Unbuffered文件不存在,打開失敗 QFile *file = new QFile("data.json"); if(file->open(QIODevice:ReadOnly) QByteArray ba = file->readAll(); QJsonDocu
11、ment d = QJsonDocument:fromJson(ba); QJsonObject json = d.object(); QJsonValue value = json.value(QString("m_highScore"); QJsonValue score = json.value(QString("m_score"); /最高分數(shù) m_highScore = value.toVariant().toInt(); this->ui->best_2->setText(QString:number(m_highScore
12、); /當前分數(shù) m_score = score.toVariant().toInt(); this->ui->score_3->setText(QString:number(m_score); /文件保存的進度 QJsonValue labelsArr = json.value(QString("labels"); QJsonArray arr = labelsArr.toArray(); for(int i = 0;i< arr.size(); i+) QJsonValue labelValue = arr.at(i); QJsonArray a
13、rr1 = labelValue.toArray(); for(int j = 0; j< arr1.size(); j+) QJsonValue arrValue = arr1.at(j); int oldValue = arrValue.toVariant().toInt(); if(oldValue != 0) MyLabel *label=new MyLabel(oldValue); int x = j * colWidth + xOffset; int y = i * rowHeight +yOffset; label->setGeometry(x,y,66,66); l
14、abel->setParent(m_bgWidget); labelsij = label; labelsij->show(); +m_labelCount; file->close(); / 判斷讀取的文件是否游戲結束 / 這里可以不用判斷,為了防止游戲在結束的時候程序意外關閉 / gameOver(); else /初始化兩個標簽 for(int i=0;i<2;i+) createLabel(); 對游戲期間數(shù)字相碰的邏輯處理:bool GameWidget:merge(MyLabel *temp, int row, int col) if(temp != NUL
15、L) /判斷兩個值是否相等,是合并 if(temp->text() = labelsrowcol->text() int x = labelsrowcol->x(); int y = row * rowHeight + yOffset;/y軸偏移 /動畫效果 QPropertyAnimation *animation = new QPropertyAnimation(temp,"geometry"); animation->setStartValue(temp->geometry(); animation->setEndValue(QRe
16、ct(x,y,temp->width(),temp->height(); animation->setDuration(100); animation->setEasingCurve(QEasingCurve:Linear); animation->start(QAbstractAnimation:DeleteWhenStopped); int score = 2*labelsrowcol->text().toInt(); labelsrowcol->reSetText(score); m_score += score; emit ScoreChang
17、e(); -m_labelCount; return true; return false;bool GameWidget:isMerge() for(int row = 0;row < 4;+row) for(int col = 0;col <4;+col) if(isMergeDown(row,col) | isMergeRight(row,col) return true; return false;游戲勝利或者失敗的判斷:bool GameWidget:gameOver() bool flag = false; /如果格子全滿(m_labelCount = 16) if(m
18、_labelCount = 16) bool isWin = isMerge(); if(!isWin) /沒有可以合并的標簽,顯示失敗 QMessageBox:about(this, "信息", "失敗!"); flag = true; else /最大數(shù)出現(xiàn)2048,則顯示勝利 for(int row = 0;row < 4;+row) for(int col = 0;col <4;+col) if(labelsrowcol != NULL && labelsrowcol->text() = "2048&q
19、uot;) QMessageBox:about(this, "信息", "恭喜,你贏了!"); flag = true; break; if(flag) break; if(flag) /刪除數(shù)組,從頭開始 releaseRes(); m_labelCount = 0; m_score = 0; emit ScoreChange(); for(int i=0;i<2;i+) createLabel(); return flag;保存數(shù)據(jù):void GameWidget:saveGame() QFile *file = new QFile("
20、;data.json"); if(file->open(QIODevice:WriteOnly) QJsonDocument d; QJsonObject json = d.object(); /進度 QJsonArray arr; for(int i = 0;i< 4; i+) QJsonArray arr1; for(int j = 0; j< 4; j+) if(labelsij != NULL) arr1.append(labelsij->text(); else arr1.append(0); arr.append(arr1); json.inser
21、t("labels",arr); /分數(shù) json.insert("m_highScore",m_highScore); json.insert("m_score",m_score); d.setObject(json); QByteArray ba = d.toJson(QJsonDocument:Indented); /將數(shù)據(jù)寫入文件 file->write(ba); file->close(); void GameWidget:releaseRes() for(int col = 0;col <4;+col)
22、for(int row = 0;row < 4;+row) if(labelsrowcol != NULL) delete labelsrowcol; labelsrowcol = NULL; void GameWidget:closeEvent(QCloseEvent *event) saveGame();void GameWidget:setScore() this->ui->score_3->setText(QString:number(m_score); if(m_score > m_highScore) m_highScore = m_score; th
23、is->ui->best_2->setText(QString:number(m_highScore); void GameWidget:keyPressEvent(QKeyEvent *event) switch (event->key() case Qt:Key_Left: emit GestureMove(GestureDirect:LEFT); break; case Qt:Key_Right: emit GestureMove(GestureDirect:RIGHT); break; case Qt:Key_Down: emit GestureMove(Ges
24、tureDirect:DOWN); break; case Qt:Key_Up: emit GestureMove(GestureDirect:UP); break; default: break; QWidget:keyPressEvent(event);void GameWidget:moveLabel(GestureDirect direction) bool isMove = false; switch (direction) case LEFT: isMove = moveLeft(); break; case RIGHT: isMove = moveRight(); break;
25、case UP: isMove = moveUp(); break; case DOWN: isMove = moveDown(); break; default: break; /游戲勝利結束 bool isOver = gameOver(); /能移動才創(chuàng)建,不能移動并且游戲結束不創(chuàng)建新的標簽 if(isMove && !isOver) createLabel(); /游戲失敗結束 if(!isOver) gameOver(); 游戲重置:void GameWidget:on_bt_restart_clicked() /刪除數(shù)組,從頭開始 releaseRes(); m_l
26、abelCount = 0; m_score = 0; emit ScoreChange(); for(int i=0;i<2;i+) createLabel(); this->m_bgWidget->setFocus();上下左右移動時的設計:bool GameWidget:moveUp() bool isMove = false; /是否能移動 int i , j ; /i:記錄行 /j:記錄當前元素是否合并過的行 for(int col = 0;col < 4;+col) j = -1; for(int row = 0;row < 4;+row) /找到的第
27、一個不為null的label if(labelsrowcol = NULL) i = row + 1; while(i < 4) MyLabel *temp = labelsicol; if(temp != NULL) isMove = true; bool flag = false; if(j != (row-1) flag= isMergeUp(i,col); if(flag) -row; j = row; -m_labelCount; else /交換兩個元素 labelsrowcol = temp; j = -1; /將j重置為 -1; int x = temp->x();
28、 int y = row * rowHeight + yOffset;/y軸偏移 /動畫讓label往上升 QPropertyAnimation *animation = new QPropertyAnimation(temp,"geometry"); /開始值和結束值 animation->setStartValue(temp->geometry(); animation->setEndValue(QRect(x,y,temp->width(),temp->height(); animation->setDuration(100);/動
29、畫播放時長 /設置動畫的運動曲線 animation->setEasingCurve(QEasingCurve:Linear); animation->start(QAbstractAnimation:DeleteWhenStopped); /改變標簽值,并改變分數(shù) if(flag) int score = 2*labelsrowcol->text().toInt(); labelsrowcol->reSetText(score); m_score += score; emit ScoreChange(); delete temp; temp = NULL; label
30、sicol = NULL; +row; +i; else if(row + 1 < 4) /當標簽第一個不為空的時候 /如果該位置上有值,并且相鄰的位置依然有值 MyLabel *temp = labelsrow + 1col; bool flag = merge(temp,row,col); if(flag) isMove = true; j = row; delete labelsrow + 1col; labelsrow + 1col = NULL; return isMove;bool GameWidget:moveDown() bool isMove = false; /是否能
31、移動,不能移動 int i , j ; /i:記錄行 /j:記錄當前元素是否合并過的行 for(int col = 0;col <4;+col) j = -1; for(int row = 3;row > -1;-row) /找到的第一個不為null的label if(labelsrowcol = NULL) i =row-1; while(i > -1) MyLabel *temp = labelsicol; if(temp != NULL) isMove = true; bool flag = false; if(j != (row + 1) flag= isMergeD
32、own(i,col); if(flag) +row; j = row; -m_labelCount; else /交換兩個元素 labelsrowcol = temp; j = -1; int x= temp->x(); int y = row * rowHeight + yOffset; /動畫讓label往下降 QPropertyAnimation *animation = new QPropertyAnimation(temp,"geometry"); /開始值和結束值 animation->setStartValue(temp->geometry(
33、); animation->setEndValue(QRect(x,y,temp->width(),temp->height(); animation->setDuration(100);/動畫播放時長為 /設置動畫的運動曲線 animation->setEasingCurve(QEasingCurve:Linear); animation->start(QAbstractAnimation:DeleteWhenStopped); /改變標簽值,并改變分數(shù) if(flag) int score = 2*labelsrowcol->text().toIn
34、t(); labelsrowcol->reSetText(score); m_score += score; emit ScoreChange(); delete temp; temp = NULL; labelsicol = NULL; -row; -i; else if(row-1>=0) MyLabel *temp = labelsrow-1col; bool flag = merge(temp,row,col); if(flag) isMove = true; j = row; delete labelsrow-1col; labelsrow-1col = NULL; re
35、turn isMove;bool GameWidget:moveLeft() bool isMove = false; /是否能移動 int i , j ; /i:記錄行 /j:記錄當前元素是否合并過的行 for(int row = 0;row < 4;+row) j = -1; for(int col = 0;col < 4;+col) /找到的第一個不為null的label if(labelsrowcol = NULL) i = col + 1; while(i < 4) MyLabel *temp = labelsrowi; if(temp != NULL) isMove = true; bool flag = false; if(j != (col-1) flag= isMergeLeft(row,i); if(flag) -col
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學數(shù)學骨干教師培訓方案設計
- 小學美術課程教學計劃及活動方案
- 招聘面試技巧培訓及評價標準
- 小學音樂課堂質量測評試卷樣本
- 基層物業(yè)投訴處理及應對技巧
- 施工方案怎么自學(3篇)
- 水泥應急預案演練(3篇)
- 兒童模特活動策劃方案(3篇)
- 審查施工方案規(guī)范(3篇)
- 火災應急預案課件(3篇)
- 心電圖室工作總結
- 明細賬(三欄式、多欄式)電子表格
- 急性心肌梗死后心律失常護理課件
- 產品供貨方案、售后服務方案
- 十八而志夢想以行+活動設計 高三下學期成人禮主題班會
- 2023年上海華東理工大學機械與動力工程學院教師崗位招聘筆試試題及答案
- 醫(yī)院18類常用急救藥品規(guī)格清單
- 放棄公開遴選公務員面試資格聲明
- 2023-2024學年江蘇省海門市小學語文五年級期末點睛提升提分卷
- 北京城市旅游故宮紅色中國風PPT模板
- DB42T1319-2021綠色建筑設計與工程驗收標準
評論
0/150
提交評論