潜水博客园多年,从未写过博客。最近才注册博客,遂将很久前写的俄罗斯方块分享出来。第一次写博客,不喜勿喷。。。
var Shape = function () { }; Shape.prototype.Init = function () { this.blocks = []; var random = parseInt(Math.random() * 100); this.status = random % 4; eval("this.st" + this.status + "()");//random status this.fillStyle = _config.blockColors[random % _config.blockColors.length];//random color } Shape.prototype.Down = function () { this.x += 1; for (var i = 0; i < this.blocks.length; i++) { this.blocks[i].x += 1; } } Shape.prototype.Left = function () { this.y -= 1; for (var i = 0; i < this.blocks.length; i++) { this.blocks[i].y -= 1; } } Shape.prototype.Right = function () { this.y += 1 for (var i = 0; i < this.blocks.length; i++) { this.blocks[i].y += 1; } } Shape.prototype.st0 = function () { };//abstract method Shape.prototype.st1 = function () { }; Shape.prototype.st2 = function () { }; Shape.prototype.st3 = function () { }; Shape.prototype.Rotate = function (i) {//rotate to target status var st = (this.status + i) % 4; this.status = st; switch (st) { case 0: this.st0(); break; case 1: this.st1(); break; case 2: this.st2(); break; case 3: this.st3(); break; default: this.st0(); break; } }
var Triangle = function (x, y) { this.x = x; this.y = y; this.Init(); }; Triangle.InheritFrom(Shape, null, null); ShapeFactory.AddShape(Triangle); Triangle.prototype.st0 = function () { this.blocks[0] = new Block(this.x - 1, this.y, 1); this.blocks[1] = new Block(this.x, this.y - 1, 1); this.blocks[2] = new Block(this.x, this.y, 1); this.blocks[3] = new Block(this.x, this.y + 1, 1); } Triangle.prototype.st1 = function () { this.blocks[0] = new Block(this.x, this.y + 1, 1); this.blocks[1] = new Block(this.x - 1, this.y, 1); this.blocks[2] = new Block(this.x, this.y, 1); this.blocks[3] = new Block(this.x + 1, this.y, 1); } Triangle.prototype.st2 = function () { this.blocks[0] = new Block(this.x + 1, this.y, 1); this.blocks[1] = new Block(this.x, this.y + 1, 1); this.blocks[2] = new Block(this.x, this.y, 1); this.blocks[3] = new Block(this.x, this.y - 1, 1); } Triangle.prototype.st3 = function () { this.blocks[0] = new Block(this.x, this.y - 1, 1); this.blocks[1] = new Block(this.x + 1, this.y, 1); this.blocks[2] = new Block(this.x, this.y, 1); this.blocks[3] = new Block(this.x - 1, this.y, 1); }
用于实现游戏中的事件通知,如得分、游戏结束等。
_events = (function () {//Pub&Sub var topics = {}, uuid = 0, event = function () { this.listen = function (topic, callback) { if (typeof topic !== "string" || typeof callback !== "function") return this; if (!topics[topic]) { topics[topic] = []; } callback.uuid = uuid++; topics[topic].push(callback); return this; }; this.trigger = function (src, topic, data) { if (!topics[topic] || topics[topic].length === 0) return this; var callbacks = topics[topic], i = 0, length = callbacks.length; for (; i < length; i++) { callbacks[i].call(src, data); } return this; }; this.remove = function (topic, callback) { if (!topics[topic] || topics[topic].length === 0) return this; var callbacks = topics[topic], i = 0, length = callbacks.length; for (; i < length; i++) { if (callback.uuid === callbacks[i].uuid) callbacks.splice(i, 1); } return this; }; }; return event; })();
BlockGame.prototype.ListenNewShapeEvents = function (fn) { this.events.listen(_eventEnum.newShape, fn); return this; }
用于随机的创建下一个图形。
var ShapeFactory = (function () {//absolute factory var ShapeArr = []; return { CreateShape: function (x, y) {//create a random shape var random = Math.floor(Math.random() * 100); return new ShapeArr[random % ShapeArr.length](x, y); }, AddShape: function (shape) { ShapeArr.push(shape); } }; })();
原文:http://www.cnblogs.com/viczha/p/3542951.html