/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { highScore: 0, difficulty: 1 }); /**** * Classes ****/ /**** * Classes ****/ var FallingWord = Container.expand(function (wordText, speed) { var self = Container.call(this); self.wordText = wordText; self.speed = speed || 2; self.typed = ""; self.completed = false; var background = self.attachAsset('wordBackground', { anchorX: 0.5, anchorY: 0.5, width: 800, height: 200 }); self.wordDisplay = new Text2(wordText, { size: 100, fill: 0xffffff }); self.wordDisplay.anchor.set(0.5, 0.5); self.addChild(self.wordDisplay); self.progressDisplay = new Text2("", { size: 90, fill: 0x000000 }); self.progressDisplay.anchor.set(0.5, 0.5); self.addChild(self.progressDisplay); self.updateTyped = function (typedText) { self.typed = typedText; self.progressDisplay.setText(typedText); if (typedText === self.wordText) { self.completed = true; return true; } return false; }; self.update = function () { self.y += self.speed / 4; }; return self; }); /**** * Initialize Game ****/ /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1a1a2e }); /**** * Game Code ****/ /**** * Plugins ****/ /**** * Game Code ****/ var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); var words = ["type", "hero", "word", "game", "play", "fast", "quick", "next", "last", "first"]; var longWords = ["keyboard", "challenge", "practice", "improvement", "experience", "butterfly", "professional", "application", "perfume", "intelligent"]; var activeWords = []; var gameActive = false; var currentInput = ""; var spawnInterval = 6000; var lastSpawnTime = 0; var score = 0; var level = 1; var wordCount = 0; var scoreDisplay = new Text2("Score: 0", { size: 90, fill: 0xFFFFFF }); scoreDisplay.anchor.set(0.5, 0); LK.gui.top.addChild(scoreDisplay); var highScoreDisplay = new Text2("High Score: " + storage.highScore, { size: 70, fill: 0xf5f5f5 }); highScoreDisplay.anchor.set(0.5, 0); highScoreDisplay.y = 120; LK.gui.top.addChild(highScoreDisplay); var levelDisplay = new Text2("", { size: 90, fill: 0xdbdfdb }); levelDisplay.anchor.set(0.8, 0); levelDisplay.y = 10; LK.gui.top.addChild(levelDisplay); var inputDisplay = new Text2("", { size: 100, fill: 0xedebf0 }); var inputBackground = new Text2("", { size: 110, fill: 0x5010d1 }); inputBackground.anchor.set(0.5, 1); inputBackground.y = -80; LK.gui.bottom.addChild(inputBackground); inputDisplay.anchor.set(0.5, 1); inputDisplay.y = -40; LK.gui.bottom.addChild(inputDisplay); var keyboard = new Container(); game.addChild(keyboard); keyboard.y = 2200; var menu = new Container(); game.addChild(menu); var congratulationsPopup = new Container(); game.addChild(congratulationsPopup); congratulationsPopup.visible = false; var congratulationsText = new Text2("Level 1 Complete!", { size: 100, fill: 0xFFFFFF }); congratulationsText.anchor.set(0.5); congratulationsPopup.addChild(congratulationsText); congratulationsPopup.x = 2048 / 2; congratulationsPopup.y = 2732 / 2; function startGame() { gameActive = true; activeWords = []; currentInput = ""; score = 0; wordCount = 0; spawnInterval = 6000; lastSpawnTime = 0; scoreDisplay.setText("Score: 0"); updateInput(""); LK.playMusic('bgMusic'); menu.visible = false; congratulationsPopup.visible = false; } function endGame() { gameActive = false; if (score > storage.highScore) { storage.highScore = score; highScoreDisplay.setText("High Score: " + storage.highScore); } LK.stopMusic(); LK.showGameOver(); menu.visible = true; congratulationsPopup.visible = false; } function getRandomWord() { var wordPool = level === 2 ? longWords : words; return wordPool[Math.floor(Math.random() * wordPool.length)]; } function updateInput(text) { currentInput = text; inputDisplay.setText(text); } function spawnWord() { var word = getRandomWord(); var speed = 3 + level * 1; var fallingWord = new FallingWord(word, speed); fallingWord.x = Math.random() * (2048 - 400) + 100; fallingWord.y = 100; game.addChild(fallingWord); activeWords.push(fallingWord); wordCount++; if (wordCount >= 10) { gameActive = false; if (level === 1) { congratulationsPopup.visible = true; level = 2; wordCount = 0; LK.setTimeout(function () { congratulationsPopup.visible = false; startGame(); }, 3000); } else { endGame(); } } } function checkInput(_char) { var newInput = currentInput + _char; updateInput(newInput); for (var i = 0; i < activeWords.length; i++) { var word = activeWords[i]; if (word.wordText.toLowerCase().startsWith(newInput.toLowerCase())) { if (newInput.length === word.wordText.length) { score += 5; LK.setScore(score); scoreDisplay.setText("Score: " + score); LK.getSound('correctWord').play(); LK.effects.flashObject(word, 0x00ff00, 400); tween(word, { alpha: 0 }, { duration: 300, onFinish: function onFinish() { game.removeChild(word); activeWords.splice(activeWords.indexOf(word), 1); } }); updateInput(""); return true; } word.updateTyped(newInput); return true; } } return false; } function clearInput() { updateInput(""); for (var i = 0; i < activeWords.length; i++) { activeWords[i].updateTyped(""); } } var keyboardButtons = []; var keyboardLayout = [['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'], ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'], ['Z', 'X', 'C', 'V', 'B', 'N', 'M']]; var totalKeyboardHeight = keyboardLayout.length * 10; function createKeyboard() { var buttonSize = 70; var spacing = 40; var rows = keyboardLayout.length; for (var row = 0; row < rows; row++) { var keys = keyboardLayout[row]; var rowWidth = keys.length * (buttonSize + spacing); var startX = (2048 - rowWidth) / 2; var yOffset = 2048 - totalKeyboardHeight - 80; for (var i = 0; i < keys.length; i++) { var key = keys[i]; var keyButton = new Container(); var keyBackground = LK.getAsset('wordBackground', { width: 100, height: 120, anchorX: 0.5, anchorY: 0.5, tint: 0xcc2d2d }); keyButton.addChild(keyBackground); var keyText = new Text2(key, { size: 90, fill: 0xFFFFFF }); keyText.anchor.set(0.5, 0.5); keyButton.addChild(keyText); keyButton.x = startX + i * (buttonSize + spacing) + buttonSize / 4; keyButton.y = row * (buttonSize + spacing) + buttonSize / 20; keyButton.keyValue = key; keyButton.interactive = true; keyButton.down = function (x, y, obj) { if (this.keyValue) { checkInput(this.keyValue); } if (obj.children && obj.children.length > 0 && obj.children[0]) { tween(obj.children[0], { tint: 0x3498db }, { duration: 100 }); } }; keyButton.up = function (x, y, obj) { if (obj.children && obj.children.length > 0 && obj.children[0]) { tween(obj.children[0], { tint: 0xcc2d2d }, { duration: 100 }); } }; keyboard.addChild(keyButton); keyboardButtons.push(keyButton); } } var deleteKey = new Container(); var deleteBackground = LK.getAsset('wordBackground', { width: 250, height: 120, anchorX: 0.5, anchorY: 0.5, tint: 0xcc2d2d }); deleteKey.addChild(deleteBackground); var deleteText = new Text2("DEL", { size: 90, fill: 0xFFFFFF }); deleteText.anchor.set(0.5, 0.5); deleteKey.addChild(deleteText); deleteKey.x = 2048 - (buttonSize + spacing) * 7; deleteKey.y = rows * (buttonSize + spacing) + buttonSize / 2; deleteKey.interactive = true; deleteKey.down = function () { if (currentInput.length > 0) { updateInput(currentInput.slice(0, -1)); } }; keyboard.addChild(deleteKey); var spaceBar = new Container(); var spaceBackground = LK.getAsset('wordBackground', { width: 400, height: 120, anchorX: 0.8, anchorY: 0.4, tint: 0xc11010 }); spaceBar.addChild(spaceBackground); var spaceText = new Text2("SPACE", { size: 90, fill: 0xFFFFFF }); spaceText.anchor.set(0.9, 0.4); spaceBar.addChild(spaceText); spaceBar.y = rows * (buttonSize + spacing) + 40; spaceBar.x = 2048 / 2; spaceBar.interactive = true; spaceBar.down = function () { checkInput(" "); }; keyboard.addChild(spaceBar); } createKeyboard(); var titleText = new Text2("Typing Titan", { size: 120, fill: 0xFFFFFF }); titleText.anchor.set(0.5); menu.addChild(titleText); titleText.y = -180; var level1Button = new Container(); var level1Text = new Text2("Level 1", { size: 100, fill: 0xFFFFFF }); level1Text.anchor.set(0.5); level1Button.addChild(level1Text); level1Button.y = 50; level1Button.interactive = true; level1Button.down = function () { level = 1; startGame(); }; menu.addChild(level1Button); var level2Button = new Container(); var level2Text = new Text2("Level 2", { size: 100, fill: 0xFFFFFF }); level2Text.anchor.set(0.5); level2Button.addChild(level2Text); level2Button.y = 180; level2Button.interactive = true; level2Button.down = function () { level = 2; startGame(); }; menu.addChild(level2Button); menu.x = 2048 / 2; menu.y = 2732 / 2; game.update = function () { if (!gameActive) { return; } var now = Date.now(); if (now - lastSpawnTime >= spawnInterval) { spawnWord(); lastSpawnTime = now; } for (var i = activeWords.length - 1; i >= 0; i--) { var word = activeWords[i]; word.update(); if (word.y > 2700) { LK.getSound('wordHitBottom').play(); game.removeChild(word); activeWords.splice(i, 1); clearInput(); endGame(); } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0,
difficulty: 1
});
/****
* Classes
****/
/**** * Classes
****/
var FallingWord = Container.expand(function (wordText, speed) {
var self = Container.call(this);
self.wordText = wordText;
self.speed = speed || 2;
self.typed = "";
self.completed = false;
var background = self.attachAsset('wordBackground', {
anchorX: 0.5,
anchorY: 0.5,
width: 800,
height: 200
});
self.wordDisplay = new Text2(wordText, {
size: 100,
fill: 0xffffff
});
self.wordDisplay.anchor.set(0.5, 0.5);
self.addChild(self.wordDisplay);
self.progressDisplay = new Text2("", {
size: 90,
fill: 0x000000
});
self.progressDisplay.anchor.set(0.5, 0.5);
self.addChild(self.progressDisplay);
self.updateTyped = function (typedText) {
self.typed = typedText;
self.progressDisplay.setText(typedText);
if (typedText === self.wordText) {
self.completed = true;
return true;
}
return false;
};
self.update = function () {
self.y += self.speed / 4;
};
return self;
});
/****
* Initialize Game
****/
/**** * Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
/**** * Plugins
****/
/**** * Game Code
****/
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
var words = ["type", "hero", "word", "game", "play", "fast", "quick", "next", "last", "first"];
var longWords = ["keyboard", "challenge", "practice", "improvement", "experience", "butterfly", "professional", "application", "perfume", "intelligent"];
var activeWords = [];
var gameActive = false;
var currentInput = "";
var spawnInterval = 6000;
var lastSpawnTime = 0;
var score = 0;
var level = 1;
var wordCount = 0;
var scoreDisplay = new Text2("Score: 0", {
size: 90,
fill: 0xFFFFFF
});
scoreDisplay.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreDisplay);
var highScoreDisplay = new Text2("High Score: " + storage.highScore, {
size: 70,
fill: 0xf5f5f5
});
highScoreDisplay.anchor.set(0.5, 0);
highScoreDisplay.y = 120;
LK.gui.top.addChild(highScoreDisplay);
var levelDisplay = new Text2("", {
size: 90,
fill: 0xdbdfdb
});
levelDisplay.anchor.set(0.8, 0);
levelDisplay.y = 10;
LK.gui.top.addChild(levelDisplay);
var inputDisplay = new Text2("", {
size: 100,
fill: 0xedebf0
});
var inputBackground = new Text2("", {
size: 110,
fill: 0x5010d1
});
inputBackground.anchor.set(0.5, 1);
inputBackground.y = -80;
LK.gui.bottom.addChild(inputBackground);
inputDisplay.anchor.set(0.5, 1);
inputDisplay.y = -40;
LK.gui.bottom.addChild(inputDisplay);
var keyboard = new Container();
game.addChild(keyboard);
keyboard.y = 2200;
var menu = new Container();
game.addChild(menu);
var congratulationsPopup = new Container();
game.addChild(congratulationsPopup);
congratulationsPopup.visible = false;
var congratulationsText = new Text2("Level 1 Complete!", {
size: 100,
fill: 0xFFFFFF
});
congratulationsText.anchor.set(0.5);
congratulationsPopup.addChild(congratulationsText);
congratulationsPopup.x = 2048 / 2;
congratulationsPopup.y = 2732 / 2;
function startGame() {
gameActive = true;
activeWords = [];
currentInput = "";
score = 0;
wordCount = 0;
spawnInterval = 6000;
lastSpawnTime = 0;
scoreDisplay.setText("Score: 0");
updateInput("");
LK.playMusic('bgMusic');
menu.visible = false;
congratulationsPopup.visible = false;
}
function endGame() {
gameActive = false;
if (score > storage.highScore) {
storage.highScore = score;
highScoreDisplay.setText("High Score: " + storage.highScore);
}
LK.stopMusic();
LK.showGameOver();
menu.visible = true;
congratulationsPopup.visible = false;
}
function getRandomWord() {
var wordPool = level === 2 ? longWords : words;
return wordPool[Math.floor(Math.random() * wordPool.length)];
}
function updateInput(text) {
currentInput = text;
inputDisplay.setText(text);
}
function spawnWord() {
var word = getRandomWord();
var speed = 3 + level * 1;
var fallingWord = new FallingWord(word, speed);
fallingWord.x = Math.random() * (2048 - 400) + 100;
fallingWord.y = 100;
game.addChild(fallingWord);
activeWords.push(fallingWord);
wordCount++;
if (wordCount >= 10) {
gameActive = false;
if (level === 1) {
congratulationsPopup.visible = true;
level = 2;
wordCount = 0;
LK.setTimeout(function () {
congratulationsPopup.visible = false;
startGame();
}, 3000);
} else {
endGame();
}
}
}
function checkInput(_char) {
var newInput = currentInput + _char;
updateInput(newInput);
for (var i = 0; i < activeWords.length; i++) {
var word = activeWords[i];
if (word.wordText.toLowerCase().startsWith(newInput.toLowerCase())) {
if (newInput.length === word.wordText.length) {
score += 5;
LK.setScore(score);
scoreDisplay.setText("Score: " + score);
LK.getSound('correctWord').play();
LK.effects.flashObject(word, 0x00ff00, 400);
tween(word, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
game.removeChild(word);
activeWords.splice(activeWords.indexOf(word), 1);
}
});
updateInput("");
return true;
}
word.updateTyped(newInput);
return true;
}
}
return false;
}
function clearInput() {
updateInput("");
for (var i = 0; i < activeWords.length; i++) {
activeWords[i].updateTyped("");
}
}
var keyboardButtons = [];
var keyboardLayout = [['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'], ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'], ['Z', 'X', 'C', 'V', 'B', 'N', 'M']];
var totalKeyboardHeight = keyboardLayout.length * 10;
function createKeyboard() {
var buttonSize = 70;
var spacing = 40;
var rows = keyboardLayout.length;
for (var row = 0; row < rows; row++) {
var keys = keyboardLayout[row];
var rowWidth = keys.length * (buttonSize + spacing);
var startX = (2048 - rowWidth) / 2;
var yOffset = 2048 - totalKeyboardHeight - 80;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var keyButton = new Container();
var keyBackground = LK.getAsset('wordBackground', {
width: 100,
height: 120,
anchorX: 0.5,
anchorY: 0.5,
tint: 0xcc2d2d
});
keyButton.addChild(keyBackground);
var keyText = new Text2(key, {
size: 90,
fill: 0xFFFFFF
});
keyText.anchor.set(0.5, 0.5);
keyButton.addChild(keyText);
keyButton.x = startX + i * (buttonSize + spacing) + buttonSize / 4;
keyButton.y = row * (buttonSize + spacing) + buttonSize / 20;
keyButton.keyValue = key;
keyButton.interactive = true;
keyButton.down = function (x, y, obj) {
if (this.keyValue) {
checkInput(this.keyValue);
}
if (obj.children && obj.children.length > 0 && obj.children[0]) {
tween(obj.children[0], {
tint: 0x3498db
}, {
duration: 100
});
}
};
keyButton.up = function (x, y, obj) {
if (obj.children && obj.children.length > 0 && obj.children[0]) {
tween(obj.children[0], {
tint: 0xcc2d2d
}, {
duration: 100
});
}
};
keyboard.addChild(keyButton);
keyboardButtons.push(keyButton);
}
}
var deleteKey = new Container();
var deleteBackground = LK.getAsset('wordBackground', {
width: 250,
height: 120,
anchorX: 0.5,
anchorY: 0.5,
tint: 0xcc2d2d
});
deleteKey.addChild(deleteBackground);
var deleteText = new Text2("DEL", {
size: 90,
fill: 0xFFFFFF
});
deleteText.anchor.set(0.5, 0.5);
deleteKey.addChild(deleteText);
deleteKey.x = 2048 - (buttonSize + spacing) * 7;
deleteKey.y = rows * (buttonSize + spacing) + buttonSize / 2;
deleteKey.interactive = true;
deleteKey.down = function () {
if (currentInput.length > 0) {
updateInput(currentInput.slice(0, -1));
}
};
keyboard.addChild(deleteKey);
var spaceBar = new Container();
var spaceBackground = LK.getAsset('wordBackground', {
width: 400,
height: 120,
anchorX: 0.8,
anchorY: 0.4,
tint: 0xc11010
});
spaceBar.addChild(spaceBackground);
var spaceText = new Text2("SPACE", {
size: 90,
fill: 0xFFFFFF
});
spaceText.anchor.set(0.9, 0.4);
spaceBar.addChild(spaceText);
spaceBar.y = rows * (buttonSize + spacing) + 40;
spaceBar.x = 2048 / 2;
spaceBar.interactive = true;
spaceBar.down = function () {
checkInput(" ");
};
keyboard.addChild(spaceBar);
}
createKeyboard();
var titleText = new Text2("Typing Titan", {
size: 120,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5);
menu.addChild(titleText);
titleText.y = -180;
var level1Button = new Container();
var level1Text = new Text2("Level 1", {
size: 100,
fill: 0xFFFFFF
});
level1Text.anchor.set(0.5);
level1Button.addChild(level1Text);
level1Button.y = 50;
level1Button.interactive = true;
level1Button.down = function () {
level = 1;
startGame();
};
menu.addChild(level1Button);
var level2Button = new Container();
var level2Text = new Text2("Level 2", {
size: 100,
fill: 0xFFFFFF
});
level2Text.anchor.set(0.5);
level2Button.addChild(level2Text);
level2Button.y = 180;
level2Button.interactive = true;
level2Button.down = function () {
level = 2;
startGame();
};
menu.addChild(level2Button);
menu.x = 2048 / 2;
menu.y = 2732 / 2;
game.update = function () {
if (!gameActive) {
return;
}
var now = Date.now();
if (now - lastSpawnTime >= spawnInterval) {
spawnWord();
lastSpawnTime = now;
}
for (var i = activeWords.length - 1; i >= 0; i--) {
var word = activeWords[i];
word.update();
if (word.y > 2700) {
LK.getSound('wordHitBottom').play();
game.removeChild(word);
activeWords.splice(i, 1);
clearInput();
endGame();
}
}
};