Implement Gameplay Functionality
Requirements:
- Proficiency with JavaScript loops and conditions.
- Know-how to manipulate the DOM using JavaScript.
Functionality:
- Craft the core game dynamics.
- Design game-over scenarios.
- Implement mechanics for block creation and movement.
Steps:
- Check whether the player has clicked on a black or white block. Update game status based on the player's click.
// Determine whether to click on a black or white block
function judge(ev) {
if (
ev.target.className.indexOf("black") == -1 &&
ev.target.className.indexOf("cell") !== -1
) {
ev.target.parentNode.pass1 = 1; // Defines the pass attribute, which indicates that the white block of this row has been clicked
}
if (ev.target.className.indexOf("black") !== -1) {
// Clicking a target with black in the class name is a black block
ev.target.className = "cell";
ev.target.parentNode.pass = 1; // Defines the pass attribute, indicating the black block in this row was clicked
score();
}
}
- Determine when the game is over based on player interactions. Trigger game-ending actions when the game is over.
// Check if the game is over
function over() {
var rows = con.childNodes;
if (rows.length == 5 && rows[rows.length - 1].pass !== 1) {
fail();
}
for (let i = 0; i < rows.length; i++) {
if (rows[i].pass1 == 1) {
fail();
}
}
}
// Game Over
function fail() {
clearInterval(clock);
flag = false;
confirm("The final score is " + parseInt($("score").innerHTML));
var con = $("con");
con.innerHTML = "";
$("score").innerHTML = 0;
con.style.top = "-408px";
}
- Generate rows of blocks for gameplay. Randomly position the "black" block amongst others.
// Create a <div class="row"> with four children <div class="cell">
function createrow() {
var con = $("con");
var row = creatediv("row"); // Create div: className=row
var arr = creatcell(); // Define the class name of the div cell, one of which is cell black
con.appendChild(row); // Add child node with row as con
for (var i = 0; i < 4; i++) {
row.appendChild(creatediv(arr[i])); // Add child nodes of row cell
}
if (con.firstChild == null) {
con.appendChild(row);
} else {
con.insertBefore(row, con.firstChild);
}
}
// Create an array of class names; one of them is "cell black", others are "cell"
function creatcell() {
var temp = ["cell", "cell", "cell", "cell"];
var i = Math.floor(Math.random() * 4); // Randomly generate the position of the black block Math.random() function parameters 0~1 random number
temp[i] = "cell black";
return temp;
}
- Make blocks move downwards continuously. Delete old rows as they move out of view.
// Move the black blocks down
function move() {
var con = $("con");
var top = parseInt(window.getComputedStyle(con, null)["top"]);
if (speed + top > 0) {
top = 0;
} else {
top += speed;
}
con.style.top = top + "px"; // Keep moving the top value so it moves
over();
if (top == 0) {
createrow();
con.style.top = "-102px";
delrow();
}
}
// Delete a row
function delrow() {
var con = $("con");
if (con.childNodes.length == 6) {
con.removeChild(con.lastChild);
}
}