js實現(xiàn)跳一跳小游戲
本文實例為大家分享了js實現(xiàn)跳一跳小游戲的具體代碼,供大家參考,具體內(nèi)容如下
效果

流程分析
1、鼠標按下開始蓄力
2、鼠標松開,根據(jù)鼠標按下的時間讓小球運動相應(yīng)的距離
3、判斷小球落點是否在平臺內(nèi)
4、如果在平臺范圍內(nèi),產(chǎn)生下一個平臺,分數(shù)加10.如果不在游戲結(jié)束,判斷分數(shù)是否大于歷史最高分,更新歷史最高分。
動畫效果
5、鼠標按下小球所在平臺要有蓄力效果,鼠標松開后慢慢恢復(fù),
6、小球在空中的運動曲線要平滑
7、小球和平臺要有3D效果
注意事項
8、運動涉及到計算器和延時器,要注意清除定時器以免擾亂動畫效果
9、鼠標按下和松開為一個完整的流程,在小球運動完之前不能重復(fù)觸發(fā)
10、確保小球運動的時間與鼠標按下的時間相關(guān)聯(lián)
布局
<div class="wrap"> <h3 class="tit">鼠標按下蓄力,松開鼠標小球開始運動</h3> <h1>分數(shù):</h1> <h2>歷史最高:</h2> <div class="con"></div> <div class="blc1"></div> </div>
樣式
* {
margin: 0;
padding: 0;
}
.wrap {
height: 500px;
position: relative;
overflow: hidden;
}
.con {
background-color: hotpink;
/*左邊的沒有背景色,右邊的加了背景色*/
background-image: radial-gradient(10px at 4px 4px,
rgba(0, 0, 0, 0),
rgba(2, 2, 2, 1));
width: 20px;
height: 20px;
border-radius: 50%;
position: absolute;
left: 30px;
bottom: 30px;
z-index: 2;
}
.blc1 {
box-shadow: 10px 10px 7px #000;
border-radius: 30%;
width: 40px;
height: 40px;
background-color: midnightblue;
position: absolute;
left: 20px;
bottom: 20px;
}
.tit {
text-align: center;
}
js
function randomInt(min, max) { //產(chǎn)生[min,max]范圍內(nèi)的整數(shù)
return Math.round(Math.random() * (max - min)) + min;
}
function randomColor() { //產(chǎn)生隨機的顏色
var map = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'];
var str = '#';
for (var i = 0; i < 6; i++) {
var index = randomInt(0, 15);
str += map[index];
}
return str;
}
var wrap = document.querySelector('.wrap');
var con = document.querySelector('.con');
var oldtime = 0; //記錄鼠標按下的時間
var timer2 = null; //小球平拋運動定時器
var timer3 = null; //鼠標按下變形定時器
var num = 0; //游戲成績
var mouseD = false; //記錄鼠標是否按下
var mouseUp = true; //記錄鼠標是否松開
var h1 = document.querySelector('h1');
var h2 = document.querySelector('h2');
var max = localStorage.getItem('max');
var div = document.createElement('div'); //創(chuàng)建下一個平臺
h2.innerText = '歷史最高:' + localStorage.getItem('max'); //從瀏覽器數(shù)據(jù)庫獲取最高分
div.style.left = 35 + randomInt(30, 60) + 'px'; //初始化平臺距離左邊的值
var div_sex = randomInt(30, 70); //初始化平臺大小,范圍
div.style.bottom = 40 - div_sex / 2 + 'px'; //平臺距離底部為小球底部減去平臺
div.style.height = div_sex + 'px';
div.style.width = div_sex + 'px';
div.className = 'blc1'; //添加初始化樣式
div.style.backgroundColor = randomColor(); //隨機顏色
wrap.appendChild(div); //存入平臺
document.onmousedown = function () { //監(jiān)聽頁面點擊事件
if (!mouseD) { //如果鼠標按下沒有抬起
var blc = document.querySelectorAll('.blc1'); //獲取所有平臺
oldtime = Date.now(); //記錄鼠標按下的時間
var target = blc[blc.length - 2]; //小球所在的平臺
var down_c = 10; //平臺最大下沉距離
var left = target.offsetLeft; //記錄鼠標按下后下沉效果之前平臺的left值
var bottom = 40 - target.offsetHeight / 2; //記錄鼠標按下后下沉效果之前平臺的bottom值
var con_l = con.offsetLeft; //記錄鼠標按下后下沉效果之前小球的left值
var con_b = 30; //記錄鼠標按下后下沉效果之前小球的bottom值
timer3 = setInterval(() => { //下沉效果定時器
down_c -= 0.03; //每一次變化0.03px
if (down_c <= 0) { //最小值為0
down_c = 0.03;
}
target.style.boxShadow = down_c + 'px ' + down_c + 'px ' + down_c +
'px #000'; //邊框陰影向里縮,實現(xiàn)3D效果
target.style.left = left + 10 - down_c + 'px';
target.style.bottom = bottom + -10 + down_c + 'px';
con.style.left = con_l + 10 - down_c + 'px';
con.style.bottom = con_b - 10 + down_c + 'px'; //小球和平臺一起向右下角移動,配合陰影達到3D效果
}, 1);
mouseD = true; //鼠標已經(jīng)按下
mouseUp = false; //鼠標即將松開
}
}
document.onmouseup = function () {
if (!mouseUp) { //如果沒有觸發(fā)mousedown事件不會執(zhí)行以下代碼
mouseUp = true; //鎖定事件,小球運動結(jié)束前不能執(zhí)行以下代碼
clearInterval(timer3); //清除下沉動畫
var timer4 = null; //上升動畫
var blc = document.querySelectorAll('.blc1'); //獲取所有平臺
var target = blc[blc.length - 2]; //同下沉動畫
var left = target.offsetLeft;
var down_time = 0;
var down_c = 0;
var click_time = Date.now() - oldtime; //鼠標按下的時間
var bottom = 40 - target.offsetHeight / 2 - (click_time * 0.03 > 10 ? 10 : click_time *
0.03); //原來的bottom-下沉的距離,最大為10
timer4 = setInterval(() => { //恢復(fù)原來的狀態(tài)
down_time++;
if (down_time > click_time) {
clearInterval(timer4);
}
down_c += 0.03;
if (down_c >= 10) {
down_c = 10;
}
target.style.boxShadow = down_c + 'px ' + down_c + 'px ' + down_c + 'px #000';
target.style.left = left - down_c + 'px';
target.style.bottom = bottom + down_c + 'px';
}, 1);
var clicktime = (Date.now() - oldtime) * 1.5; //小球運動時間為鼠標按下的1.5倍,減少蓄力時間
var time2 = 0; //小球跳一跳計時器
var y = 30; //小球初始bottom值
var x = con.offsetLeft; //小球初始left值
clearTimeout(tout); //清除延時器
timer2 = setInterval(() => { //小球彈跳計時器
time2 += 20;
y = 30 + clicktime / 50 * Math.sin(time2 * Math.PI /
clicktime); //將總的時間分成180份通過sin(0,180)從0到0的特性完成一個圓滑的拋物線
con.style.left = x + time2 / 20 + 'px';
con.style.bottom = y + 'px';
}, 20);
var tout = setTimeout(function () { //控制小球運動的時間與鼠標按下的時間*1.5相等,控制小球運動的時間
clearInterval(timer2); //結(jié)束小球運動
x = con.offsetLeft; //獲取運動結(jié)束后小球的left值
var blc = document.querySelectorAll('.blc1'); //獲取所有的平臺
if (con.offsetLeft >= wrap.lastElementChild.offsetLeft && con.offsetLeft <= wrap
.lastElementChild.offsetLeft + wrap.lastElementChild.offsetHeight - 10
) { //判斷小球是否位于平臺里面
num += 10; //每跳一次加10分
h1.innerText = '分數(shù):' + num; //動態(tài)展示分數(shù)
//生成下一個平臺
var div_sex2 = randomInt(40, 60);
var newdiv = document.createElement('div');
newdiv.style.bottom = 40 - div_sex2 / 2 + 'px';
newdiv.style.height = div_sex2 + 'px';
newdiv.style.width = div_sex2 + 'px';
newdiv.style.left = x + randomInt(80, 120) + 'px';
newdiv.style.backgroundColor = randomColor();
newdiv.className = 'blc1';
wrap.appendChild(newdiv);
} else {
alert('游戲結(jié)束,分數(shù):' + num);
max = max > num ? max : num;
localStorage.setItem('max', max) //更新最高分
location.reload(); //刷新當(dāng)前頁面
}
wrap.scrollLeft = wrap.scrollWidth; //控制滾動條位于最右邊
mouseD = false; //完成一次事件,重新開啟
mouseUp = true; //
}, clicktime)
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中的console.group()函數(shù)詳細介紹
這篇文章主要介紹了JavaScript中的console.group()函數(shù)詳細介紹,當(dāng)程序調(diào)試日志過多時會有些雜亂,此時可以使用console.group()函數(shù)調(diào)來分組顯示,需要的朋友可以參考下2014-12-12
JavaScript整除運算函數(shù)ceil和floor的區(qū)別分析
這篇文章主要介紹了JavaScript整除運算函數(shù)ceil和floor的區(qū)別分析,實例分析了ceil和floor函數(shù)的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
js動態(tài)創(chuàng)建表格,刪除行列的小例子
這篇文章介紹了js動態(tài)創(chuàng)建表格,刪除行列的實例代碼,有需要的朋友可以參考一下2013-07-07

