//Game display piece size+spacing side = 20; space = 2; //Game controls CONTROL = {}; CONTROL.LEFT = Key.LEFT; CONTROL.RIGHT = Key.RIGHT; CONTROL.DOWN = Key.DOWN; CONTROL.ROTCW = Key.UP; CONTROL.ROTCCW = Key.CONTROL; CONTROL.DROP = new String('Q').charCodeAt(0); CONTROL.PAUSE = new String('W').charCodeAt(0); CONTROL.NEWGAME = new String(' ').charCodeAt(0); //Stage setup Stage.scaleMode = 'noScale'; Stage.align = 'TL'; //Clip setup this.createEmptyMovieClip('board_mc', 0); this.createEmptyMovieClip('queue_mc', 1); this.createEmptyMovieClip('dP', 2); //Create/format fields this.createTextField('info_txt', 5, 0, 0, 100, 100); info_txt.html = true; info_txt.multiline = true; info_txt.wordWrap = false; this.createTextField('ctrls_txt', 7, 0, 120, 200, 500); ctrls_txt.html = true; ctrls_txt.htmlText = 'CONTROLS\nLEFT: LEFT ARROW\nRIGHT: RIGHT ARROW\nDOWN: DOWN ARROW\nROT CW: UP ARROW\nROT CCW: CONTROL\nDROP: Q\nPAUSE: W\nNEWGAME: SPACE'; ctrls_txt.setTextFormat(new TextFormat('_typewriter')); this.createTextField('disp_txt', 6, (Stage.width-100)/2, (Stage.height-20)/2, 100, 20); disp_txt.border = true; disp_txt.autoSize = 'left'; disp_txt.backgroundColor = 0xCCCCCC; disp_txt.selectable = false; disp_txt.background = true; //Center all on Stage resize this.onResize = function() { disp_txt._x = (Stage.width-disp_txt._width)/2; disp_txt._y = (Stage.height-disp_txt._height)/2; board_mc._x = (Stage.width-board_mc._width)/2; board_mc._y = (Stage.height-board_mc._height)/2; queue_mc._x = board_mc._x+board_mc._width+20; queue_mc._y = board_mc._y; dP._x = board_mc._x+gridC(dP.gX); dP._y = board_mc._y+gridC(dP.gY); }; Stage.addListener(this); //Center when display text changes disp_txt.addProperty('texts', function () { return this.text; }, function (t) { this.text = t; onResize(); }); //======================================================================= //Array of Tetris Pieces var p, q; p = pieces=[]; //I piece q = p[0]=dim(5, 5, 0); q[2][1] = q[2][2]=q[2][3]=q[2][4]=0x00FFFF; //Z piece q = p[1]=dim(3, 3, 0); q[0][0] = q[0][1]=q[1][1]=q[1][2]=0xFF0000; //S piece q = p[2]=dim(3, 3, 0); q[1][0] = q[1][1]=q[0][1]=q[0][2]=0x00FF00; //J piece q = p[3]=dim(3, 3, 0); q[0][0] = q[1][0]=q[1][1]=q[1][2]=0x0000FF; //L piece q = p[4]=dim(3, 3, 0); q[0][2] = q[1][0]=q[1][1]=q[1][2]=0xFF9900; //O piece q = p[5]=dim(2, 2, 0xFFFF00); //T piece q = p[6]=dim(3, 3, 0); q[0][1] = q[1][0]=q[1][1]=q[1][2]=0xCC66CC; //Created 2D Array with default values of 'c' function dim(x, y, c) { var row = []; for (var X = 0; X=0; ivar--) { var p = random(ivar+1); var t = this[ivar]; this[ivar] = this[p]; this[p] = t; } return this; }; //Positions MovieClip from top-left corner MovieClip.prototype.pos = function(x, y) { var b = this.getBounds(this._parent); this._x = this._x-b.xMin+x; this._y = this._y-b.yMin+y; }; //Takes next piece from queue array queue_mc.pull = function() { var p = pieceQueue.shift(); if (pieceQueue.length == 7) { pieceQueue = pieceQueue.concat(new Array(0, 1, 2, 3, 4, 5, 6).shuffle()); } this.removeFirst(); this.addPiece(); return p; }; //Removes first piece in queue queue_mc.removeFirst = function() { var clip = this.clip; clip.shift().removeMovieClip(); var cy = queue_mc.clip[0]._y; for (var c = 0; cLines: '+lines+'
Level: '+level; }; //Changes game speed based on level info.watch('level', function (p, o, n) { this.updateScore(this.score, this.lines, n); clearInterval(gameID); gameID = setInterval(gameplay, 1000-(n-1)*999/29); return n; }); info.watch('lines', function (p, o, n) { this.updateScore(this.score, n, this.level); return n; }); info.watch('score', function (p, o, n) { this.updateScore(n, this.lines, this.level); return n; }); //Initial field views info.lines = 0; //Can use querystring to test different levels, e.g. tetris.swf?level=30 info.level = _root.level == undefined ? 1 : _root.level; info.score = 0; info_txt._visible = true; disp_txt.texts = ''; disp_txt._visible = false; }