时间:2023-09-12 16:24:02 | 来源:网站运营
时间:2023-09-12 16:24:02 来源:网站运营
制作2048小游戏(html、css、JS):简约的小游戏的制作主要由以下思路结构:<body> <div id="header"> <h1 class="title1">2048</h1> <h3 class="title2">Created by Cheng Xiao Gang</h3> <div class="wrapper"> <div class="score-wrapper"> <span id="txt">score:</span> <span id="score">0</span> </div> <div class="newgame-wrapper"> <a href="javascript:newgame()" id="newGame">New Game</a> </div> </div> </div> <div id="grid-container"> <div class="grid-cell" id="grid-cell-0-0"></div> <div class="grid-cell" id="grid-cell-0-1"></div> <div class="grid-cell" id="grid-cell-0-2"></div> <div class="grid-cell" id="grid-cell-0-3"></div> <div class="grid-cell" id="grid-cell-1-0"></div> <div class="grid-cell" id="grid-cell-1-1"></div> <div class="grid-cell" id="grid-cell-1-2"></div> <div class="grid-cell" id="grid-cell-1-3"></div> <div class="grid-cell" id="grid-cell-2-0"></div> <div class="grid-cell" id="grid-cell-2-1"></div> <div class="grid-cell" id="grid-cell-2-2"></div> <div class="grid-cell" id="grid-cell-2-3"></div> <div class="grid-cell" id="grid-cell-3-0"></div> <div class="grid-cell" id="grid-cell-3-1"></div> <div class="grid-cell" id="grid-cell-3-2"></div> <div class="grid-cell" id="grid-cell-3-3"></div> </div></body>
#header{ width: 100%; margin: 0 auto; text-align: center;}#header .title1{ font: bold 50px Arial; color:#776e65; margin:0 auto;}#header .title2{ font:normal 18px Arial; margin:10px auto; padding-left:20%;}#header .wrapper{ display:flex; justify-content:space-between; width:500px; margin:0 auto;}#header .score-wrapper{ width:100px; background-color:#8f7a66; padding:6px; border-radius:6px;}#header .score-wrapper #txt{ color:#d2c1c1;}#header .score-wrapper #score{ color:#fff; font-size:30px; vertical-align:middle;}#header .newgame-wrapper #newGame{ width:100px; display:block; background-color:#8f7a66; text-decoration:none; padding:13px; border-radius:6px; color:#fff; font:bold 15px Arial;}#header .newgame-wrapper #newGame:hover{ background-color:#bdac9c;}
function init(){ //初始化单元格位置(下层单元格) for(var i=0;i<4;i++){ for(var j=0;j<4;j++){ var gridCell=$('#grid-cell-'+i+'-'+j); gridCell.css('top',getPosTop(i,j)); gridCell.css('left',getPosLeft(i,j)); } } //初始化数组 for(var i=0;i<4;i++){ nums[i]=new Array(); hasConflicted[i]=new Array(); for(var j=0;j<4;j++){ nums[i][j]=0; hasConflicted[i][j]=false; //false表示未曾叠加过,true表示已经叠加过 } } //动态创建上层单元格并初始化 updateView(); score=0; updateScore(score);}
当点击重新开始后,产生随机数。建立该独立函数用于每次移动时都会产生随机数,且要求每次的随机数都出现在空白处,避免与原有数值重合。function generateOneNumber(){ //判断是否还有空间,如果没有空间则直接返回 if(noSpace(nums)){ return; } //随机一个位置 var count=0; var temp=new Array(); for(var i=0;i<4;i++){ for(var j=0;j<4;j++){ if(nums[i][j]==0){ temp[count] = i*4+j; // 1*4+3=7 ------> 7/4=1 7%4=3 count++; } } } var pos=Math.floor(Math.random()*count); //[0,1) * 6 = [0,5] var randx=Math.floor(temp[pos]/4); var randy=Math.floor(temp[pos]%4); //随机一个数字 var randNum=Math.random()<0.5?2:4; //在随机位置上显示随机数字 nums[randx][randy]=randNum; showNumberWithAnimation(randx,randy,randNum);}
2.滑动组合function canMoveLeft(nums){ for(var i=0;i<4;i++){ for(var j=1;j<4;j++){ if(nums[i][j]!=0){ if(nums[i][j-1]==0 || nums[i][j-1]==nums[i][j]){ return true; } } } } return false;}
当左边数值和需要移动的数值相同时,这时需要叠加组合。if(nums[i][k]==nums[i][j] && noBlockHorizontal(i,k,j,nums) && !hasConflicted[i][k]){ showMoveAnimation(i,j,i,k); nums[i][k]+=nums[i][j]; nums[i][j]=0; //统计分数 score+=nums[i][k]; updateScore(score); hasConflicted[i][k]=true; //已经叠加 break;}
简约的游戏少不了动画效果的真实性,需要建立从起始点到末端的动画以及延时效果。//通过动画显示移动function showMoveAnimation(fromx,fromy,tox,toy){ var numberCell=$('#number-cell-'+fromx+'-'+fromy); numberCell.animate({ top:getPosTop(tox,toy), left:getPosLeft(tox,toy) }, 200);}
文章主要描述重要代码,下方有成品文件,解压后将index1.html放入浏览器即可。关键词:游戏