成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS實(shí)戰(zhàn) | 貪吃蛇游戲 | JS全注釋

開發(fā) 前端 OpenHarmony
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com

[[405278]]

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

本人之前是Java后端開發(fā),對于前端開發(fā)一知半解。但是對于官方資料中的這個(gè)貪吃蛇項(xiàng)目十分感興趣。打算做一遍流程。雖然資料中也含有開發(fā)流程,但是不全,而且沒有注釋。所以決定自己寫完之后,把每個(gè)步驟總結(jié)出來并寫上注釋。屬于二次創(chuàng)作吧!

開發(fā)完成的程序界面如下所示。抄一遍代碼運(yùn)行,也不會(huì)超過2h。適合剛學(xué)習(xí)鴻蒙的開發(fā)者~如果你喜歡,不妨收藏一下(≧∇≦)ノ

正文

一、創(chuàng)建項(xiàng)目

1. 選擇JS模板

2. 定義自己的項(xiàng)目名和包名,然后點(diǎn)擊finsh

3. 創(chuàng)建完畢,生成的目錄結(jié)構(gòu)如下:

由于項(xiàng)目比較簡單,所以就只需要在紅框中的位置編寫代碼。這是由項(xiàng)目默認(rèn)生成的噢!

二、編寫代碼

1. 導(dǎo)入圖片資源

在我提供的壓縮資料里找到圖片文件夾,他它們直接copy到項(xiàng)目目錄中。

2. 編寫html頁面

先把標(biāo)簽、內(nèi)容和資源定義好,編寫完后運(yùn)行查看結(jié)果:

  1. <!--容器--> 
  2. <div class="container"
  3.  
  4.     <!--標(biāo)題--> 
  5.     <text class="title">Snake Game</text> 
  6.  
  7.     <!--畫布組件:貪吃蛇的移動(dòng)區(qū)域--> 
  8.     <canvas style="width: 600px; height: 600px; background-color: black;"></canvas> 
  9.  
  10.     <!--上按鍵--> 
  11.     <image src="/common/up.png"></image> 
  12.     <!--左按鍵--> 
  13.     <image src="/common/left.png"></image> 
  14.     <!--下按鍵--> 
  15.     <image src="/common/down.png"></image> 
  16.     <!--右按鍵--> 
  17.     <image src="/common/right.png"></image> 
  18.      
  19.     <!--顯示得分--> 
  20.     <text> 
  21.         <span>Score: </span> 
  22.     </text> 
  23.  
  24. </div> 

運(yùn)行后發(fā)現(xiàn)樣式布局混亂,不過沒關(guān)系,運(yùn)行主要是確保樣式和資源有沒有加載。都加載好之后,再調(diào)整樣式。

(測試的話,要去最上方導(dǎo)航欄,點(diǎn)擊 Tools>HVD Manager>登陸開發(fā)者賬號>選擇P40后面的那個(gè)三角形)。

3. 編寫css代碼

調(diào)整樣式,在需要調(diào)整的樣式后面加對應(yīng)的類名class="",通過這些類名調(diào)用css文件的數(shù)據(jù)。

  1. <!--上按鍵--> 
  2. <image class="backBtnup" src="/common/up.png"></image> 
  3. <!--左按鍵--> 
  4. <image class="backBtnleft" src="/common/left.png"></image> 
  5. <!--下按鍵--> 
  6. <image class="backBtncenter" src="/common/down.png"></image> 
  7. <!--右按鍵--> 
  8. <image class="backBtnright" src="/common/right.png"></image> 
  9.  
  10. <!--顯示得分--> 
  11. <text class="scoretitle"
  12.     <span>Score: </span> 
  13. </text> 

 確認(rèn)好類名之后,就在index.css文件中根據(jù)類名寫css。

  1. .container { 
  2.     flex-direction: column
  3.     justify-content: center; 
  4.     align-items: center; 
  5.     background-color: white; 
  6.  
  7. .title { 
  8.     font-size: 100px; 
  9.     margin-bottom: 130px; 
  10.  
  11. .scoretitle { 
  12.     font-size: 50px; 
  13.     margin-top: 30px; 
  14.  
  15. /* 
  16.     css選擇器,逗號代表并列關(guān)系 
  17.     具體可以百度噢,因?yàn)榍岸瞬┐缶?nbsp;
  18. */ 
  19. .backBtnup, .backBtncenter, .backBtnleft, .backBtnright { 
  20.     width: 100px; 
  21.     height: 100px; 
  22.     margin-bottom: 20px; 
  23.     margin-top: 20px; 
  24.     border-radius: 10px; 
  25.     background-color: black; 
  26.  
  27. .backBtnup { 
  28.     margin-top: 80px; 
  29.  
  30. .backBtncenter { 
  31.     margin-left: 40px; 
  32.     margin-right: 40px; 

 寫好上述內(nèi)容后,再次運(yùn)行一下。發(fā)現(xiàn)有點(diǎn)樣子了,只需處理一下按鈕即可。

4. 優(yōu)化按鈕

想要的效果是方向鍵如同鍵盤方向的布局,所以只需對下面三個(gè)按鍵進(jìn)行處理。可以用一個(gè)div標(biāo)簽把它們包裹起來,再定義一個(gè)新屬性。

  1. <!--上按鍵--> 
  2. <image class="backBtnup" src="/common/up.png"></image> 
  3. <!--下面三個(gè)按鍵用同一樣式,所以用同一個(gè)div包圍--> 
  4. <div class="directsecond"
  5.     <!--左按鍵--> 
  6.     <image src="/common/left.png" class="backBtnleft"></image> 
  7.     <!--下按鍵--> 
  8.     <image src="/common/down.png" class="backBtncenter"></image> 
  9.     <!--右按鍵--> 
  10.     <image src="/common/right.png" class="backBtnright"></image> 
  11. </div> 

 css部分的新添加的代碼:

  1. .directsecond { 
  2.     flex-direction: row; 
  3.     justify-content: center; 
  4.     align-items: center; 

  運(yùn)行測試一下,發(fā)現(xiàn)達(dá)到了我們想要的效果。

三、編寫JS代碼

我們發(fā)現(xiàn)現(xiàn)在只有樣式,光點(diǎn)擊按鈕沒有反饋,而且也沒有小蛇和食物……

所以我們接下來編寫JS代碼就是要解決這些事情,但是切忌邊寫邊想。應(yīng)該先設(shè)計(jì)再寫代碼!

1. 設(shè)計(jì)思想

  • 按鈕的觸發(fā)是通過點(diǎn)擊屏幕,所以要有點(diǎn)擊事件

鼠標(biāo)點(diǎn)擊事件是有對應(yīng)的方法

通過方法傳不同的參數(shù)來區(qū)別不同的方向

  • 食物的生成

隨機(jī)生成

判斷食物生成的位置如果出現(xiàn)在蛇身上,則重新生成

  • 蛇身的初始化 (由于案例比較簡單,所以沒有設(shè)定隨機(jī)生成初始位置)

給定長度并設(shè)定一個(gè)空數(shù)組

通過for循環(huán),把x和y的坐標(biāo)push進(jìn)數(shù)組,作為蛇身每格的位置

  • 蛇運(yùn)動(dòng)

移動(dòng)是靠每幀重繪位置

吃到水果就頭部立刻加長

沒吃到水果就去掉尾部,把頭部方向指向的下一個(gè)位置記錄到數(shù)組頭部,等下次刷新幀

  • 判定游戲結(jié)束

碰壁

相對方向移動(dòng)

形成環(huán)路

2. 方法調(diào)用流程圖

虛線代表 if 判斷,如果為符合判斷條件才會(huì)調(diào)用該方法。

3. 編寫代碼

在index.html文件中綁定對應(yīng)的事件(這也是html文件的全部內(nèi)容)

  1. <!--容器--> 
  2. <div class="container"
  3.  
  4.     <!--標(biāo)題--> 
  5.     <text class="title">Snake Game</text> 
  6.  
  7.     <!--畫布組件:貪吃蛇的移動(dòng)區(qū)域--> 
  8.     <canvas ref="canvasref" style="width: 600px; height: 600px; background-color: black;"></canvas> 
  9.  
  10.     <!--上按鍵--> 
  11.     <image src="/common/up.png" class="backBtnup" onclick="onStartGame(1)"></image> 
  12.  
  13.     <!--下面三個(gè)按鍵用同一樣式,所以用同一個(gè)div包圍--> 
  14.     <div class="directsecond"
  15.         <!--左按鍵--> 
  16.         <image src="/common/left.png" class="backBtnleft" onclick="onStartGame(2)"></image> 
  17.         <!--下按鍵--> 
  18.         <image src="/common/down.png" class="backBtncenter" onclick="onStartGame(3)"></image> 
  19.         <!--右按鍵--> 
  20.         <image src="/common/right.png" class="backBtnright" onclick="onStartGame(4)"></image> 
  21.     </div> 
  22.  
  23.     <!--用if判斷,如果游戲結(jié)束,則顯示該模塊--> 
  24.     <text if="{{gameOver}}" class="scoretitle"
  25.         <span>Game Over!!!</span> 
  26.     </text> 
  27.     <!--用if判斷,如果游戲沒有結(jié)束,則顯示該模塊。顯示得分--> 
  28.     <text if="{{!gameOver}}" class="scoretitle"
  29.         <span>Score: {{score}}</span> 
  30.     </text> 
  31. </div> 

index.js文件的全部內(nèi)容

  1. export default { 
  2.     data: { 
  3.         title: ""
  4.         snakeSize: 30,      // 蛇身格子像素大小 
  5.         w: 600,             // 背景的寬度 
  6.         h: 600,             // 背景的高度 
  7.         score: 0,           // 得分為0 
  8.         snake : [],         // 數(shù)組用來存蛇每個(gè)格子的位置 
  9.         ctx: null,          // 用來調(diào)用填充顏色的 
  10.         food: null,         // 食物位置 
  11.         direction: '',      // 按鍵的狀態(tài) 
  12.         gameOver: false,    // 游戲狀態(tài) 
  13.         tail: {             // 記錄更新后蛇頭的位置 
  14.             x: 0, 
  15.             y: 0 
  16.         }, 
  17.         interval : null     // 獲得setInterval()的返回值 
  18.     }, 
  19.     onInit() { 
  20.         this.title = this.$t('strings.world'); 
  21.     }, 
  22.     onShow() { 
  23.         // 通過$refs得到組件,進(jìn)而調(diào)用組件的變量和方法 
  24.         const canvas = this.$refs.canvasref; 
  25.         // 指定了二維繪畫 
  26.         this.ctx = canvas.getContext("2d"); 
  27.         // 第一次打開app時(shí),初始化蛇的方向 
  28.         this.direction = 'down'
  29.         // 調(diào)用初始化蛇體的方法 
  30.         this.drawSnake() 
  31.         // 創(chuàng)建食物的位置 
  32.         this.createFood() 
  33.         // 渲染幀畫面 
  34.         this.paint() 
  35.     }, 
  36.     // 畫背景 
  37.     drawArea() { 
  38.         var ctx = this.ctx 
  39.         // 設(shè)置填充顏色的 
  40.         ctx.fillStyle = '#61c7e6'
  41.         // 填充 
  42.         ctx.fillRect(0, 0, this.w, this.h); 
  43.         // 設(shè)置矩陣顏色的 
  44.         ctx.strokeStyle = '#00000'
  45.         // 矩陣的線寬 
  46.         ctx.lineWidth = 5; 
  47.         // 繪制矩陣(不填色的) 
  48.         ctx.strokeRect(0, 0, this.w, this.h); 
  49.         this.ctx = ctx 
  50.     }, 
  51.     // 創(chuàng)建蛇體 
  52.     drawSnake() { 
  53.         var len = 7; 
  54.         var snake = []; 
  55.         // 默認(rèn)蛇的長度為7 
  56.         for (var i = len - 1; i >= 0; i--) { 
  57.             // 將x軸和y軸的坐標(biāo)數(shù)據(jù)存到數(shù)組中,這些數(shù)據(jù)就是每個(gè)蛇格子的位置 
  58.             snake.push({ 
  59.                 x: 0, 
  60.                 y: i 
  61.             }); 
  62.         } 
  63.         // 更新蛇的長度 
  64.         this.snake = snake; 
  65.     }, 
  66.     // 設(shè)計(jì)蛇身的顏色的 
  67.     bodySnake(x, y) { 
  68.         //single square of snake 
  69.         var ctx = this.ctx; 
  70.         // 蛇的顏色及填充的位置和大小 
  71.         ctx.fillStyle = '#e28743'
  72.         // fillRect()指的是要填充的位置及大小 參數(shù)說明:fillRect(X軸位置, Y軸位置, 寬度, 高度) 
  73.         ctx.fillRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize); 
  74.         // 蛇的內(nèi)部格子邊框顏色,加了才會(huì)分割 
  75.         ctx.strokeStyle = '#063970'
  76.         ctx.strokeRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize); 
  77.         this.ctx = ctx; 
  78.     }, 
  79.     // 設(shè)計(jì)食物的顏色的 
  80.     cookie(x, y) { 
  81.         var ctx = this.ctx; 
  82.         // 食物的顏色及填充位置和大小 
  83.         ctx.fillStyle = '#e2d743'
  84.         ctx.fillRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize); 
  85.         this.ctx = ctx; 
  86.     }, 
  87.     // 創(chuàng)建食物的位置 
  88.     createFood() { 
  89.         // 隨機(jī)生成食物的位置 
  90.         // 這里的20是背景高度(寬度)/ 格子高度(寬度),即 600 / 30 = 20 
  91.         this.food = { 
  92.             x: Math.floor((Math.random() * 20) + 1), 
  93.             y: Math.floor((Math.random() * 20) + 1) 
  94.         } 
  95.         for (var i = 0; i > this.snake.length; i++) { 
  96.             // 獲取剛創(chuàng)建蛇的時(shí)候,蛇上每個(gè)點(diǎn)的位置,再和食物的位置進(jìn)行比較 
  97.             var snakeX = this.snake[i].x; 
  98.             var snakeY = this.snake[i].y; 
  99.             // 如果食物的位置出現(xiàn)在蛇的身上,則重新生成 
  100.             if (this.food.x === snakeX && this.food.y === snakeY || this.food.y === snakeY && this.food.x === snakeX) { 
  101.                 this.food.x = Math.floor((Math.random() * 20) + 1); 
  102.                 this.food.y = Math.floor((Math.random() * 20) + 1); 
  103.             } 
  104.         } 
  105.     }, 
  106.     // 檢查是否碰壁 
  107.     checkCollision(x, y, array) { 
  108.         for(var i = 0; i < array.length; i++) { 
  109.             if(array[i].x === x && array[i].y === y) 
  110.             return true
  111.         } 
  112.         return false
  113.     }, 
  114.     // 鼠標(biāo)點(diǎn)擊綁定的事件 
  115.     onStartGame(direct){ 
  116.         // 設(shè)置游戲初始狀態(tài),控制text標(biāo)簽的顯示 
  117.         this.gameOver = false 
  118.         // 通過對應(yīng)的參數(shù),獲取對應(yīng)direct的字段 
  119.         if (direct == 1) { 
  120.             this.direction = 'up' 
  121.         } else if (direct == 2) { 
  122.             this.direction = 'left' 
  123.         } else if (direct == 3) { 
  124.             this.direction = 'down' 
  125.         } else if (direct == 4) { 
  126.             this.direction = 'right' 
  127.         } 
  128.         // 調(diào)用繪圖方法 
  129.         this.paint() 
  130.         // 設(shè)置蛇的移動(dòng)間隔時(shí)間,也可以理解為繪圖的時(shí)間間隔 
  131.         if (this.interval == null) { 
  132.             // setInterval() 方法可按照指定的周期(以毫秒計(jì))來調(diào)用函數(shù)或計(jì)算表達(dá)式 
  133.             this.interval = setInterval(this.paint, 250); 
  134.         } 
  135.     }, 
  136.     // 每次移動(dòng)刷新的操作,即幀畫面創(chuàng)建和渲染的流程 
  137.     paint() { 
  138.         // 調(diào)用畫背景 
  139.         this.drawArea() 
  140.         // 獲得蛇頭的位置的初始坐標(biāo) 
  141.         var snakeX = this.snake[0].x; 
  142.         var snakeY = this.snake[0].y; 
  143.         // 移動(dòng)操作,更新數(shù)據(jù) 
  144.         if (this.direction == 'right') { 
  145.             snakeX++; 
  146.         } 
  147.         else if (this.direction == 'left') { 
  148.             snakeX--; 
  149.         } 
  150.         else if (this.direction == 'up') { 
  151.             snakeY--; 
  152.         } else if (this.direction == 'down') { 
  153.             snakeY++; 
  154.         } 
  155.         // 反向移動(dòng)或碰撞壁的時(shí)候,游戲失敗,重啟游戲 
  156.         if (snakeX == -1 || snakeX == this.w / this.snakeSize || snakeY == -1 || snakeY == this.h / this.snakeSize || this.checkCollision(snakeX, snakeY, this.snake)) { 
  157.             //ctx.clearRect(0,0,this.w,this.h); //clean up the canvas 
  158.             clearInterval(this.interval); 
  159.             this.interval = null 
  160.             this.restart() 
  161.             return
  162.         } 
  163.         //  判斷是否吃到食物 
  164.         if(snakeX == this.food.x && snakeY == this.food.y) { 
  165.             // 吃到食物 
  166.             // 將食物的位置記錄下來 
  167.             this.tail = {x: snakeX, y: snakeY}; 
  168.             // 分?jǐn)?shù)加5 
  169.             this.score = this.score+5; 
  170.             // 再創(chuàng)建食物 
  171.             this.createFood(); 
  172.         } else { 
  173.             // 沒吃到食物 
  174.             // 去掉數(shù)組最后的元素并返回,相當(dāng)于刪除蛇尾 
  175.             this.tail = this.snake.pop(); 
  176.             // 將移動(dòng)更新后蛇頭的位置加到tail中 
  177.             this.tail.x = snakeX; 
  178.             this.tail.y = snakeY; 
  179.         } 
  180.         // unshift()方法可向數(shù)組的開頭添加一個(gè)或多個(gè)元素 
  181.         // 將更新后的節(jié)點(diǎn)添加蛇頭 
  182.         this.snake.unshift(this.tail); 
  183.         // 渲染每個(gè)蛇身格子的位置 
  184.         for(var i = 0; i < this.snake.length; i++) { 
  185.             this.bodySnake(this.snake[i].x, this.snake[i].y); 
  186.         } 
  187.         // 渲染食物的位置 
  188.         this.cookie(this.food.x, this.food.y); 
  189.     }, 
  190.     // 重啟操作 
  191.     restart() { 
  192.         this.drawArea() 
  193.         this.drawSnake() 
  194.         this.createFood() 
  195.         this.gameOver = true 
  196.         this.score = 0 
  197.     }, 

 運(yùn)行測試ok。

總結(jié)

寫貼方式有點(diǎn)虎頭蛇尾,在比較重要的JS代碼部分沒有細(xì)致說清步驟。不過這也是沒辦法的,因?yàn)檫@里面太多嵌套調(diào)用了,只有文字無法說清,唯有視頻講解才能把邏輯理清。所以考慮到這樣的缺點(diǎn),我也做出了程序調(diào)用流程圖來方便大家理解。就好像很難用語言去描述遞歸的調(diào)用流程一樣,因?yàn)檫@是套娃……

在我看來這個(gè)小項(xiàng)目還是有比較多改進(jìn)的地方

比如:

  • 蛇的位置隨機(jī)生成;
  • 優(yōu)化按下相對按鍵的操作:即蛇向右走的時(shí)候,按左方向是不會(huì)影響蛇的方向,就不會(huì)被程序判定失敗了;
  • 定制專門的重啟游戲按鍵
  • 等等

引用

4-1.生態(tài)案例:【開發(fā)者說】重塑經(jīng)典,如何在HarmonyOS手機(jī)上還原貪吃蛇游戲.pdf

HarmonyOS開發(fā)者 / 重塑經(jīng)典,如何在HarmonyOS手機(jī)上還原貪吃蛇游戲

PS:友情提示

# 圖片模糊怎么辦?

對圖片右鍵在新窗口打開

對圖片右鍵另存為本地看

# 下面有兩個(gè)文件

【鴻蒙——貪吃蛇項(xiàng)目源碼.rar】是項(xiàng)目源碼

【4-1.生態(tài)案例:【開發(fā)者說】重塑經(jīng)典,如何.pdf】是官方大禮包的原文件

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載

原文鏈接:https://harmonyos.51cto.com/posts/4776

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2012-06-05 14:42:57

Silverlight

2024-12-17 17:46:05

Android原生控件貪吃蛇

2022-07-25 14:17:04

JS應(yīng)用開發(fā)

2015-07-31 11:26:24

Swift貪吃蛇

2020-08-20 20:30:49

C語言小游戲貪吃蛇

2021-09-02 15:25:53

鴻蒙HarmonyOS應(yīng)用

2022-11-07 11:27:00

JS游戲開發(fā)

2024-01-18 11:22:41

C++Windows開發(fā)

2022-10-28 09:33:10

Linux貪吃蛇

2021-04-20 11:40:12

Linux圖形庫curses

2023-10-17 10:20:53

VueReact

2025-02-27 09:31:05

2016-09-19 21:24:08

PythonAsyncio游戲

2016-09-22 21:12:14

2016-09-14 21:17:47

PythonAsyncio游戲

2010-02-05 15:00:44

Android 調(diào)用u

2018-08-31 15:48:33

2015-07-07 15:47:57

Razer雷蛇

2025-02-18 09:00:00

2021-05-27 16:53:09

開發(fā)技能代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 国产在线一区二区三区 | 日韩一区二区三区在线观看 | 国产在线一区二区三区 | 久久久www成人免费无遮挡大片 | 日韩一区二区在线播放 | 久久久久免费精品国产 | 国产精品一区二区三 | 午夜影院在线观看免费 | 天天爱综合 | 久久99精品久久久久久青青日本 | 成人免费视频观看视频 | 久久久久精 | 亚洲视频在线观看免费 | 亚洲一区二区三区免费在线观看 | 久久99精品久久久久子伦 | 黄色片免费在线观看 | 亚洲欧美日韩一区二区 | 国产乱码精品一品二品 | 日韩综合一区 | 少妇精品久久久久久久久久 | 日韩高清不卡 | 一区二区三区电影在线观看 | 日韩中文字幕 | 午夜视频在线观看一区二区 | 国产精品网址 | 美女久久| 国产高清美女一级a毛片久久w | 欧美性tv | 国产精品一区久久久 | 国产精品亚洲成在人线 | 久草.com| 日韩一区二区在线观看 | 天天看夜夜 | 亚洲夜射 | 欧美又大粗又爽又黄大片视频 | 伊人狠狠操 | av中文字幕在线播放 | 国产精品欧美一区二区 | 99国内精品久久久久久久 | 成人高潮片免费视频欧美 | 久久人体 |