Tetris

by Elijah Cirioli

by ElijahCirioli

HTML

<canvas id="layer1" width="600" height="640" style="border:5px solid #ffffff; position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
<canvas id="layer2" width="600" height="640" style="border:5px solid #ffffff; position: absolute; left: 0; top: 0; z-index: 1;"></canvas>

JavaScript

//By Elijah Cirioli

//setup canvases
var canvas = document.getElementById("layer1");
var context = canvas.getContext("2d");
var flashLayer = document.getElementById("layer2");
var fl = flashLayer.getContext("2d");

//game information
var playing = false; //is the game actively being run
var squareSize = 32; //how many pixels across is a signal tile
var screenBounds = [132, -96]; //the corners of the playfield
var currentPiece = []; //the current tetrimino falling
var holdPiece = []; //the current tetrimino being held
var board = []; //the game board
var x = 0; //the x coordinate of the current tetrimino
var y = 0; //the y coordinate of the current tetrimino
var rotation = 0; //the rotation of the current tetrimino
var randomBag = []; //the bag of possible random tetriminoes
var queue = []; //the next three tetriminoes to fall

//scoring and levels
var score = 0; //the player's current score
var highscore = ["", 0]; //the highest score the player has achieved locally
var totalLines = 0; //the total number of lines cleared in a game
var time; //how long the game lasted
var tempTime = 0; //for tracking time when pausing
var level = 1; //game difficulty
var multiplier = 1.0; //score multipier based on level
var linesRemaining = 10; //lines to get to next level
var levelLines = [0, 10, 15, 20, 20, 30, 30, 40, 40, 50, 60, 70, 80, 90, 100, 100, -1]; //the number of lines needed to clear each level
var speeds = [0, 1000, 940, 880, 820, 760, 700, 640, 580, 520, 460, 400, 340, 280, 220, 160, 100]; //how fast tetriminoes fall

//controlling consecutive actions
var lastTetris = false; //was the last score a tetris?
var tSpin = false; //is the player currently perfoming a t-spin
var rotated = false; //have the player rotated without releasing the key
var held = false; //have they held the current piece
var pressedDown = false; //is the player holding the down arrow
var pressedUp = false; //is the player holding the up arrow
var pressedR = false; //is the player holding the...