/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Heart = Container.expand(function () { var self = Container.call(this); var heartGraphic = self.attachAsset('heart', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var LanguageButton = Container.expand(function () { var self = Container.call(this); // Create button background var buttonBg = self.attachAsset('wordBubble', { anchorX: 0.5, anchorY: 0.5 }); buttonBg.width = 300; buttonBg.height = 90; // Create text for the button self.buttonText = new Text2('Español', { size: 48, fill: 0xFFFFFF }); self.buttonText.anchor.set(0.5, 0.5); self.addChild(self.buttonText); self.setLanguage = function (lang) { if (lang === 'es') { self.buttonText.setText('Español'); } else if (lang === 'en') { self.buttonText.setText('English'); } else if (lang === 'fr') { self.buttonText.setText('Français'); } }; // Touch handler for language cycling self.down = function (x, y, obj) { // Cycle through languages if (currentLanguage === 'es') { currentLanguage = 'en'; } else if (currentLanguage === 'en') { currentLanguage = 'fr'; } else { currentLanguage = 'es'; } // Save language preference storage.selectedLanguage = currentLanguage; // Update button text self.setLanguage(currentLanguage); // Update word categories updateWordCategories(); // Visual feedback LK.effects.flashObject(self, 0xFFEB3B, 300); }; return self; }); var MisspelledWord = Container.expand(function () { var self = Container.call(this); // Create word bubble background var bubble = self.attachAsset('wordBubble', { anchorX: 0.5, anchorY: 0.5 }); // Create text for the word self.wordText = new Text2('', { size: 72, fill: 0xFFFFFF }); self.wordText.anchor.set(0.5, 0.5); self.addChild(self.wordText); // Word properties self.fallSpeed = 2; self.word = ''; self.missed = false; // Set the word text self.setWord = function (word) { self.word = word; self.wordText.setText(word); }; // Update method called every frame self.update = function () { self.y += self.fallSpeed; }; // Touch handler - touching misspelled word reduces life self.down = function (x, y, obj) { if (!self.missed) { // Misspelled word was touched - lose a life loseLife(); // Visual effect LK.effects.flashObject(self, 0xFF1744, 300); tween(self, { scaleX: 1.5, scaleY: 1.5, alpha: 0 }, { duration: 300, onFinish: function onFinish() { self.destroy(); } }); // Remove from words array for (var i = words.length - 1; i >= 0; i--) { if (words[i] === self) { words.splice(i, 1); break; } } } }; return self; }); var MusicToggleButton = Container.expand(function () { var self = Container.call(this); // Create button background var buttonBg = self.attachAsset('wordBubble', { anchorX: 0.5, anchorY: 0.5 }); buttonBg.width = 300; buttonBg.height = 90; // Create text for the button self.buttonText = new Text2('Música ON', { size: 48, fill: 0xFFFFFF }); self.buttonText.anchor.set(0.5, 0.5); self.addChild(self.buttonText); // Update button text based on music state self.updateText = function () { if (musicEnabled) { self.buttonText.setText('Música ON'); } else { self.buttonText.setText('Música OFF'); } }; // Touch handler for music toggle self.down = function (x, y, obj) { // Toggle music state musicEnabled = !musicEnabled; // Save music preference storage.musicEnabled = musicEnabled; // Update button text self.updateText(); // Control music playback if (musicEnabled) { LK.playMusic('backgroundMusic'); } else { LK.stopMusic(); } // Visual feedback LK.effects.flashObject(self, 0xFFEB3B, 300); }; return self; }); var Word = Container.expand(function () { var self = Container.call(this); // Create word bubble background var bubble = self.attachAsset('wordBubble', { anchorX: 0.5, anchorY: 0.5 }); // Create text for the word self.wordText = new Text2('', { size: 72, fill: 0xFFFFFF }); self.wordText.anchor.set(0.5, 0.5); self.addChild(self.wordText); // Word properties self.fallSpeed = 2; self.word = ''; self.missed = false; // Set the word text self.setWord = function (word) { self.word = word; self.wordText.setText(word); }; // Update method called every frame self.update = function () { self.y += self.fallSpeed; }; // Touch handler self.down = function (x, y, obj) { if (!self.missed) { // Word was touched correctly LK.setScore(LK.getScore() + 10); scoreText.setText(LK.getScore()); // Visual effect LK.effects.flashObject(self, 0xFFEB3B, 300); tween(self, { scaleX: 1.5, scaleY: 1.5, alpha: 0 }, { duration: 300, onFinish: function onFinish() { self.destroy(); } }); // Play sound if music is enabled if (musicEnabled) { LK.getSound('correctWord').play(); } // Remove from words array for (var i = words.length - 1; i >= 0; i--) { if (words[i] === self) { words.splice(i, 1); break; } } } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1976D2 }); /**** * Game Code ****/ // Game state management var gameState = 'playing'; // 'menu' or 'playing' // Game variables var words = []; var lives = 3; var hearts = []; var currentSpeed = 2; var wordSpawnTimer = 0; var spawnInterval = 120; // Spawn every 2 seconds at 60 FPS var backgroundColors = [0x1976D2, 0x7B1FA2, 0xD32F2F, 0xF57C00, 0x388E3C, 0x1976D2]; var currentColorIndex = 0; var lastSpeedLevel = 0; // Language system var currentLanguage = storage.selectedLanguage || 'es'; var languageButton; var musicToggleButton; var scoreText; var livesContainer; // Music system var musicEnabled = storage.musicEnabled !== undefined ? storage.musicEnabled : true; // Multi-language words by category var allLanguageWords = { es: { animales: ['gato', 'perro', 'pájaro', 'pez', 'león', 'tigre', 'oso', 'ratón'], colores: ['rojo', 'azul', 'verde', 'amarillo', 'naranja', 'morado', 'rosa', 'negro'], comida: ['manzana', 'pan', 'agua', 'leche', 'queso', 'pollo', 'arroz', 'pasta'], objetos: ['mesa', 'silla', 'libro', 'lápiz', 'coche', 'casa', 'puerta', 'ventana'] }, en: { animals: ['cat', 'dog', 'bird', 'fish', 'lion', 'tiger', 'bear', 'mouse'], colors: ['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'pink', 'black'], food: ['apple', 'bread', 'water', 'milk', 'cheese', 'chicken', 'rice', 'pasta'], objects: ['table', 'chair', 'book', 'pencil', 'car', 'house', 'door', 'window'] }, fr: { animaux: ['chat', 'chien', 'oiseau', 'poisson', 'lion', 'tigre', 'ours', 'souris'], couleurs: ['rouge', 'bleu', 'vert', 'jaune', 'orange', 'violet', 'rose', 'noir'], nourriture: ['pomme', 'pain', 'eau', 'lait', 'fromage', 'poulet', 'riz', 'pâtes'], objets: ['table', 'chaise', 'livre', 'crayon', 'voiture', 'maison', 'porte', 'fenêtre'] } }; // Misspelled words for each language var misspelledWords = { es: ['gto', 'prero', 'pájro', 'pz', 'leó', 'tigr', 'os', 'rató', 'rjo', 'azl', 'verd', 'amarilo'], en: ['ct', 'dg', 'brd', 'fsh', 'lin', 'tigr', 'ber', 'mous', 'rd', 'blu', 'gren', 'yelow'], fr: ['cht', 'chin', 'oisa', 'poisso', 'lin', 'tigr', 'ous', 'souri', 'roug', 'ble', 'vrt', 'jaun'] }; var wordCategories = {}; var allWords = []; var misspelledWordsArray = []; // Function to update word categories based on current language function updateWordCategories() { wordCategories = allLanguageWords[currentLanguage]; allWords = []; for (var category in wordCategories) { allWords = allWords.concat(wordCategories[category]); } misspelledWordsArray = misspelledWords[currentLanguage] || []; } // Initialize with current language updateWordCategories(); // Function to start the game function startGame() { gameState = 'playing'; // Reset game variables words = []; lives = 3; currentSpeed = 2; wordSpawnTimer = 0; currentColorIndex = 0; lastSpeedLevel = 0; LK.setScore(0); // Show game UI showGameUI(); // Reset hearts for (var i = 0; i < hearts.length; i++) { hearts[i].alpha = 1; } // Update score display if (scoreText) { scoreText.setText(LK.getScore()); } // Start spawning words immediately spawnWord(); // Play background music if enabled if (musicEnabled) { LK.playMusic('backgroundMusic'); } console.log("Game started, state:", gameState); } // Function to cycle language function cycleLanguage() { if (currentLanguage === 'es') { currentLanguage = 'en'; } else if (currentLanguage === 'en') { currentLanguage = 'fr'; } else { currentLanguage = 'es'; } // Save language preference storage.selectedLanguage = currentLanguage; // Update word categories updateWordCategories(); // Update language button if visible if (languageButton) { languageButton.setLanguage(currentLanguage); } } // Function to show game UI function showGameUI() { if (scoreText) { scoreText.visible = true; } if (livesContainer) { livesContainer.visible = true; } if (languageButton) { languageButton.visible = true; } if (musicToggleButton) { musicToggleButton.visible = true; } } // Function to hide game UI function hideGameUI() { if (scoreText) { scoreText.visible = false; } if (livesContainer) { livesContainer.visible = false; } if (languageButton) { languageButton.visible = false; } if (musicToggleButton) { musicToggleButton.visible = false; } } // Score display scoreText = new Text2('0', { size: 60, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); scoreText.y = 50; // Lives display - Create a container for hearts with better positioning livesContainer = new Container(); livesContainer.x = -200; // Offset to position hearts better in top right livesContainer.y = 50; LK.gui.topRight.addChild(livesContainer); // Add hearts to the lives container for (var i = 0; i < 3; i++) { var heart = new Heart(); heart.x = i * 80; heart.y = 0; hearts.push(heart); livesContainer.addChild(heart); } // Add "Lives:" label next to hearts var livesLabel = new Text2('Lives:', { size: 60, fill: 0xFFFFFF }); livesLabel.anchor.set(1, 0.5); livesLabel.x = -20; livesLabel.y = 0; livesContainer.addChild(livesLabel); // Language selection button languageButton = new LanguageButton(); languageButton.x = -150; languageButton.y = 150; languageButton.setLanguage(currentLanguage); LK.gui.topRight.addChild(languageButton); // Music toggle button musicToggleButton = new MusicToggleButton(); musicToggleButton.x = -150; musicToggleButton.y = 220; musicToggleButton.updateText(); LK.gui.topRight.addChild(musicToggleButton); // Start game immediately startGame(); // Function to spawn a new word function spawnWord() { var word; // 20% chance to spawn a misspelled word if (Math.random() < 0.2 && misspelledWordsArray.length > 0) { word = new MisspelledWord(); var randomMisspelledWord = misspelledWordsArray[Math.floor(Math.random() * misspelledWordsArray.length)]; word.setWord(randomMisspelledWord); } else { word = new Word(); var randomWord = allWords[Math.floor(Math.random() * allWords.length)]; word.setWord(randomWord); } // Random horizontal position word.x = Math.random() * (2048 - 300) + 150; word.y = -100; word.fallSpeed = currentSpeed; words.push(word); game.addChild(word); } // Function to lose a life function loseLife() { lives--; if (lives >= 0 && lives < hearts.length) { hearts[lives].alpha = 0.3; } if (lives <= 0) { // Save high score before game over var currentScore = LK.getScore(); var highScoreKey = 'highScore' + currentLanguage.charAt(0).toUpperCase() + currentLanguage.slice(1); var currentHighScore = storage[highScoreKey] || 0; if (currentScore > currentHighScore) { storage[highScoreKey] = currentScore; } // Show game over LK.showGameOver(); } // Play wrong sound if music is enabled if (musicEnabled) { LK.getSound('wrongWord').play(); } // Flash screen red LK.effects.flashScreen(0xFF1744, 500); } // Function to show high scores when game is paused function showHighScores() { // Get high scores for all languages var highScoreEs = storage.highScoreEs || 0; var highScoreEn = storage.highScoreEn || 0; var highScoreFr = storage.highScoreFr || 0; // Create high score display container var highScoreContainer = new Container(); highScoreContainer.x = -400; highScoreContainer.y = 300; LK.gui.topRight.addChild(highScoreContainer); // Title var titleText = new Text2('High Scores:', { size: 40, fill: 0xFFFFFF }); titleText.anchor.set(1, 0); titleText.x = -20; titleText.y = 0; highScoreContainer.addChild(titleText); // Spanish high score var esText = new Text2('Español: ' + highScoreEs, { size: 32, fill: 0xFFFFFF }); esText.anchor.set(1, 0); esText.x = -20; esText.y = 50; highScoreContainer.addChild(esText); // English high score var enText = new Text2('English: ' + highScoreEn, { size: 32, fill: 0xFFFFFF }); enText.anchor.set(1, 0); enText.x = -20; enText.y = 90; highScoreContainer.addChild(enText); // French high score var frText = new Text2('Français: ' + highScoreFr, { size: 32, fill: 0xFFFFFF }); frText.anchor.set(1, 0); frText.x = -20; frText.y = 130; highScoreContainer.addChild(frText); return highScoreContainer; } // Store reference to high score display var highScoreDisplay = null; // Game pause handler LK.on('pause', function () { if (!highScoreDisplay) { highScoreDisplay = showHighScores(); } }); // Game resume handler LK.on('resume', function () { if (highScoreDisplay) { highScoreDisplay.destroy(); highScoreDisplay = null; } }); // Game update loop game.update = function () { // Only update game logic when playing if (gameState !== 'playing') { return; } // Spawn new words wordSpawnTimer++; if (wordSpawnTimer >= spawnInterval) { spawnWord(); wordSpawnTimer = 0; } // Update and check words for (var i = words.length - 1; i >= 0; i--) { var word = words[i]; // Check if word reached bottom if (word.y > 2732 + 50) { if (!word.missed) { word.missed = true; // Only lose life if it's not a misspelled word if (!(word instanceof MisspelledWord)) { loseLife(); } } word.destroy(); words.splice(i, 1); } } // Increase speed every 20 points var speedLevel = Math.floor(LK.getScore() / 20); currentSpeed = 2 + speedLevel * 0.5; // Decrease spawn interval slightly as speed increases spawnInterval = Math.max(60, 120 - speedLevel * 5); // Change background color when speed level increases if (speedLevel !== lastSpeedLevel) { currentColorIndex = speedLevel % backgroundColors.length; var targetColor = backgroundColors[currentColorIndex]; // Simple immediate color change - tween doesn't work well with color interpolation in this context game.setBackgroundColor(targetColor); lastSpeedLevel = speedLevel; } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Heart = Container.expand(function () {
var self = Container.call(this);
var heartGraphic = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var LanguageButton = Container.expand(function () {
var self = Container.call(this);
// Create button background
var buttonBg = self.attachAsset('wordBubble', {
anchorX: 0.5,
anchorY: 0.5
});
buttonBg.width = 300;
buttonBg.height = 90;
// Create text for the button
self.buttonText = new Text2('Español', {
size: 48,
fill: 0xFFFFFF
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
self.setLanguage = function (lang) {
if (lang === 'es') {
self.buttonText.setText('Español');
} else if (lang === 'en') {
self.buttonText.setText('English');
} else if (lang === 'fr') {
self.buttonText.setText('Français');
}
};
// Touch handler for language cycling
self.down = function (x, y, obj) {
// Cycle through languages
if (currentLanguage === 'es') {
currentLanguage = 'en';
} else if (currentLanguage === 'en') {
currentLanguage = 'fr';
} else {
currentLanguage = 'es';
}
// Save language preference
storage.selectedLanguage = currentLanguage;
// Update button text
self.setLanguage(currentLanguage);
// Update word categories
updateWordCategories();
// Visual feedback
LK.effects.flashObject(self, 0xFFEB3B, 300);
};
return self;
});
var MisspelledWord = Container.expand(function () {
var self = Container.call(this);
// Create word bubble background
var bubble = self.attachAsset('wordBubble', {
anchorX: 0.5,
anchorY: 0.5
});
// Create text for the word
self.wordText = new Text2('', {
size: 72,
fill: 0xFFFFFF
});
self.wordText.anchor.set(0.5, 0.5);
self.addChild(self.wordText);
// Word properties
self.fallSpeed = 2;
self.word = '';
self.missed = false;
// Set the word text
self.setWord = function (word) {
self.word = word;
self.wordText.setText(word);
};
// Update method called every frame
self.update = function () {
self.y += self.fallSpeed;
};
// Touch handler - touching misspelled word reduces life
self.down = function (x, y, obj) {
if (!self.missed) {
// Misspelled word was touched - lose a life
loseLife();
// Visual effect
LK.effects.flashObject(self, 0xFF1744, 300);
tween(self, {
scaleX: 1.5,
scaleY: 1.5,
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
}
});
// Remove from words array
for (var i = words.length - 1; i >= 0; i--) {
if (words[i] === self) {
words.splice(i, 1);
break;
}
}
}
};
return self;
});
var MusicToggleButton = Container.expand(function () {
var self = Container.call(this);
// Create button background
var buttonBg = self.attachAsset('wordBubble', {
anchorX: 0.5,
anchorY: 0.5
});
buttonBg.width = 300;
buttonBg.height = 90;
// Create text for the button
self.buttonText = new Text2('Música ON', {
size: 48,
fill: 0xFFFFFF
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
// Update button text based on music state
self.updateText = function () {
if (musicEnabled) {
self.buttonText.setText('Música ON');
} else {
self.buttonText.setText('Música OFF');
}
};
// Touch handler for music toggle
self.down = function (x, y, obj) {
// Toggle music state
musicEnabled = !musicEnabled;
// Save music preference
storage.musicEnabled = musicEnabled;
// Update button text
self.updateText();
// Control music playback
if (musicEnabled) {
LK.playMusic('backgroundMusic');
} else {
LK.stopMusic();
}
// Visual feedback
LK.effects.flashObject(self, 0xFFEB3B, 300);
};
return self;
});
var Word = Container.expand(function () {
var self = Container.call(this);
// Create word bubble background
var bubble = self.attachAsset('wordBubble', {
anchorX: 0.5,
anchorY: 0.5
});
// Create text for the word
self.wordText = new Text2('', {
size: 72,
fill: 0xFFFFFF
});
self.wordText.anchor.set(0.5, 0.5);
self.addChild(self.wordText);
// Word properties
self.fallSpeed = 2;
self.word = '';
self.missed = false;
// Set the word text
self.setWord = function (word) {
self.word = word;
self.wordText.setText(word);
};
// Update method called every frame
self.update = function () {
self.y += self.fallSpeed;
};
// Touch handler
self.down = function (x, y, obj) {
if (!self.missed) {
// Word was touched correctly
LK.setScore(LK.getScore() + 10);
scoreText.setText(LK.getScore());
// Visual effect
LK.effects.flashObject(self, 0xFFEB3B, 300);
tween(self, {
scaleX: 1.5,
scaleY: 1.5,
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
}
});
// Play sound if music is enabled
if (musicEnabled) {
LK.getSound('correctWord').play();
}
// Remove from words array
for (var i = words.length - 1; i >= 0; i--) {
if (words[i] === self) {
words.splice(i, 1);
break;
}
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1976D2
});
/****
* Game Code
****/
// Game state management
var gameState = 'playing'; // 'menu' or 'playing'
// Game variables
var words = [];
var lives = 3;
var hearts = [];
var currentSpeed = 2;
var wordSpawnTimer = 0;
var spawnInterval = 120; // Spawn every 2 seconds at 60 FPS
var backgroundColors = [0x1976D2, 0x7B1FA2, 0xD32F2F, 0xF57C00, 0x388E3C, 0x1976D2];
var currentColorIndex = 0;
var lastSpeedLevel = 0;
// Language system
var currentLanguage = storage.selectedLanguage || 'es';
var languageButton;
var musicToggleButton;
var scoreText;
var livesContainer;
// Music system
var musicEnabled = storage.musicEnabled !== undefined ? storage.musicEnabled : true;
// Multi-language words by category
var allLanguageWords = {
es: {
animales: ['gato', 'perro', 'pájaro', 'pez', 'león', 'tigre', 'oso', 'ratón'],
colores: ['rojo', 'azul', 'verde', 'amarillo', 'naranja', 'morado', 'rosa', 'negro'],
comida: ['manzana', 'pan', 'agua', 'leche', 'queso', 'pollo', 'arroz', 'pasta'],
objetos: ['mesa', 'silla', 'libro', 'lápiz', 'coche', 'casa', 'puerta', 'ventana']
},
en: {
animals: ['cat', 'dog', 'bird', 'fish', 'lion', 'tiger', 'bear', 'mouse'],
colors: ['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'pink', 'black'],
food: ['apple', 'bread', 'water', 'milk', 'cheese', 'chicken', 'rice', 'pasta'],
objects: ['table', 'chair', 'book', 'pencil', 'car', 'house', 'door', 'window']
},
fr: {
animaux: ['chat', 'chien', 'oiseau', 'poisson', 'lion', 'tigre', 'ours', 'souris'],
couleurs: ['rouge', 'bleu', 'vert', 'jaune', 'orange', 'violet', 'rose', 'noir'],
nourriture: ['pomme', 'pain', 'eau', 'lait', 'fromage', 'poulet', 'riz', 'pâtes'],
objets: ['table', 'chaise', 'livre', 'crayon', 'voiture', 'maison', 'porte', 'fenêtre']
}
};
// Misspelled words for each language
var misspelledWords = {
es: ['gto', 'prero', 'pájro', 'pz', 'leó', 'tigr', 'os', 'rató', 'rjo', 'azl', 'verd', 'amarilo'],
en: ['ct', 'dg', 'brd', 'fsh', 'lin', 'tigr', 'ber', 'mous', 'rd', 'blu', 'gren', 'yelow'],
fr: ['cht', 'chin', 'oisa', 'poisso', 'lin', 'tigr', 'ous', 'souri', 'roug', 'ble', 'vrt', 'jaun']
};
var wordCategories = {};
var allWords = [];
var misspelledWordsArray = [];
// Function to update word categories based on current language
function updateWordCategories() {
wordCategories = allLanguageWords[currentLanguage];
allWords = [];
for (var category in wordCategories) {
allWords = allWords.concat(wordCategories[category]);
}
misspelledWordsArray = misspelledWords[currentLanguage] || [];
}
// Initialize with current language
updateWordCategories();
// Function to start the game
function startGame() {
gameState = 'playing';
// Reset game variables
words = [];
lives = 3;
currentSpeed = 2;
wordSpawnTimer = 0;
currentColorIndex = 0;
lastSpeedLevel = 0;
LK.setScore(0);
// Show game UI
showGameUI();
// Reset hearts
for (var i = 0; i < hearts.length; i++) {
hearts[i].alpha = 1;
}
// Update score display
if (scoreText) {
scoreText.setText(LK.getScore());
}
// Start spawning words immediately
spawnWord();
// Play background music if enabled
if (musicEnabled) {
LK.playMusic('backgroundMusic');
}
console.log("Game started, state:", gameState);
}
// Function to cycle language
function cycleLanguage() {
if (currentLanguage === 'es') {
currentLanguage = 'en';
} else if (currentLanguage === 'en') {
currentLanguage = 'fr';
} else {
currentLanguage = 'es';
}
// Save language preference
storage.selectedLanguage = currentLanguage;
// Update word categories
updateWordCategories();
// Update language button if visible
if (languageButton) {
languageButton.setLanguage(currentLanguage);
}
}
// Function to show game UI
function showGameUI() {
if (scoreText) {
scoreText.visible = true;
}
if (livesContainer) {
livesContainer.visible = true;
}
if (languageButton) {
languageButton.visible = true;
}
if (musicToggleButton) {
musicToggleButton.visible = true;
}
}
// Function to hide game UI
function hideGameUI() {
if (scoreText) {
scoreText.visible = false;
}
if (livesContainer) {
livesContainer.visible = false;
}
if (languageButton) {
languageButton.visible = false;
}
if (musicToggleButton) {
musicToggleButton.visible = false;
}
}
// Score display
scoreText = new Text2('0', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreText.y = 50;
// Lives display - Create a container for hearts with better positioning
livesContainer = new Container();
livesContainer.x = -200; // Offset to position hearts better in top right
livesContainer.y = 50;
LK.gui.topRight.addChild(livesContainer);
// Add hearts to the lives container
for (var i = 0; i < 3; i++) {
var heart = new Heart();
heart.x = i * 80;
heart.y = 0;
hearts.push(heart);
livesContainer.addChild(heart);
}
// Add "Lives:" label next to hearts
var livesLabel = new Text2('Lives:', {
size: 60,
fill: 0xFFFFFF
});
livesLabel.anchor.set(1, 0.5);
livesLabel.x = -20;
livesLabel.y = 0;
livesContainer.addChild(livesLabel);
// Language selection button
languageButton = new LanguageButton();
languageButton.x = -150;
languageButton.y = 150;
languageButton.setLanguage(currentLanguage);
LK.gui.topRight.addChild(languageButton);
// Music toggle button
musicToggleButton = new MusicToggleButton();
musicToggleButton.x = -150;
musicToggleButton.y = 220;
musicToggleButton.updateText();
LK.gui.topRight.addChild(musicToggleButton);
// Start game immediately
startGame();
// Function to spawn a new word
function spawnWord() {
var word;
// 20% chance to spawn a misspelled word
if (Math.random() < 0.2 && misspelledWordsArray.length > 0) {
word = new MisspelledWord();
var randomMisspelledWord = misspelledWordsArray[Math.floor(Math.random() * misspelledWordsArray.length)];
word.setWord(randomMisspelledWord);
} else {
word = new Word();
var randomWord = allWords[Math.floor(Math.random() * allWords.length)];
word.setWord(randomWord);
}
// Random horizontal position
word.x = Math.random() * (2048 - 300) + 150;
word.y = -100;
word.fallSpeed = currentSpeed;
words.push(word);
game.addChild(word);
}
// Function to lose a life
function loseLife() {
lives--;
if (lives >= 0 && lives < hearts.length) {
hearts[lives].alpha = 0.3;
}
if (lives <= 0) {
// Save high score before game over
var currentScore = LK.getScore();
var highScoreKey = 'highScore' + currentLanguage.charAt(0).toUpperCase() + currentLanguage.slice(1);
var currentHighScore = storage[highScoreKey] || 0;
if (currentScore > currentHighScore) {
storage[highScoreKey] = currentScore;
}
// Show game over
LK.showGameOver();
}
// Play wrong sound if music is enabled
if (musicEnabled) {
LK.getSound('wrongWord').play();
}
// Flash screen red
LK.effects.flashScreen(0xFF1744, 500);
}
// Function to show high scores when game is paused
function showHighScores() {
// Get high scores for all languages
var highScoreEs = storage.highScoreEs || 0;
var highScoreEn = storage.highScoreEn || 0;
var highScoreFr = storage.highScoreFr || 0;
// Create high score display container
var highScoreContainer = new Container();
highScoreContainer.x = -400;
highScoreContainer.y = 300;
LK.gui.topRight.addChild(highScoreContainer);
// Title
var titleText = new Text2('High Scores:', {
size: 40,
fill: 0xFFFFFF
});
titleText.anchor.set(1, 0);
titleText.x = -20;
titleText.y = 0;
highScoreContainer.addChild(titleText);
// Spanish high score
var esText = new Text2('Español: ' + highScoreEs, {
size: 32,
fill: 0xFFFFFF
});
esText.anchor.set(1, 0);
esText.x = -20;
esText.y = 50;
highScoreContainer.addChild(esText);
// English high score
var enText = new Text2('English: ' + highScoreEn, {
size: 32,
fill: 0xFFFFFF
});
enText.anchor.set(1, 0);
enText.x = -20;
enText.y = 90;
highScoreContainer.addChild(enText);
// French high score
var frText = new Text2('Français: ' + highScoreFr, {
size: 32,
fill: 0xFFFFFF
});
frText.anchor.set(1, 0);
frText.x = -20;
frText.y = 130;
highScoreContainer.addChild(frText);
return highScoreContainer;
}
// Store reference to high score display
var highScoreDisplay = null;
// Game pause handler
LK.on('pause', function () {
if (!highScoreDisplay) {
highScoreDisplay = showHighScores();
}
});
// Game resume handler
LK.on('resume', function () {
if (highScoreDisplay) {
highScoreDisplay.destroy();
highScoreDisplay = null;
}
});
// Game update loop
game.update = function () {
// Only update game logic when playing
if (gameState !== 'playing') {
return;
}
// Spawn new words
wordSpawnTimer++;
if (wordSpawnTimer >= spawnInterval) {
spawnWord();
wordSpawnTimer = 0;
}
// Update and check words
for (var i = words.length - 1; i >= 0; i--) {
var word = words[i];
// Check if word reached bottom
if (word.y > 2732 + 50) {
if (!word.missed) {
word.missed = true;
// Only lose life if it's not a misspelled word
if (!(word instanceof MisspelledWord)) {
loseLife();
}
}
word.destroy();
words.splice(i, 1);
}
}
// Increase speed every 20 points
var speedLevel = Math.floor(LK.getScore() / 20);
currentSpeed = 2 + speedLevel * 0.5;
// Decrease spawn interval slightly as speed increases
spawnInterval = Math.max(60, 120 - speedLevel * 5);
// Change background color when speed level increases
if (speedLevel !== lastSpeedLevel) {
currentColorIndex = speedLevel % backgroundColors.length;
var targetColor = backgroundColors[currentColorIndex];
// Simple immediate color change - tween doesn't work well with color interpolation in this context
game.setBackgroundColor(targetColor);
lastSpeedLevel = speedLevel;
}
};