/**** * Classes ****/ var Mine = Container.expand(function () { var self = Container.call(this); var mineGraphics = self.attachAsset('mine', { anchorX: 0.5, anchorY: 0.5 }); }); var NumberTile = Container.expand(function (number) { var self = Container.call(this); var tileType = number === 0 ? 'number_0' : 'number_other'; var numberGraphics = self.createAsset(tileType, { anchorX: 0.5, anchorY: 0.5 }); numberGraphics.alpha = number === 0 ? 0 : .5; if (number >= 1) { var dangerColors = [0x77dd77, 0x779ecb, 0xfff79a, 0xcb99c9, 0xaec6cf, 0xff6961, 0xb19cd9, 0xfdfd96]; var label = new Text2(number.toString(), { size: 120, fill: '#ffffff', align: 'center', weight: 800, stroke: '#000000', strokeThickness: 16 }); label.anchor.set(0.5); label.tint = dangerColors[number - 1]; self.addChild(label); } }); var Cell = Container.expand(function () { var self = Container.call(this); self.gridX = 0; self.gridY = 0; self.setPosition = function (x, y) { self.gridX = x; self.gridY = y; self.x = x * 180 + 90; self.y = y * 180 + 90; }; var cellGraphics = self.attachAsset('cell', { anchorX: 0.5, anchorY: 0.5 }); self.isMine = false; self.isRevealed = false; self.neighborMines = 0; self.reveal = function () { if (self.isRevealed) { return; } self.isRevealed = true; if (self.isMine) { cellGraphics.visible = false; var mine = self.addChild(new Mine()); } else { cellGraphics.visible = false; var number = self.addChild(new NumberTile(self.neighborMines)); self.addChild(number); self.parent.parent.score++; LK.setScore(self.parent.parent.score); self.parent.parent.scoreTxt.setText(LK.getScore().toString()); } }; self.placeMine = function () { self.isMine = true; }; self.calculateNeighborMines = function (getNeighbor) { self.neighborMines = 0; var neighbors = getNeighbor(self.gridX, self.gridY); for (var i = 0; i < neighbors.length; i++) { if (neighbors[i].isMine) { self.neighborMines++; } } }; }); var Board = Container.expand(function () { var self = Container.call(this); self.resetBoard = function () { for (var x = 0; x < self.cells.length; x++) { for (var y = 0; y < self.cells[x].length; y++) { self.cells[x][y].destroy(); } } self.cells = []; self.generateBoard(10, 10); self.placeMines(5 + self.resetCount); self.calculateAllNeighborMines(); self.resetCount++; var totalWidth = self.cells.length * 180; var totalHeight = self.cells[0].length * 180; self.x = (2048 - totalWidth) / 2; self.y = (2732 - totalHeight) / 2; var levelText = new Text2('Level ' + self.resetCount, { size: 225, fill: '#ffffff', align: 'center', weight: 800, stroke: '#000000', strokeThickness: 16 }); levelText.anchor.set(0.5); levelText.x = totalWidth / 2; levelText.y = totalHeight / 2; self.addChild(levelText); LK.setTimeout(function () { levelText.destroy(); }, 1000); }; self.getNeighbors = function (x, y) { var neighbors = []; for (var dx = -1; dx <= 1; dx++) { for (var dy = -1; dy <= 1; dy++) { if (dx === 0 && dy === 0) { continue; } var nx = x + dx, ny = y + dy; if (nx >= 0 && nx < self.cells.length && ny >= 0 && ny < self.cells[nx].length) { neighbors.push(self.cells[nx][ny]); } } } return neighbors; }; self.revealCellAndNeighbors = function (x, y) { if (x < 0 || x >= self.cells.length || y < 0 || y >= self.cells[x].length) { return; } var cell = self.cells[x][y]; if (cell && !cell.isRevealed) { cell.reveal(); if (cell.neighborMines === 0) { var neighbors = self.getNeighbors(x, y); for (var i = 0; i < neighbors.length; i++) { var neighbor = neighbors[i]; if (Math.abs(neighbor.gridX - x) + Math.abs(neighbor.gridY - y) === 1) { self.revealCellAndNeighbors(neighbor.gridX, neighbor.gridY); } } } } }; self.on('down', function (obj) { var pos = obj.event.getLocalPosition(self); var cellX = Math.floor(pos.x / 180); var cellY = Math.floor(pos.y / 180); self.onCellClicked(cellX, cellY); }); self.onCellClicked = function (cellX, cellY) { if (cellX >= 0 && cellX < self.cells.length && cellY >= 0 && cellY < self.cells[cellX].length) { var cell = self.cells[cellX][cellY]; } if (cell && !cell.isRevealed) { if (cell.isMine && !self.firstClick) { self.firstClick = true; cell.isMine = false; var randomX, randomY, randomCell; do { randomX = Math.floor(Math.random() * self.cells.length); randomY = Math.floor(Math.random() * self.cells[randomX].length); randomCell = self.cells[randomX][randomY]; } while (randomCell.isMine); randomCell.placeMine(); self.calculateAllNeighborMines(); } self.revealCellAndNeighbors(cellX, cellY); self.firstClick = true; if (cell.isMine) { gameOver = true; self.revealAllMines(); LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } else { var unrevealedCells = 0; var mineCells = 0; for (var x = 0; x < self.cells.length; x++) { for (var y = 0; y < self.cells[x].length; y++) { if (!self.cells[x][y].isRevealed) { unrevealedCells++; if (self.cells[x][y].isMine) { mineCells++; } } } } if (unrevealedCells === mineCells) { LK.effects.flashScreen(0x00ff00, 1000); self.resetBoard(); } } } }; self.revealAllMines = function () { for (var x = 0; x < self.cells.length; x++) { for (var y = 0; y < self.cells[x].length; y++) { var cell = self.cells[x][y]; if (cell.isMine) { cell.reveal(); } } } }; var boardGraphics = self.attachAsset('board', {}); boardGraphics.x = -28; boardGraphics.y = -28; self.cells = []; self.firstClick = false; self.firstClick = false; self.resetCount = 0; self.generateBoard = function (width, height) { for (var x = 0; x < width; x++) { self.cells[x] = []; for (var y = 0; y < height; y++) { var cell = new Cell(); self.addChild(cell); self.cells[x][y] = cell; cell.setPosition(x, y); } } }; self.revealCell = function (x, y) { if (x < 0 || x >= self.cells.length || y < 0 || y >= self.cells[x].length) { return; } var cell = self.cells[x][y]; if (!cell.isRevealed) { cell.reveal(); return cell.isMine; } }; self.placeMines = function (mineCount) { var placedMines = 0; while (placedMines < mineCount) { var x = Math.floor(Math.random() * self.cells.length); var y = Math.floor(Math.random() * self.cells[x].length); if (!self.cells[x][y].isMine) { self.cells[x][y].placeMine(); placedMines++; } } }; self.calculateAllNeighborMines = function () { var getNeighbor = function getNeighbor(x, y) { var neighbors = []; for (var dx = -1; dx <= 1; dx++) { for (var dy = -1; dy <= 1; dy++) { if (dx === 0 && dy === 0) { continue; } var nx = x + dx, ny = y + dy; if (nx >= 0 && nx < self.cells.length && ny >= 0 && ny < self.cells[nx].length) { neighbors.push(self.cells[nx][ny]); } } } return neighbors; }; for (var x = 0; x < self.cells.length; x++) { for (var y = 0; y < self.cells[x].length; y++) { self.cells[x][y].calculateNeighborMines(getNeighbor); } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var background = game.attachAsset('background', { anchorX: 0.5, anchorY: 0.5 }); background.alpha = 0.5; background.x = 2048 / 2; background.y = 2732 / 2; game.addChild(background); game.setBackgroundColor(0x89a6bb); game.score = 0; game.scoreTxt = new Text2(game.score.toString(), { size: 220, fill: '#ffffff', weight: 800, dropShadow: true, dropShadowColor: '#000000', dropShadowBlur: 4, dropShadowAngle: Math.PI / 6, dropShadowDistance: 6 }); game.scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(game.scoreTxt); var board = game.addChild(new Board()); var gameOver = false; board.resetBoard();
/****
* Classes
****/
var Mine = Container.expand(function () {
var self = Container.call(this);
var mineGraphics = self.attachAsset('mine', {
anchorX: 0.5,
anchorY: 0.5
});
});
var NumberTile = Container.expand(function (number) {
var self = Container.call(this);
var tileType = number === 0 ? 'number_0' : 'number_other';
var numberGraphics = self.createAsset(tileType, {
anchorX: 0.5,
anchorY: 0.5
});
numberGraphics.alpha = number === 0 ? 0 : .5;
if (number >= 1) {
var dangerColors = [0x77dd77, 0x779ecb, 0xfff79a, 0xcb99c9, 0xaec6cf, 0xff6961, 0xb19cd9, 0xfdfd96];
var label = new Text2(number.toString(), {
size: 120,
fill: '#ffffff',
align: 'center',
weight: 800,
stroke: '#000000',
strokeThickness: 16
});
label.anchor.set(0.5);
label.tint = dangerColors[number - 1];
self.addChild(label);
}
});
var Cell = Container.expand(function () {
var self = Container.call(this);
self.gridX = 0;
self.gridY = 0;
self.setPosition = function (x, y) {
self.gridX = x;
self.gridY = y;
self.x = x * 180 + 90;
self.y = y * 180 + 90;
};
var cellGraphics = self.attachAsset('cell', {
anchorX: 0.5,
anchorY: 0.5
});
self.isMine = false;
self.isRevealed = false;
self.neighborMines = 0;
self.reveal = function () {
if (self.isRevealed) {
return;
}
self.isRevealed = true;
if (self.isMine) {
cellGraphics.visible = false;
var mine = self.addChild(new Mine());
} else {
cellGraphics.visible = false;
var number = self.addChild(new NumberTile(self.neighborMines));
self.addChild(number);
self.parent.parent.score++;
LK.setScore(self.parent.parent.score);
self.parent.parent.scoreTxt.setText(LK.getScore().toString());
}
};
self.placeMine = function () {
self.isMine = true;
};
self.calculateNeighborMines = function (getNeighbor) {
self.neighborMines = 0;
var neighbors = getNeighbor(self.gridX, self.gridY);
for (var i = 0; i < neighbors.length; i++) {
if (neighbors[i].isMine) {
self.neighborMines++;
}
}
};
});
var Board = Container.expand(function () {
var self = Container.call(this);
self.resetBoard = function () {
for (var x = 0; x < self.cells.length; x++) {
for (var y = 0; y < self.cells[x].length; y++) {
self.cells[x][y].destroy();
}
}
self.cells = [];
self.generateBoard(10, 10);
self.placeMines(5 + self.resetCount);
self.calculateAllNeighborMines();
self.resetCount++;
var totalWidth = self.cells.length * 180;
var totalHeight = self.cells[0].length * 180;
self.x = (2048 - totalWidth) / 2;
self.y = (2732 - totalHeight) / 2;
var levelText = new Text2('Level ' + self.resetCount, {
size: 225,
fill: '#ffffff',
align: 'center',
weight: 800,
stroke: '#000000',
strokeThickness: 16
});
levelText.anchor.set(0.5);
levelText.x = totalWidth / 2;
levelText.y = totalHeight / 2;
self.addChild(levelText);
LK.setTimeout(function () {
levelText.destroy();
}, 1000);
};
self.getNeighbors = function (x, y) {
var neighbors = [];
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if (dx === 0 && dy === 0) {
continue;
}
var nx = x + dx,
ny = y + dy;
if (nx >= 0 && nx < self.cells.length && ny >= 0 && ny < self.cells[nx].length) {
neighbors.push(self.cells[nx][ny]);
}
}
}
return neighbors;
};
self.revealCellAndNeighbors = function (x, y) {
if (x < 0 || x >= self.cells.length || y < 0 || y >= self.cells[x].length) {
return;
}
var cell = self.cells[x][y];
if (cell && !cell.isRevealed) {
cell.reveal();
if (cell.neighborMines === 0) {
var neighbors = self.getNeighbors(x, y);
for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i];
if (Math.abs(neighbor.gridX - x) + Math.abs(neighbor.gridY - y) === 1) {
self.revealCellAndNeighbors(neighbor.gridX, neighbor.gridY);
}
}
}
}
};
self.on('down', function (obj) {
var pos = obj.event.getLocalPosition(self);
var cellX = Math.floor(pos.x / 180);
var cellY = Math.floor(pos.y / 180);
self.onCellClicked(cellX, cellY);
});
self.onCellClicked = function (cellX, cellY) {
if (cellX >= 0 && cellX < self.cells.length && cellY >= 0 && cellY < self.cells[cellX].length) {
var cell = self.cells[cellX][cellY];
}
if (cell && !cell.isRevealed) {
if (cell.isMine && !self.firstClick) {
self.firstClick = true;
cell.isMine = false;
var randomX, randomY, randomCell;
do {
randomX = Math.floor(Math.random() * self.cells.length);
randomY = Math.floor(Math.random() * self.cells[randomX].length);
randomCell = self.cells[randomX][randomY];
} while (randomCell.isMine);
randomCell.placeMine();
self.calculateAllNeighborMines();
}
self.revealCellAndNeighbors(cellX, cellY);
self.firstClick = true;
if (cell.isMine) {
gameOver = true;
self.revealAllMines();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else {
var unrevealedCells = 0;
var mineCells = 0;
for (var x = 0; x < self.cells.length; x++) {
for (var y = 0; y < self.cells[x].length; y++) {
if (!self.cells[x][y].isRevealed) {
unrevealedCells++;
if (self.cells[x][y].isMine) {
mineCells++;
}
}
}
}
if (unrevealedCells === mineCells) {
LK.effects.flashScreen(0x00ff00, 1000);
self.resetBoard();
}
}
}
};
self.revealAllMines = function () {
for (var x = 0; x < self.cells.length; x++) {
for (var y = 0; y < self.cells[x].length; y++) {
var cell = self.cells[x][y];
if (cell.isMine) {
cell.reveal();
}
}
}
};
var boardGraphics = self.attachAsset('board', {});
boardGraphics.x = -28;
boardGraphics.y = -28;
self.cells = [];
self.firstClick = false;
self.firstClick = false;
self.resetCount = 0;
self.generateBoard = function (width, height) {
for (var x = 0; x < width; x++) {
self.cells[x] = [];
for (var y = 0; y < height; y++) {
var cell = new Cell();
self.addChild(cell);
self.cells[x][y] = cell;
cell.setPosition(x, y);
}
}
};
self.revealCell = function (x, y) {
if (x < 0 || x >= self.cells.length || y < 0 || y >= self.cells[x].length) {
return;
}
var cell = self.cells[x][y];
if (!cell.isRevealed) {
cell.reveal();
return cell.isMine;
}
};
self.placeMines = function (mineCount) {
var placedMines = 0;
while (placedMines < mineCount) {
var x = Math.floor(Math.random() * self.cells.length);
var y = Math.floor(Math.random() * self.cells[x].length);
if (!self.cells[x][y].isMine) {
self.cells[x][y].placeMine();
placedMines++;
}
}
};
self.calculateAllNeighborMines = function () {
var getNeighbor = function getNeighbor(x, y) {
var neighbors = [];
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if (dx === 0 && dy === 0) {
continue;
}
var nx = x + dx,
ny = y + dy;
if (nx >= 0 && nx < self.cells.length && ny >= 0 && ny < self.cells[nx].length) {
neighbors.push(self.cells[nx][ny]);
}
}
}
return neighbors;
};
for (var x = 0; x < self.cells.length; x++) {
for (var y = 0; y < self.cells[x].length; y++) {
self.cells[x][y].calculateNeighborMines(getNeighbor);
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
background.alpha = 0.5;
background.x = 2048 / 2;
background.y = 2732 / 2;
game.addChild(background);
game.setBackgroundColor(0x89a6bb);
game.score = 0;
game.scoreTxt = new Text2(game.score.toString(), {
size: 220,
fill: '#ffffff',
weight: 800,
dropShadow: true,
dropShadowColor: '#000000',
dropShadowBlur: 4,
dropShadowAngle: Math.PI / 6,
dropShadowDistance: 6
});
game.scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(game.scoreTxt);
var board = game.addChild(new Board());
var gameOver = false;
board.resetBoard();
Sea mine. Minesweeper. Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Watery background. 10x10 Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Single white square. Round corners. Vector. Simple details. Flat shaded. No shadows. No outlines.