【编程游戏】贺岁放礼花。(点燃续帖1-37楼muxrwc的焰火)

【编程游戏】贺岁放礼花。(第一名奖励10000可用分)
作者:
点燃  [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>燃放烟花Bata版</title> <script type="text/javascript"> if ('undefined' != typeof HTMLElement && HTMLElement.prototype && !HTMLElement.prototype.insertAdjacentHTML) HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) { //BlueDestiny var df; var r = this.ownerDocument.createRange(); switch (String(sWhere).toLowerCase()) { case "beforebegin": r.setStartBefore(this); df = r.createContextualFragment(sHTML); this.parentNode.insertBefore(df, this); break; case "afterbegin": r.selectNodeContents(this); r.collapse(true); df = r.createContextualFragment(sHTML); this.insertBefore(df, this.firstChild); break; case "beforeend": r.selectNodeContents(this); r.collapse(false); df = r.createContextualFragment(sHTML); this.appendChild(df); break; case "afterend": r.setStartAfter(this); df = r.createContextualFragment(sHTML); this.parentNode.insertBefore(df, this.nextSibling); break; } }; var FireworksBase = function (member) { //烟花基类 if (!(this.constructor.prototype instanceof arguments.callee)) this.constructor = arguments.callee; if (member.prototype) for (var i in member.prototype) this[i] = member.prototype[i]; with ({ domid : '', velocity : 0, angle : 0, side : 0, startX : 0, startY : 0, end : 0, x : 0, y : 0, t : 0 }) //初始值 with (member) { this.domid = domid; //DOM对象ID this.velocity = velocity; //初始速度 this.angle = angle; //角度 this.side = side; //边长 this.startX = startX; //初始x值 this.startY = startY; //初始y值 this.end = end; //最大y值 this.x = x, this.y = y; //当前位置 this.t = t; //当前时间 } if (this.domid) { if (!document.body) throw new Error('Body未初始化'); document.body.insertAdjacentHTML('beforeEnd', '<div id="' + this.domid + '" style="position:absolute;width:' + this.side + 'px;height:' + this.side + 'px;font-size:' + this.side + 'px;">●</div>'); this.dom = document.getElementById(this.domid); //DOM对象 if (!this.dom) throw new Error('烟花DOM元素不存在'); } }; var Fireworks = function (member) { //烟花类 if (!(this.constructor.prototype instanceof arguments.callee)) this.constructor = arguments.callee; this.constructor.prototype.constructor.call(this, member); //call super this.time = member.time || 0; //最大时间 }; var FireworksFragment = function (member) { //烟花碎片类 if (!(this.constructor.prototype instanceof arguments.callee)) this.constructor = arguments.callee; this.constructor.prototype.constructor.call(this, member); //call super this.set(this.startX, this.startY); }; FireworksBase.prototype = { displacement : function (v, t) { //位移公式:vt - 0.5gt^2 = S return v * t - t * t * 98 * .5; //重力加速度98像素 } , v : function (velocity, angle) { //速度 var radian = Math.PI / 180 * angle; return { x : -Math.cos(radian) * velocity, y : -Math.sin(radian) * velocity }; } , set : function (x, y) { //设置位置 if ('undefined' != typeof x) this.dom.style.left = (this.x = x) + 'px'; if ('undefined' != typeof y) this.dom.style.top = (this.y = y) + 'px'; } , run : function () {} , destruction : function () { //销毁对象 this.dom.parentNode.removeChild(this.dom); this.domid = ''; //DOM对象ID this.velocity = 0; //初始速度 this.angle = 0; //角度 this.side = 0; //边长 this.startX = 0; //初始x值 this.startY = 0; //初始y值 this.end = 0; //最大y值 this.x = 0; this.y = 0; this.t = 0; this.dom = null; } }; Fireworks.prototype = new FireworksBase({ prototype : { run : function (time) { this.t += time; var v = this.v(this.velocity, this.angle); var x = Math.ceil(v.x * this.t); var y = Math.ceil(this.displacement(v.y, this.t)); if (this.time > this.t && this.startY - y < this.end) { this.set(this.startX + x, this.startY - y); return true; } else { return false; } } , blast : function (list) { list = list || []; for (i = 0 ; i < 15 ; i ++) { list[list.length] = new FireworksFragment({ domid : this.dom.id + '_' + i , side : 5 , velocity : 49 + Math.round(Math.random() * 49) , angle : Math.floor(Math.random() * 360) , startY : this.y + Math.floor(this.side / 2) - 2 , startX : this.x + Math.floor(this.side / 2) - 2 , end : this.end }); } return list; } , destruction : function (list) { this.blast(list); this.constructor.prototype.constructor.prototype.destruction.call(this); //call super destruction } }}); FireworksFragment.prototype = new FireworksBase({ prototype : { run : function (time) { this.t += time; var v = this.v(this.velocity, this.angle); var x = Math.ceil(v.x * this.t); var y = Math.ceil(this.displacement(v.y, this.t)); if (this.startY - y < this.end) { this.set(this.startX + x, this.startY - y); return true; } else { return false; } } }}); var Timer = function (member) { with ({ list : [] }) with (member) { this.list = list; this.time = time; this.step = step; } }; Timer.prototype.run = function (F, one) { var wc = this, next = false; with (this) { for (var i = 0 ; i < list.length ; i ++) { if (!list[i]) continue; if (list[i].run(step)) next = true; else { list[i].destruction(list); list[i] = null; } } clear(); !one && next && window.setTimeout(function () { wc.run(F) }, time) || F && F(); } }; Timer.prototype.clear = function () { for (var i = this.list.length - 1 ; i > -1 ; i --) (this.list[i] === null) && this.list.splice(i, 1); }; window.onload = function () { //- -卡成这样了,还要不要做轨迹了。。。 var N = 0; var C = function (id) { return new Fireworks({ domid : id + (++ N) , velocity : 300 + Math.floor(Math.random() * 200) //每秒200到500像素的发射速度 , angle : 240 + Math.floor(Math.random() * 60) , side : 10 , startY : 500 , startX : 500 , end : 500 , time : 1 }); }; var t = new Timer({ list : [C('example')], time : 10, step : 10 / 1000 }); window.setInterval(function () { t.run(null, true); }, t.time); window.setInterval(function () { t.list.push(C('example')); }, 2000); }; </script> </head> <body> </body> </html>
点燃  [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

你可能感兴趣的:(游戏,编程,function,list,velocity,prototype)