VCHS Computer Science
Computer Science at Valley Catholic High School
Pages
Home
APCSA
APCSP
Ten Commandments
Jump
Tetris Part 1
Start
Source Code
<!-- CSS --> <style> #grid { background-color: black; position: relative; border-right: solid 1px black; border-bottom: solid 1px black; } .block { position: absolute; border: solid 1px silver; } </style> <!-- HTML --> <div id="grid" style="height: 400px; width: 250px;"></div> <p><button id="start">Start</button></p> <!-- JavaScript --> <script> (function IIFE() { // Immediately Invoked Function Expression "use strict"; // Opt into strict mode. (Opt out of sloppy mode.) /*** GLOBAL VARIABLE ***/ let currentShape; /*** GLOBAL CONSTANTS ***/ // Unit Constants const unitConst = Object.freeze({ units: "px", qtyPer: 25 }); // Block Constants const blockConst = { unitsWide: unitConst.qtyPer, unitsTall: unitConst.qtyPer, length: unitConst.qtyPer + unitConst.units }; blockConst.width = blockConst.length; blockConst.height = blockConst.length; Object.freeze(blockConst); // Grid Constants const gridConst = { blocksWide: 10, blocksTall: 16 }; gridConst.width = (gridConst.blocksWide * blockConst.unitsWide) + unitConst.units; gridConst.height = (gridConst.blocksTall * blockConst.unitsTall) + unitConst.units; Object.freeze(gridConst); // Element Constants const elemConst = Object.freeze({ grid: getSizedGrid(), start: getStartButton() }); // Shape Constants const shapeConst = Object.freeze({ LColor: "orange", JColor: "orange", SColor: "blue", ZColor: "blue" }); /*** FUNCTIONS ***/ function getStartButton() { const element = document.getElementById("start"); element.addEventListener("click", startGame); return element; } /** Gets, sets the size of, and returns the grid element. */ function getSizedGrid() { const grid = document.getElementById("grid"); grid.style.height = gridConst.height; grid.style.width = gridConst.width; return grid; } /** Constructs a single block object. */ function Block(options) { // Initialize the new block. const thisObject = this; const element = createBlockElement(); let currentLeft = options.left; let currentTop = options.top; updatePosition(); /* Private Functions */ function updateLeft() { element.style.left = (currentLeft * blockConst.unitsWide) + unitConst.units; } function updateTop() { element.style.top = (currentTop * blockConst.unitsTall) + unitConst.units; } function updatePosition() { updateLeft(); updateTop(); } function createBlockElement() { const block = document.createElement("div"); block.classList.add("block"); block.style.height = blockConst.height; block.style.width = blockConst.width; block.style.backgroundColor = options.bgColor; return block; } function canMoveLeft() { return currentLeft > 0; } function canMoveRight() { return currentLeft < gridConst.blocksWide - 1; } function canMoveDown() { return currentTop < gridConst.blocksTall - 1; } function moveLeft() { --currentLeft; updateLeft(); } function moveRight() { ++currentLeft; updateLeft(); } function moveDown() { ++currentTop; updateTop(); } /* Public Methods */ this.addTo = function(otherElement) { otherElement.append(element); return thisObject; }; this.moveLeft = function() { if (canMoveLeft()) moveLeft(); return thisObject; }; this.moveRight = function() { if (canMoveRight()) moveRight(); return thisObject; }; this.moveDown = function() { if (canMoveDown()) moveDown(); return thisObject; }; this.canMoveRight = canMoveRight; this.canMoveLeft = canMoveLeft; this.canMoveDown = canMoveDown; } function onKeyDown(event) { const key = event.key; if (key === "ArrowLeft") { currentShape.moveLeft(); event.preventDefault(); } else if (key === "ArrowRight") { currentShape.moveRight(); event.preventDefault(); } else if (key === "ArrowDown") { currentShape.moveDown(); event.preventDefault(); } else if (key === "ArrowUp") { event.preventDefault(); } } /** Base class for a shape object consisting of blocks. */ function Shape(blocks) { const thisObject = this; const numBlocks = blocks.length; blocks.forEach(function(block) { block.addTo(elemConst.grid); }); function canMoveRight() { for (let i = 0; i < numBlocks; ++i) { if (!blocks[i].canMoveRight()) return false; } return true; } function canMoveLeft() { for (let i = 0; i < numBlocks; ++i) { if (!blocks[i].canMoveLeft()) return false; } return true; } function canMoveDown() { for (let i = 0; i < numBlocks; ++i) { if (!blocks[i].canMoveDown()) return false; } return true; } function moveDown() { for (let i = 0; i < numBlocks; ++i) blocks[i].moveDown(); } function moveLeft() { for (let i = 0; i < numBlocks; ++i) blocks[i].moveLeft(); } function moveRight() { for (let i = 0; i < numBlocks; ++i) blocks[i].moveRight(); } this.moveDown = function() { if (canMoveDown()) moveDown(); return thisObject; }; this.moveLeft = function() { if (canMoveLeft()) moveLeft(); return thisObject; }; this.moveRight = function() { if (canMoveRight()) moveRight(); return thisObject; }; this.canMoveRight = canMoveRight; this.canMoveLeft = canMoveLeft; this.canMoveDown = canMoveDown; } /* Shape Subclasses */ function LShape() { Shape.call(this, getBlocks()); const thisObject = this; function getBlocks() { const color = shapeConst.LColor; const blocks = []; blocks.push(new Block({bgColor: color, left: 3, top: 1})); blocks.push(new Block({bgColor: color, left: 4, top: 1})); blocks.push(new Block({bgColor: color, left: 5, top: 1})); blocks.push(new Block({bgColor: color, left: 5, top: 0})); return blocks; } } function JShape() { } function SShape() { } function ZShape() { } function TShape() { } function OShape() { } function IShape() { } function getNextShape() { return new LShape(); } function startGame() { currentShape = getNextShape(); } /*** MAIN ***/ window.addEventListener("keydown", onKeyDown); })(); </script>
Newer Post
Older Post
Home