/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Class for individual letter tiles var LetterTile = Container.expand(function () { var self = Container.call(this); var tileGraphics = self.attachAsset('tile', { anchorX: 0.5, anchorY: 0.5 }); self.letter = ''; self.setLetter = function (_char) { self.letter = _char; self.text.setText(_char); }; self.text = new Text2('', { size: 100, fill: "#ffffff" }); self.text.anchor.set(0.5, 0.5); self.addChild(self.text); }); // Class for the word puzzle board var WordPuzzleBoard = Container.expand(function () { var self = Container.call(this); self.tiles = []; self.createBoard = function (rows, cols) { for (var i = 0; i < rows; i++) { self.tiles[i] = []; for (var j = 0; j < cols; j++) { var tile = new LetterTile(); tile.x = j * 120; tile.y = i * 120; self.tiles[i][j] = tile; self.addChild(tile); } } }; self.setWord = function (word) { for (var i = 0; i < word.length; i++) { var row = Math.floor(i / self.tiles[0].length); var col = i % self.tiles[0].length; self.tiles[row][col].setLetter(word[i]); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize the word puzzle board var puzzleBoard = new WordPuzzleBoard(); puzzleBoard.createBoard(5, 5); // Create a 5x5 board puzzleBoard.x = (2048 - 5 * 120) / 2; // Center the board horizontally puzzleBoard.y = (2732 - 5 * 120) / 2; // Center the board vertically game.addChild(puzzleBoard); // Set a word on the board puzzleBoard.setWord("HELLO"); // Handle touch events to select tiles var selectedTiles = []; game.down = function (x, y, obj) { var localPos = game.toLocal(obj.global); for (var i = 0; i < puzzleBoard.tiles.length; i++) { for (var j = 0; j < puzzleBoard.tiles[i].length; j++) { var tile = puzzleBoard.tiles[i][j]; if (tile.getBounds().contains(localPos.x, localPos.y)) { selectedTiles.push(tile); tile.alpha = 0.5; // Highlight selected tile } } } }; game.up = function (x, y, obj) { // Check if selected tiles form a valid word var formedWord = selectedTiles.map(function (tile) { return tile.letter; }).join(''); if (isValidWord(formedWord)) { LK.setScore(LK.getScore() + formedWord.length); } // Reset selected tiles selectedTiles.forEach(function (tile) { return tile.alpha = 1; }); selectedTiles = []; }; // Function to check if a word is valid function isValidWord(word) { // Placeholder for word validation logic return word === "HELLO"; } // Update score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.setText(LK.getScore()); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); game.update = function () { scoreTxt.setText(LK.getScore()); };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Class for individual letter tiles
var LetterTile = Container.expand(function () {
var self = Container.call(this);
var tileGraphics = self.attachAsset('tile', {
anchorX: 0.5,
anchorY: 0.5
});
self.letter = '';
self.setLetter = function (_char) {
self.letter = _char;
self.text.setText(_char);
};
self.text = new Text2('', {
size: 100,
fill: "#ffffff"
});
self.text.anchor.set(0.5, 0.5);
self.addChild(self.text);
});
// Class for the word puzzle board
var WordPuzzleBoard = Container.expand(function () {
var self = Container.call(this);
self.tiles = [];
self.createBoard = function (rows, cols) {
for (var i = 0; i < rows; i++) {
self.tiles[i] = [];
for (var j = 0; j < cols; j++) {
var tile = new LetterTile();
tile.x = j * 120;
tile.y = i * 120;
self.tiles[i][j] = tile;
self.addChild(tile);
}
}
};
self.setWord = function (word) {
for (var i = 0; i < word.length; i++) {
var row = Math.floor(i / self.tiles[0].length);
var col = i % self.tiles[0].length;
self.tiles[row][col].setLetter(word[i]);
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize the word puzzle board
var puzzleBoard = new WordPuzzleBoard();
puzzleBoard.createBoard(5, 5); // Create a 5x5 board
puzzleBoard.x = (2048 - 5 * 120) / 2; // Center the board horizontally
puzzleBoard.y = (2732 - 5 * 120) / 2; // Center the board vertically
game.addChild(puzzleBoard);
// Set a word on the board
puzzleBoard.setWord("HELLO");
// Handle touch events to select tiles
var selectedTiles = [];
game.down = function (x, y, obj) {
var localPos = game.toLocal(obj.global);
for (var i = 0; i < puzzleBoard.tiles.length; i++) {
for (var j = 0; j < puzzleBoard.tiles[i].length; j++) {
var tile = puzzleBoard.tiles[i][j];
if (tile.getBounds().contains(localPos.x, localPos.y)) {
selectedTiles.push(tile);
tile.alpha = 0.5; // Highlight selected tile
}
}
}
};
game.up = function (x, y, obj) {
// Check if selected tiles form a valid word
var formedWord = selectedTiles.map(function (tile) {
return tile.letter;
}).join('');
if (isValidWord(formedWord)) {
LK.setScore(LK.getScore() + formedWord.length);
}
// Reset selected tiles
selectedTiles.forEach(function (tile) {
return tile.alpha = 1;
});
selectedTiles = [];
};
// Function to check if a word is valid
function isValidWord(word) {
// Placeholder for word validation logic
return word === "HELLO";
}
// Update score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.update = function () {
scoreTxt.setText(LK.getScore());
};