/****
* Plugins
****/
var facekit = LK.import("@upit/facekit.v1");
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Duck class representing the player's duck
var Duck = Container.expand(function () {
var self = Container.call(this);
var duckGraphics = self.attachAsset('duck', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1; // Initial speed of the duck
self.speedVariation = 0; // Variable to keep track of the speed variation
self.update = function () {
if (countdown == 0) {
// Update the duck's speed using the tween plugin
tween(self, {
speedVariation: Math.random() * 2 - 1
}, {
duration: Math.random() * 1000,
// Random duration for the speed variation
easing: tween.easeInOut
});
self.x += self.speed + self.speedVariation;
}
};
});
// HzBar class representing the Hz bar
var HzBar = Container.expand(function () {
var self = Container.call(this);
var barGraphics = self.attachAsset('hzBar', {
width: 10,
anchorX: 0.0,
anchorY: 0.5
});
self.update = function () {
// Only update the bar's width and color when the player is speaking
if (facekit.volume > 0.5 && facekit.mouthOpen) {
// Update the bar's width based on the pitch
// The maximum width is the screen width minus the starting position of the bar
// Multiply the pitch by the maximum width and divide by the maximum pitch (2000Hz)
barGraphics.width = facekit.pitch * ((1500 - hzBar.x) / 500);
// Update the bar's color based on the pitch
if (facekit.volume < 1000) {
self.tint = 0x4b0082; // Indigo for low pitch
} else {
self.tint = 0xFF0000; // Red for high pitch
}
}
};
return self;
});
// OtherDuck class representing the other ducks
var OtherDuck = Container.expand(function () {
var self = Container.call(this);
var duckGraphics = self.attachAsset('duck', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1; // Initial speed of the duck
self.speedVariation = 0; // Variable to keep track of the speed variation
self.update = function () {
if (countdown == 0) {
// Update the duck's speed using the tween plugin
tween(self, {
speedVariation: Math.random() * 2 - 1
}, {
duration: Math.random() * 1000,
// Random duration for the speed variation
easing: tween.easeInOut
});
self.x += self.speed + self.speedVariation;
}
};
return self;
});
// PlayerDuck class representing the player's duck
var PlayerDuck = Container.expand(function () {
var self = Container.call(this);
// Create a playerDuck graphic and assign it to the playerDuck instance
var playerDuckGraphics = self.attachAsset('playerDuck', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
if (countdown == 0 && facekit.volume > 0.5 && facekit.mouthOpen) {
// Move only if volume is above the threshold and the player is speaking
// Update the speed based on the pitch
self.speed = facekit.pitch / 100;
self.x += self.speed;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate sky
});
/****
* Game Code
****/
// Initialize game elements
var ducks = [];
var countdown = 3;
var countdownText = new Text2(countdown.toString(), {
size: 150,
fill: 0xFFFFFF
});
LK.gui.center.addChild(countdownText);
var startX = 100; // Define startX as the starting position of the duck
for (var i = 0; i < 3; i++) {
// Change this from 5 to 7 to create 7 ducks
var duck = game.addChild(new Duck());
duck.x = startX; // Starting position of the duck
duck.y = 600 + i * 200 + i * 50; // Aligned vertically
ducks.push(duck);
}
var playerDuck = game.addChild(new PlayerDuck());
var finishLine = game.addChild(LK.getAsset('finishLine', {}));
finishLine.x = 2048 - finishLine.width - 50;
finishLine.y = 0;
var hzBar = game.addChild(new HzBar());
hzBar.x = 100; // Start a few cm from the left end of the screen
hzBar.y = 100; // Start a little bit down from the top of the screen
var lastDuck = ducks[ducks.length - 1];
playerDuck.y = lastDuck.y + lastDuck.height + 100;
playerDuck.x = startX;
var score = 0;
// Update function called every frame
game.update = function () {
if (countdown > 0 && LK.ticks % 60 == 0) {
countdown--;
countdownText.setText(countdown.toString());
if (countdown == 0) {
LK.gui.center.removeChild(countdownText);
// Reset the duck's speed variation
for (var j = 0; j < ducks.length; j++) {
var duck = ducks[j];
duck.speedVariation = 0;
}
}
}
if (playerDuck.x > finishLine.x) {
LK.showYouWin();
return;
}
if (LK.ticks % Math.floor(Math.random() * 60 + 60) == 0) {
LK.getSound('quack').play();
}
for (var j = 0; j < ducks.length; j++) {
var duck = ducks[j];
duck.update();
// Check if duck has reached the end of the track
if (duck.x > finishLine.x) {
LK.showGameOver();
}
}
};
// Update function called every frame
game.update = function () {
if (countdown > 0 && LK.ticks % 60 == 0) {
countdown--;
countdownText.setText(countdown.toString());
if (countdown == 0) {
LK.gui.center.removeChild(countdownText);
// Reset the duck's speed variation
for (var j = 0; j < ducks.length; j++) {
var duck = ducks[j];
duck.speedVariation = 0;
}
}
}
if (playerDuck.x > finishLine.x) {
LK.showYouWin();
return;
}
if (LK.ticks % Math.floor(Math.random() * 60 + 60) == 0) {
LK.getSound('quack').play();
}
for (var j = 0; j < ducks.length; j++) {
var duck = ducks[j];
duck.update();
// Check if duck has reached the end of the track
if (duck.x > finishLine.x) {
LK.showGameOver();
}
}
};