您的位置:首页 > 编程语言

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

2009-01-02 23:53 465 查看
function viewPage(html) {
var page = window.open('', '', '');
page.opener = null;
page.document.write(html);
page.document.close();
}

【编程游戏】贺岁放礼花。(第一名奖励10000可用分)
作者:


点燃 [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

<html>
<head><title>贺岁礼花--2009清洁工版</title></head>
<body style="overflow:hidden;background-color:Black;">
</body>
<script type="text/javascript">
function hsl2color(hsl) {
if (hsl.h > 360 || hsl.h < 0 || hsl.s > 100 || hsl.s < 0 || hsl.l > 100 || hsl.l < 0)
return "#000000";
var rgb = {r: 0, g: 0, b: 0};
if (hsl.h <= 60) {
rgb.r = 255;
rgb.g = Math.floor(255 / 60 * hsl.h);
} else if (hsl.h <= 120) {
rgb.r = Math.floor(255 - (255 / 60) * (hsl.h - 60));
rgb.g = 255;
} else if (hsl.h <= 180) {
rgb.g = 255;
rgb.b = Math.floor((255 / 60) * (hsl.h - 120));
} else if (hsl.h <= 240) {
rgb.g = Math.floor(255 - (255 / 60) * (hsl.h - 180));
rgb.b = 255;
} else if (hsl.h <= 300) {
rgb.r = Math.floor((255 / 60) * (hsl.h - 240));
rgb.b = 255;
} else if (hsl.h <= 360) {
rgb.r = 255;
rgb.b = Math.floor(255 - (255 / 60) * (hsl.h - 300));
}
var sat = Math.abs((hsl.s - 100) / 100);
rgb.r = Math.floor(rgb.r - (rgb.r - 128) * sat);
rgb.g = Math.floor(rgb.g - (rgb.g - 128) * sat);
rgb.b = Math.floor(rgb.b - (rgb.b - 128) * sat);
var lum = (hsl.l - 50) / 50;
if (lum > 0) {
rgb.r = Math.floor(rgb.r + (255 - rgb.r) * lum);
rgb.g = Math.floor(rgb.g + (255 - rgb.g) * lum);
rgb.b = Math.floor(rgb.b + (255 - rgb.b) * lum);
} else if (lum < 0) {
rgb.r = Math.floor(rgb.r + rgb.r * lum);
rgb.g = Math.floor(rgb.g + rgb.g * lum);
rgb.b = Math.floor(rgb.b + rgb.b * lum);
}
return "#" + ("00000" + (rgb.r * 256 * 256 + rgb.g * 256 + rgb.b).toString(16)).replace(/^.*(.{6}$)/g, "$1");
}

function Ball(parent, text) {
this.parent = parent;
this.text = text;
this.visible = true;
this.span = document.createElement("span");
this.span.innerHTML = this.text;
this.span.style.position = "absolute";
}

Ball.prototype.update = function(color, x, y, size, alpha, speed, fire) {
this.fire = fire;
this.alpha = alpha;
this.speed = speed;
this.gravity = 0;
this.x = x;
this.y = y;
this.color = color;
this.hsl = {h: color, s: 100, l: 80};
this.size = size;
this.doChange();
}

Ball.prototype.doChange = function() {
with (this.span.style) {
fontSize = this.size + "px";
left = this.x + "px";
top = this.y + "px";
color = hsl2color(this.hsl);
}
if (!this.span.parent) this.parent.insertBefore(this.span, this.parent.firstChild);
}

Ball.prototype.move = function() {
this.x = Math.cos(this.alpha) * this.speed + this.x;
this.y = Math.sin(this.alpha) * this.speed + this.y;
this.y = this.gravity + this.y;
this.gravity += 0.05;
this.hsl.l -= this.fire;
this.doChange();
}

Ball.prototype.hide = function() {
if (!this.visible) return;
this.visible = false;
this.span.style.display = "none";
}

Ball.prototype.show = function() {
if (this.visible) return;
this.visible = true;
this.span.style.display = "block";
}

Ball.prototype.dispose = function () {
this.disposing = true;
for (var i in this) {
if (i == "parent") continue;
if (typeof this[i].dispose == "function" && !this[i].disposing) this[i].dispose();
if (typeof this[i].parentNode == "object")
this[i].parentNode.removeChild(this[i]);
delete this[i];
}
}

function Firework(parent, text, size, count, speed) {
this.parent = parent;
this.speed = speed;
this.size = size;
this.total = 0;
this.active = false;
this.balls = new Array(count);
for (var i = 0; i < this.balls.length; i++) {
this.balls[i] = new Ball(parent, text);
this.balls[i].firework = this;
this.balls[i].index = i;
this.balls[i].hide();
}
}

Firework.prototype.tick = function() {
this.total++;
if (typeof this.ontick == "function") this.ontick(this);
for (var i = 0; i < this.balls.length; i++) {
if (!this.balls[i].visible) continue;
if (typeof this.onballmove == "function")
this.onballmove(this.balls[i]);
else this.balls[i].move();
}
var self = this;
this.timer = setTimeout(function() { self.tick(); }, 10);
}

Firework.prototype.stop = function() {
if (!this.active) return;
this.active = false;
clearTimeout(this.timer);
for (var i = 0; i < this.balls.length; i++)
this.balls[i].hide();
if (typeof this.onstop == "function") this.onstop(this);
}

Firework.prototype.replay = function() {
this.stop();
this.active = true;
this.selected = -1;
this.total = 0;
this.tick();
if (typeof this.onreplay == "function") this.onreplay(this);
}

var firework = new Firework(document.body, "●", 32, 15, 7.5);
firework.alpha = 0.5 - Math.random();
firework.offset = Math.random() < 0.5 ? +1 : -1;
firework.ontick = function () {
if (this.total % 10 == 0) {
var h = document.body.clientHeight || document.documentElement.clientHeight;
var w = document.body.clientWidth || document.documentElement.clientWidth;
this.selected = (this.selected + 1) % this.balls.length;
var alpha = this.alpha + 1 / this.balls.length * this.offset;
if (alpha < -0.5 || alpha > 0.5) this.offset = -this.offset;
this.alpha = alpha;
this.balls[this.selected].update(
typeof this.color == "undefined" ? Math.random() * 360 : this.color,
w / 2, h,
this.size, Math.PI * 1.5 + this.alpha * 2, this.speed, 0.5
);
this.balls[this.selected].show();
}
}
firework.replay();

var firework2 = new Firework(document.body, "●", 15, 20, 12.5);
firework2.ontick = function () {
if (this.total % 50 == 0) {
var h = document.body.clientHeight || document.documentElement.clientHeight;
var w = document.body.clientWidth || document.documentElement.clientWidth;
var ball = firework.balls[Math.floor(firework.selected + firework.balls.length / 2) % firework.balls.length];
if (!ball || !ball.visible) return;
ball.hide();
for (var i = 0; i < this.balls.length; i++) {
this.balls[i].update(
ball.color,
ball.x, ball.y, this.size, 2 * Math.PI * Math.random(), Math.random() * this.speed, 1.5);
this.balls[i].show();
}
}
}
firework2.replay();
</script>
</html>

点燃 [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐