/****
* Classes
****/
// Assets will be automatically created based on usage in the code.
// Parrot class to handle the parrot's behavior
var Parrot = Container.expand(function () {
var self = Container.call(this);
// Attach parrot image asset
var parrotGraphics = self.attachAsset('parrot', {
anchorX: 0.5,
anchorY: 0.5
});
// Function to make the parrot say a phrase
self.sayPhrase = function (phrase) {
// Display the phrase near the parrot
var phraseText = new Text2(phrase, {
size: 50,
fill: "#ffffff",
anchorX: 0.5,
anchorY: 1 // Anchor at the bottom to appear above the parrot
});
phraseText.x = self.x;
phraseText.y = self.y - self.height / 2 - 10; // Position above the parrot
game.addChild(phraseText);
// Remove the phrase after 3 seconds
LK.setTimeout(function () {
phraseText.destroy();
}, 3000);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate the sky
});
/****
* Game Code
****/
// Create and position the parrot in the center of the screen
var parrot = new Parrot();
parrot.x = 2048 / 2;
parrot.y = 2732 / 2;
game.addChild(parrot);
// Create a "Talk" button
var talkButton = LK.getAsset('talkButton', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
// Make the button larger
scaleY: 1.5,
x: 2048 / 2,
// Center horizontally
y: 2732 - 200 // Position towards the bottom
});
game.addChild(talkButton);
// Predefined phrases for the parrot
var phrases = ["Hello!", "I'm a parrot!", "FRVR rocks!", "Coding is fun!"];
// Function to get a random phrase
function getRandomPhrase() {
return phrases[Math.floor(Math.random() * phrases.length)];
}
// Event listener for the "Talk" button
talkButton.on('down', function () {
parrot.sayPhrase(getRandomPhrase());
});
// Custom phrase input
var customPhrase = ""; // Initialize an empty string for custom phrases
// Event listener for keyboard input
game.on('keydown', function (obj) {
var key = obj.event.key;
// Check if the key is a letter, number, or space
if (/^[a-zA-Z0-9 ]$/.test(key)) {
customPhrase += key; // Append the key to the custom phrase
} else if (key === "Enter" && customPhrase.trim() !== "") {
// If the Enter key is pressed and the custom phrase is not empty, make the parrot say the custom phrase
parrot.sayPhrase(customPhrase);
customPhrase = ""; // Reset the custom phrase
} else if (key === "Backspace") {
// Allow backspace to remove the last character
customPhrase = customPhrase.slice(0, -1);
}
});
// Note: The game is designed with mobile compatibility in mind. However, as per the guidelines, keyboard inputs are implemented for demonstration purposes. In a mobile environment, an on-screen keyboard or alternative input method would be necessary for custom phrase input. /****
* Classes
****/
// Assets will be automatically created based on usage in the code.
// Parrot class to handle the parrot's behavior
var Parrot = Container.expand(function () {
var self = Container.call(this);
// Attach parrot image asset
var parrotGraphics = self.attachAsset('parrot', {
anchorX: 0.5,
anchorY: 0.5
});
// Function to make the parrot say a phrase
self.sayPhrase = function (phrase) {
// Display the phrase near the parrot
var phraseText = new Text2(phrase, {
size: 50,
fill: "#ffffff",
anchorX: 0.5,
anchorY: 1 // Anchor at the bottom to appear above the parrot
});
phraseText.x = self.x;
phraseText.y = self.y - self.height / 2 - 10; // Position above the parrot
game.addChild(phraseText);
// Remove the phrase after 3 seconds
LK.setTimeout(function () {
phraseText.destroy();
}, 3000);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate the sky
});
/****
* Game Code
****/
// Create and position the parrot in the center of the screen
var parrot = new Parrot();
parrot.x = 2048 / 2;
parrot.y = 2732 / 2;
game.addChild(parrot);
// Create a "Talk" button
var talkButton = LK.getAsset('talkButton', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
// Make the button larger
scaleY: 1.5,
x: 2048 / 2,
// Center horizontally
y: 2732 - 200 // Position towards the bottom
});
game.addChild(talkButton);
// Predefined phrases for the parrot
var phrases = ["Hello!", "I'm a parrot!", "FRVR rocks!", "Coding is fun!"];
// Function to get a random phrase
function getRandomPhrase() {
return phrases[Math.floor(Math.random() * phrases.length)];
}
// Event listener for the "Talk" button
talkButton.on('down', function () {
parrot.sayPhrase(getRandomPhrase());
});
// Custom phrase input
var customPhrase = ""; // Initialize an empty string for custom phrases
// Event listener for keyboard input
game.on('keydown', function (obj) {
var key = obj.event.key;
// Check if the key is a letter, number, or space
if (/^[a-zA-Z0-9 ]$/.test(key)) {
customPhrase += key; // Append the key to the custom phrase
} else if (key === "Enter" && customPhrase.trim() !== "") {
// If the Enter key is pressed and the custom phrase is not empty, make the parrot say the custom phrase
parrot.sayPhrase(customPhrase);
customPhrase = ""; // Reset the custom phrase
} else if (key === "Backspace") {
// Allow backspace to remove the last character
customPhrase = customPhrase.slice(0, -1);
}
});
// Note: The game is designed with mobile compatibility in mind. However, as per the guidelines, keyboard inputs are implemented for demonstration purposes. In a mobile environment, an on-screen keyboard or alternative input method would be necessary for custom phrase input.