Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 欧美在线伦理,国内精品在线观看视频,国产精品一区在线麻豆

          整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          CSS小游戲2048-簡單小游戲編程有手就行~會玩你

          CSS小游戲2048-簡單小游戲編程有手就行~會玩你會寫嗎?

          單實現2048小游戲

          想實現2048游戲書寫代碼時可以分為三個步驟

          一、HTML部分

          先書寫HTML把游戲結構搭建出來

          <!DOCTYPE html>
          <html lang="en">
          <head>
              <meta charset="UTF-8">
              <title>Title</title>
              <link rel="stylesheet" href="index.css">
          </head>
          <body>
          <!--最外部的大框-->
          <div class="outermost"> //包裹游戲全局的大盒子
              <!--title-->
              <span class="top"><b>SCORE:<span id="score01"></span></b></span>//頂部實時顯示的游戲分數
              <!--游戲大框框-->
              <div class="big">//2048游戲為四行四列因此需要16個div盒子
                  <div class="cell" id="c00"></div>
                  <div class="cell" id="c01"></div>
                  <div class="cell" id="c02"></div>
                  <div class="cell" id="c03"></div>
           
                  <div class="cell" id="c10"></div>
                  <div class="cell" id="c11"></div>
                  <div class="cell" id="c12"></div>
                  <div class="cell" id="c13"></div>
           
                  <div class="cell" id="c20"></div>
                  <div class="cell" id="c21"></div>
                  <div class="cell" id="c22"></div>
                  <div class="cell" id="c23"></div>
           
                  <div class="cell" id="c30"></div>
                  <div class="cell" id="c31"></div>
                  <div class="cell" id="c32"></div>
                  <div class="cell" id="c33"></div>
              //游戲結束時會彈出的提示框
              </div>
              <!--提示框-->
              <div class="tips" id="gameover">
                 <p>GAME OVER!!! <br>
                     SCORE: <span id="score02">0</span><br>
                     <button class="startbtn">重新開始</button>
                 </p>
              </div>
              <!--重玩一遍-->
              <div class="foot">
                  <button class="replay"><a>重玩一遍</a></button>
              </div>
          </div>
          <script type="text/javascript" src="index.js"></script>
          </body>
          </html>

          二、css部分

          經過了第一步的搭建游戲框架,第二部就是給游戲添加樣式,使它能顯示出來

          *{
              padding: 0px;
              margin: 0px auto;
              font-family: Arial;
           
          }
          /*最外部的大框*/
          .outermost{
              width: 480px;
              height: 600px;
              font-size: 40px;
              margin-top: 120px;
          }
          /*title*/
          <!--頂部顯示分數的樣式-->
          .top{
              margin: auto;
          }
          .top span{
              color: red;
          }
          /*游戲大框框*/
          .big{
              width: 480px;
              height: 480px;
              background:pink;
              border-radius: 8px;
          }
          <!--給每一個盒子包裹的小框子添加樣式-->
          .cell{
              list-style: none;
              float: left;
              display: inline-block;
              width: 100px;
              height: 100px;
              line-height: 100px;
              text-align: center;
              background-color: #fbf8cd;
              margin-left: 16px;
              margin-top: 16px;
              border-radius: 6px;
          }
          <!--提前把出現的數2、4、8、16等的數所在的格子給添加好樣式增加游戲體驗感-->
          .n2{background-color:#f65e3b;color:#776e65}
          .n4{background-color:#33b5e5;color:#776e65}
          .n8{background-color:#f2b179;color:#776e65}
          .n16{background-color:#f59563;color:#776e65}
          .n32{background-color:#f67c5f;color:#776e65}
          .n64{background-color:#f65e3b;color:#776e65}
          .n128{background-color:#edcf72;color:#776e65}
          .n256{background-color:#edcc61;color:#776e65}
          .n512{background-color:#9c0;color:#776e65}
          .n1024{background-color:#33b5e5;color:#776e65;font-size:40px}
          .n2048{background-color:#09c;color:#776e65;font-size:40px}
          /*提示框樣式*/
          .tips{
              border: 1px solid #cccccc;
              background: #FFFFFF;
              width: 400px;
              height: 200px;
              border-radius: 10px;
              color: #ff4456;
              text-align: center;
              line-height: 60px;
              position: absolute;
              top: 50%;
              left: 50%;
              margin-left: -200px;
              margin-top: -100px;
              display: none;
          }
          .tips .startbtn{
              height: 50px;
              width: 200px;
              color: #FFFFFF;
              font-size: 20px;
              line-height: 50px;
              border-radius: 10px;
              background: cornflowerblue;
              border: none;
          }
          /*重玩一遍*/
          .foot{
              width: 200px;
              height: 50px;
          }
          .foot>.replay{
              width: 200px;
              height: 50px;
              background: aquamarine;
              margin-top: 60px;
              color: lightpink;
              border:0;
              font-size: 24px;
              font-weight: bold;
              border-radius: 6px;
          }

          書寫好了HTML+CSS部分游戲的模樣也就出來了,如下圖所示:

          三、JS部分

          下面就到了最后也是最關鍵的一步----添加行為,也就是JS部分的書寫,給其添加效果

          //創建一個對象,里面存儲所有的游戲數據及游戲方法
          var game={
          	data : [],   //定義一個數組,用來存所有的游戲的數據
          	score : 0,   //定義一個分數的屬性
          	gamerunning : 1,   //定義一個游戲運行的狀態,將其設置為1與其他狀態區分開
          	gameover : 0,     //定義一個游戲結束的狀態
          	status : 0,      //這個是目前游戲的狀態,時刻的跟上面兩個狀態做比較,確定游戲處于運行或者結束
          	start : function(){   //游戲開始時候的方法
          //		游戲開始的時候肯定是要把游戲的狀態設置成游戲運行的狀態
          //		this==game
          		this.status=this.gamerunning;
          //		游戲開始的時候分數清空
          		this.score=0;
          //		數組中的所有元素全部設置成0
          		this.data=[
          			[0,0,0,0],
          			[0,0,0,0],
          			[0,0,0,0],
          			[0,0,0,0]
          		];
          		this.randomNum();//調用下面自定義的隨機函數,可以在移動和開始的時候隨機出來一個數
          		this.randomNum();//調用兩次是因為這是游戲開始時的方法,開局隨機出現兩個數和位置,因此需要調用兩次
          		this.dataView();//調用下面所寫的更新視圖的方法
          	},
          //	隨機數的函數,開始的時候隨機生成,移動的時候隨機生成
          	randomNum: function(){
          		while(true){
          			//		隨機生成行和列 0 - 3隨機整數
          			var r=Math.floor( Math.random() * 4 );   //隨機生成一個行
          			var c=Math.floor( Math.random() * 4 );   //隨機生成一個列
          			
          			if(this.data[r][c]==0){
          				var num=Math.random() > 0.5 ? 2 : 4;//隨機出現2或4
          				this.data[r][c]=num;
          				break;
          			}
          		}
          	},
          //	更新試圖的方法
          	dataView: function(){
          //		大的循環,然后把所有的元素全部遍歷一遍
          		for(var r=0; r < 4; r++){
          			for(var c=0; c < 4; c++){
          //				找到對應的div
          				var div=document.getElementById("c" + r + c);  //字符串拼接
          				if(this.data[r][c] !=0){
          //					數組中對應的內容放到格子上面去
          					div.innerHTML=this.data[r][c];
          //					樣式也寫成對應的
          					div.className="cell n" + this.data[r][c];
          				}else{
          					div.innerHTML="";
          					div.className="cell"
          				}
          			}
          		}
          //		更新分數
          		document.getElementById("score01").innerHTML=this.score;
          		//游戲沒有結束的時候 彈出層時刻都是隱藏的
          		if(this.status==this.gamerunning){
          			document.getElementById("gameover").style.display="none";
          		}else{
          			document.getElementById("gameover").style.display="block";
          			document.getElementById("score02").innerHTML=this.score;
          		}
          	},
          //	判斷游戲是否結束的方法
          	isgameover: function(){
          		for(var r=0; r < 4; r++){
          			for(var c=0; c < 4; c++){
          				if(this.data[r][c]==0){  //里面有空格子的時候,游戲還是可以運行
          					return false;   //表示游戲還沒有結束
          				}
          				if(c < 3){//判斷左右是否有相同的
          					if(this.data[r][c]==this.data[r][c+1]){
          						return false;
          					}
          				}
          				if(r < 3){
          					if(this.data[r][c]==this.data[r+1][c]){
          						return false;
          					}
          				}
          			}
          		}
          		return true;
          	},

          //移動的方法,左 右 上 下四個部分

          //	左 右 上 下
          //	左移的方法
          	moveLeft: function(){
          		var before=String(this.data);   //之前做一次轉換
          //		具體的移動需要處理的邏輯,直接處理好每一行即可
          		for(var r=0;r < 4;r ++){
          			this.moveLeftInRow(r);
          		}
          		var after=String(this.data);  //移動之后再做一次轉換
          //		如果說移動之前不等于移動之后,肯定是發生了移動
          		if(before !=after){
          			this.randomNum();   //生成隨機數
          //			生成的隨機數可能會造成游戲的gameover
          			if(this.isgameover()){
          //				改變游戲的狀態
          				this.status=this.gameover
          			}
          //			更新視圖
          			this.dataView();
          		}
          	},
          	moveLeftInRow: function(r){   //只去做處理每一行的邏輯
          		for(var c=0; c < 3; c++){
          			var nextc=this.getNextinRow(r,c);
          			if(nextc !=-1){
          				if(this.data[r][c]==0){
          //					如果等于0,直接替換
          					this.data[r][c]=this.data[r][nextc];
          					this.data[r][nextc]=0;  //位置恢復成0
          					c --;   //要讓位置恢復到原地
          				}else if(this.data[r][c]==this.data[r][nextc]){
          					this.data[r][c] *=2;   //位置直接翻一倍
          					this.data[r][nextc]=0;
          					this.score +=this.data[r][c];  //更新分數
          				}
          			}else{   //沒有找到
          				break;   //直接退出循環
          			}
          		}
          	},
          	getNextinRow: function(r,c){
          		for(var i=c + 1; i < 4; i++){
          			if(this.data[r][i] !=0){
          				return i;    //表示已經找到位置,并且把位置返回出來
          			}
          		}
          		return -1;   //返回一個標識符
          	},
          //	右移的方法
          	moveRight: function(){
          		var before=String(this.data);
          		for(var r=0; r < 4; r++){
          			this.moveRightInRow(r);
          		}
          		var after=String(this.data);
          		if(before !=after){
          			this.randomNum();
          			if(this.isgameover()){
          				this.status=this.gameover;
          			}
          			this.dataView();
          		}
          	},
          	moveRightInRow: function(r){
          		for(var c=4; c > 0; c--){
          			var prevc=this.getPrevInRow(r,c);
          			if(prevc !=-1){
          				if(this.data[r][c]==0){
          					this.data[r][c]=this.data[r][prevc];
          					this.data[r][prevc]=0;
          					c ++
          				}else if(this.data[r][c]==this.data[r][prevc]){
          					this.data[r][c] *=2;
          					this.data[r][prevc]=0;
          					this.score +=this.data[r][c];
          				}
          			}else{
          				break;
          			}
          		}
          	},
          	getPrevInRow: function(r,c){
          		for(var i=c - 1; i >=0; i--){
          			if(this.data[r][i] !=0){
          				return i;
          			}
          		}
          		return -1;
          	},
          //	上移
          	moveUp: function(){
          		var before=String(this.data);
          		for(var c=0; c < 4; c++){
          			this.moveUpInCol(c);
          		}
          		var after=String(this.data);
          		if(before !=after){
          			this.randomNum();
          			if(this.isgameover()){
          				this.status=this.gameover;
          			}
          			this.dataView();
          		}
          	},
          	moveUpInCol: function(c){
          		for(var r=0;r < 4; r++){
          			var nextr=this.getNextInCol(r,c);
          			if(nextr !=-1){
          				if(this.data[r][c]==0){
          					this.data[r][c]=this.data[nextr][c];
          					this.data[nextr][c]=0;
          					r -- ;
          				}else if(this.data[r][c]==this.data[nextr][c]){
          					this.data[r][c] *=2;
          					this.data[nextr][c]=0;
          					this.score +=this.data[r][c];
          				}
          			}else{
          				break;
          			}
          		}
          	},
          	getNextInCol: function(r,c){
          		for(var i=r + 1; i < 4; i++){
          			if(this.data[i][c] !=0){
          				return i;
          			}
          		}
          		return -1;
          	},
          //	下移的方法
          	moveDown: function(){
          		var before=String(this.data);
          		for(var c=0;c < 4; c++){
          			this.moveDownInCol(c);
          		}
          		var after=String(this.data);
          		if(before !=after){
          			this.randomNum();
          			if(this.isgameover()){
          				this.status=this.gameover;
          			}
          			this.dataView();
          		}
          	},
          	moveDownInCol: function(c){
          		for(var r=3; r > 0; r--){
          			var prev=this.getPrevIncol(r,c);
          			if(prev !=-1){
          				if(this.data[r][c]==0){
          					this.data[r][c]=this.data[prev][c];
          					this.data[prev][c]=0;
          					r -- ;
          				}else if(this.data[r][c]==this.data[prev][c]){
          					this.data[r][c] *=2;
          					this.data[prev][c]=0;
          					this.score +=this.data[r][c];
          				}
          			}else{
          				break;
          			}
          		}
          	},
          	getPrevIncol: function(r,c){
          		for(var i=r - 1; i >=0; i--){
          			if(this.data[i][c] !=0){
          				return i;
          			}
          		}
          		return -1;
          	},
          }
          game.start();
          console.log(game.data)
          console.log(game.status);
          console.log(game.score);
          //鍵盤事件
          document.onkeydown=function(){
          	if(event.keyCode==37){
          		//console.log("左")
          		game.moveLeft();
          	}else if(event.keyCode==38){
          		//console.log("上")
          		game.moveUp()
          	}else if(event.keyCode==39){
          		//console.log("右")
          		game.moveRight()
          	}else if(event.keyCode==40){
          		//console.log("下")
          		game.moveDown()
          	}
          }
          //touch事件
          //手指按下
          var startX;//設定開始起始位置的x坐標
          var startY;//設定開始起始位置的y坐標
          var endX;//設定結束滑動位置的x坐標
          var endY;//設定結束滑動位置的y坐標
          document.addEventListener('touchstart',function(){
          //	console.log("手指按下了屏幕")
          	console.log(event);
          	startX=event.touches[0].pageX;
          	startY=event.touches[0].pageY;
          })
          //手指移動
          //document.addEventListener('touchmove',function(){
          //	console.log("手指的移動")
          //})
          //手指松開
          document.addEventListener("touchend",function(){
          //	console.log("手指松開")
          	console.log(event);
          	endX=event.changedTouches[0].pageX;//如何獲取結束時的位置x
          	endY=event.changedTouches[0].pageY;
          	var X=endX - startX;
          	var Y=endY - startY
          	var absX=Math.abs(X) > Math.abs(Y);
          	var absY=Math.abs(Y) > Math.abs(X);
          	if(X > 0 && absX){
          		console.log("右滑動")
          		game.moveRight()
          	}else if(X < 0 && absX){
          		console.log("左滑動")
          		game.moveLeft()
          	}if(Y > 0 && absY){
          		console.log("下滑動")
          		game.moveDown()
          	}if(Y < 0 && absY){
          		console.log("上滑動")
          		game.moveUp()
          	}
          })

          如下為游戲效果圖

          就這樣一個簡單的2048游戲就完成啦~


          最后

          非常感謝您能看到這里~

          關注我~帶給你更多驚喜~

          imulink中的Shift Arithmetic 模塊可以移動輸入信號中的數位和/或二進制小數點。今天主要是實例講解Simulink的Shift Arithmetic移位模塊。


          1.Shift Arithmetic 模塊

          模塊功能:Shift Arithmetic 模塊可以移動輸入信號中的數位和
          /或二進制小數點。
          庫:
          Simulink / Logic and Bit Operations
          HDL Coder / Logic and Bit Operations
          
          

          雙擊模塊可以查看模塊的參數設置:

          參數1:二進制位移位數:源 — 要移位的位的源  Source
          對話框 (默認) | 輸入端口
          指定是直接在對話框中輸入要移動的位數,還是從輸入端口繼承值。
          編程用法
          模塊參數:BitShiftNumberSource
          類型:字符向量、字符串
          值:'Dialog' | 'Input port'
          默認值:'Dialog'
          參數2:二進制位移位數:數量 — 二進制位移位數
          8 (默認) | 標量
          指定標量、向量或移位值數組。當二進制位移位數:源為對話框時,此參數才可用。
          如果方向為...  則...
          左側或右側    使用正整數指定移位。
          雙向      使用正整數表示右移,使用負整數表示左移。
          編程用法
          模塊參數:BitShiftNumber
          類型:字符向量、字符串
          值:scalar
          默認值:'9'
          
          
          參數3:二進制位移位數:方向 — 移位的方向
          左側 (默認) | 右側 | 雙向
          指定移位的方向:向左、向右或雙向。
          編程用法
          模塊參數:BitShiftDirection
          類型:字符向量、字符串
          值:'Left' | 'Right' | 'Bidrectional'
          默認值:'Bidrectional'
          
          
          參數4:二進制小數點移位數:數量 — 二進制小數點移位的位數
          0 (默認) | scalar
          指定將輸入信號中的二進制小數點移動的整數位數。正值表示右移,負值表示左移。
          編程用法
          模塊參數:BinPtShiftNumber
          類型:字符向量、字符串
          值:'Dialog' | 'Input port'
          默認值:'Dialog'
          
          
          參數5:超范圍移位值的診斷 — 診斷操作
          無 (默認) | 警告 | 錯誤
          指定當模塊包含超出范圍的移位值時,是否在仿真過程中生成警告或錯誤消
          息。選項包括:
          None - 不執行任何 Simulink操作。
          Warning - Simulink 顯示警告并繼續進行仿真。
          Error - Simulink 終止仿真并顯示錯誤。
          編程用法
          模塊參數:DiagnosticForOORShift
          類型:字符向量、字符串
          值:'None' | 'Warning' | 'Error'
          默認值:'None'
          
          
          參數6:檢查生成代碼中是否有超出范圍的 '二進制位移位數' — 檢查生成代碼中是
          否有超出范圍的二進制位移位數
          off (默認) | on
          如果選中此復選框,將在生成的代碼中包含條件語句,以防移位值超出范圍。
          當二進制位移位數:源為輸入端口時,此復選框才可用。
          編程用法
          模塊參數:CheckOORBitShift
          類型:字符向量、字符串
          值:'on' | 'off'
          默認值:'off'

          2.實例

          實例1

          3轉換成二進制得到"0011",將"0011"左移3位變成"0001 1000",再將"0001 1000"轉換成十進制就變成了24,移位其實就是乘以2的n次方,所以out=3*2^3=3*8=24。

          參數設置:


          實例2

          參數設置:


          實例3

          5的二進制是101,此時向右移動2位變成1.01,小數部分值為1,兩位二進制有4種,小數部分為0.25。5的二進制是101,此時向右移動1位變成10.1,小數部分值為1,1位二進制有2種,小數部分為0.5。

          具體轉換:
          5的二進制101=1*2^2+0*2^1+1*2^0=5
          1.01的二進制 1.01=1*2^0+0*2^(-1)+1*2^(-2)=1+0+0.25=1.25
          10.1的二進制 10.1=1*2^1+0*2^0+1*2^(-1)=2.5

          模塊參數:


          實例4

          模塊參數:

          實例5


          3.參考內容

          [1]https://ww2.mathworks.cn/help/simulink/slref/shiftarithmetic.html#bstdjf8_head

          [2] CSDN作者chhttty的文章《Simulink建模:位運算》,文章鏈接為:

          https://blog.csdn.net/u013288925/article/details/127520692

          本文內容來源于網絡,僅供參考學習,如內容、圖片有任何版權問題,請聯系處理,24小時內刪除。


          作 者 | 郭志龍

          編 輯 | 郭志龍
          校 對 | 郭志龍


          果提供了 alpha 值,則將顏色代碼轉換為rgb()或 rgba() 字符串。

          • 使用按位右移運算符和帶有&(and) 運算符的掩碼位將十六進制顏色代碼(帶或不帶前綴#)轉換為具有 RGB 值的字符串。
          • 如果是 3 位色碼,請先轉換為 6 位版本。
          • 如果在 6 位十六進制旁邊提供了 alpha 值,則rgba()返回字符串。

          JavaScript

          const hexToRGB=hex=> {
            let alpha=false,
              h=hex.slice(hex.startsWith('#') ? 1 : 0);
            if (h.length===3) h=[...h].map(x=> x + x).join('');
            else if (h.length===8) alpha=true;
            h=parseInt(h, 16);
            return (
              'rgb' +
              (alpha ? 'a' : '') +
              '(' +
              (h >>> (alpha ? 24 : 16)) +
              ', ' +
              ((h & (alpha ? 0x00ff0000 : 0x00ff00)) >>> (alpha ? 16 : 8)) +
              ', ' +
              ((h & (alpha ? 0x0000ff00 : 0x0000ff)) >>> (alpha ? 8 : 0)) +
              (alpha ? `, ${h & 0x000000ff}` : '') +
              ')'
            );
          };

          示例:

          hexToRGB('#27ae60ff'); // 'rgba(39, 174, 96, 255)'
          hexToRGB('27ae60'); // 'rgb(39, 174, 96)'
          hexToRGB('#fff'); // 'rgb(255, 255, 255)'

          更多內容請訪問我的網站:https://www.icoderoad.com


          主站蜘蛛池模板: 国产在线一区二区三区在线| 无码人妻av一区二区三区蜜臀 | 高清国产AV一区二区三区| 久久精品黄AA片一区二区三区| 伊人无码精品久久一区二区| 久久久精品日本一区二区三区| 亚洲午夜一区二区三区| 好爽毛片一区二区三区四| 国产乱子伦一区二区三区| 亚洲国产欧美日韩精品一区二区三区| 国产在线精品一区二区不卡| 国产精品无码一区二区在线观一 | 日本精品啪啪一区二区三区| 无码av免费一区二区三区| 无码8090精品久久一区| 日本一区二区三区在线看| 人妻视频一区二区三区免费| 制服丝袜一区在线| 人妻aⅴ无码一区二区三区| 无码少妇一区二区三区浪潮AV| 狠狠色婷婷久久一区二区| 久久综合一区二区无码| 中文字幕一区精品| 国产精品无码AV一区二区三区| 国产福利一区二区在线视频| 精品在线一区二区三区| 欧洲精品码一区二区三区免费看| 精品一区二区三区中文| 无码人妻一区二区三区免费视频| 精品视频午夜一区二区| 波多野结衣高清一区二区三区| 国产精品一区三区| 国产精品丝袜一区二区三区 | 免费一区二区无码东京热| 国产精品毛片a∨一区二区三区 | 国内精自品线一区91| 日韩爆乳一区二区无码| 日本免费一区二区三区| 久久久国产精品无码一区二区三区 | 综合人妻久久一区二区精品| 日本免费电影一区二区|