標籤:style blog http io os ar for 2014 art
相信接元寶的遊戲大家都很熟悉了,自從抓住神經貓火了之後,遊戲越來越多,html5像人們料想的那樣逐漸佔據舞台。當然由於瀏覽器安全色的問題,html5的遊戲依然只能在移動端大展拳腳,不過沒關係。今天博主給大家帶來一個jquery接元寶的小遊戲,即使在ie6下也可以運行。
不多說先上代碼:
<!DOCTYPE PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang = "en" >
<head><meta charset = "UTF-8" >
<script src = "jquery-1.8.3.min.js" ></script>
<title>遊戲</title > </head>
<body>
<div style="background:url(bei.png);position:absolute;margin:auto auto;width:650px;height:450px;border:1px solid #131E63" id="stage">
<img src="xiaozhu.png" style="position:absolute;bottom:10px;left:260px" id="npc"/ > </div>
<div style="position:absolute;height:20px;width:200px;">得分<span id="num">0</span > </div>
<div style="float:right;position:absolute;height:20px;width:200px;left:600px">時間<span id="time">15</span > </div>
<div style="position:absolute;top:480px;left:100px;">
提示:a鍵向左移動,d鍵向右移動
</div>
</body>
<script>
$(function(){
// 構造人物
var npc = $("#npc"),
left, time = $("#time"),
stage = $("#stage");
//計時器
var timer = 15;
t = setInterval(function() {
timer--;
time.html(timer);
}, 1000);
//讀取鍵盤事件
var left = npc.css(‘left‘).match(/\d+/) * 1;
$(document).keydown(function(d) {
//alert(d.keyCode)
d.keyCode==68;
alert(d.keyCode);
if (d.keyCode == 68) {
left = npc.css(‘left‘).match(/\d+/) * 1;
if (left > 550) {
return false;
} else {
npc.css("left", left + 25 + ‘px‘);
}
}
if (d.keyCode == 65) {
left = npc.css(‘left‘).match(/\d+/) * 1;
//alert(left)
if (left <= 0) {
return false;
} else {
npc.css("left", left - 25 + ‘px‘);
}
}
});
//載入元寶節點
var rank = [],
num = $("#num"),
number;
rank[0] = ‘<img src="yuanbao.png" class="money" style="position:absolute;top:0;left:0;display:none;"/>‘;
rank[1] = ‘<img src="yuanbao.png" class="money-1" style="position:absolute;top:0;left:0;display:none;"/>‘;
rank[2] = ‘<img src="yuanbao.png" class="money-2" style="position:absolute;top:0;left:0;display:none;"/>‘;
$(stage).append(rank[0]);
$(stage).append(rank[1]);
$(stage).append(rank[2]);
//建立元寶對象
function money(dom, left, top) {
this.i = 2;
this.dom = $(dom);
this.left = left;
this.top = top;
this.dom.css("top", top).css("left", left).css("display", "block"); //設定元寶初始位置
this.flag = true; //加分鎖
}
money.prototype = {
//元寶對象的move方法
move: function() {
if (timer <= 0) {
stop();
clearTimeout(t);
clearTimeout(m);
clearTimeout(t1);
clearTimeout(t2);
clearTimeout(t3);
return false;
}
var val = Math.abs(left - this.left);
this.i += 0.03;
this.top = this.dom.css("top").match(/\d+/) * 1;
if (this.top >= 335 && val <= 45) {
this.dom.css("display", "none");
this.i = 2;
if (this.flag == true) { //加分
//console.log(this.flag)
number = num.html() * 1 + 20;
num.html(number);
this.flag = false; //鎖分
} else {
return false;
}
} else if (this.top > 370) { //未接到元寶,元寶消失,收回
this.dom.css("display", "none");
this.i = 2;
clearTimeout(this.t);
this.flag = false; //收回同樣鎖分
} else { //元寶運動
this.dom.css("top", this.top + this.i + "px");
}
}
};
//遊戲開始,產生元寶執行個體
start();
m = setInterval(function() {
clearTimeout(t1);
clearTimeout(t2);
clearTimeout(t3);
start();
}, 3000);
function start() {
var top = [],
left = [];
for (var i = 0; i < 3; i++) {
top[i] = Math.floor(Math.random() * 200 + 200);
left[i] = Math.floor(Math.random() * 400);
//console.log(top);
}
var qy = new money(‘.money‘, top[0], left[0]);
t1 = setInterval(function() {
qy.move();
}, 15);
var by = new money(‘.money-1‘, top[1], left[1]);
t2 = setInterval(function() {
by.move();
}, 15);
var cy = new money(‘.money-2‘, top[2], left[2]);
t3 = setInterval(function() {
cy.move();
}, 15);
}
});
//結束操作
function stop() {
var ove = ‘<div style="9999px;height:9999px;z-index:99;opacity:0.5;background:#fff;"></div>‘,
over = ‘<div style="position:absolute;top:200px;left:200px;font-size:20px;height:500px;width:200px;z-index:999" id="over">時間到啦!!!!!!</div>‘;
$("body").append(ove);
$("body").append(over);
}
</script>
</html >
OK,基本上代碼不難,就是利用物件導向的思想,這裡最值得一提的便是構造元寶的執行個體,因為元寶要隨機出現,並且落地後(無論接到與否),都要獎構建的執行個體回收並且重新構造。當然,操作的任務移動其實很簡單,並沒有多少難度,只要將元寶構建完畢即可,另外元寶的屬性和方法最核心的便是掉落的方法,然後我們在掉落方法中加入判斷函數即可。
最後:
jquery小遊戲之接元寶