Code edit (25 edits merged)
Please save this source code
Code edit (10 edits merged)
Please save this source code
User prompt
Please fix the bug: 'PlayerDuck is not defined' in or related to this line: 'var playerDuck = game.addChild(new PlayerDuck());' Line Number: 100
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'attachAsset')' in or related to this line: 'self.attachAsset('playerDuck', {' Line Number: 47
Code edit (1 edits merged)
Please save this source code
User prompt
Make the bar to start from a little lower and more right
Code edit (5 edits merged)
Please save this source code
User prompt
Make the bar not move unless I speak
Code edit (5 edits merged)
Please save this source code
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'volumeText is not defined' in or related to this line: 'volumeText.anchor.set(0.5, 0);' Line Number: 106
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'anchor')' in or related to this line: 'var volumeText = volumeText.anchor.set(0.5, 0);' Line Number: 106
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'startX is not defined' in or related to this line: 'duck.x = startX; // Starting position of the duck' Line Number: 109
Code edit (10 edits merged)
Please save this source code
User prompt
Put other two ducks (not players)
User prompt
Delete the last thing I asked you to do
Code edit (1 edits merged)
Please save this source code
User prompt
Make the player duck (white duck) more slow
User prompt
Perfect. Now I want to change a little the game. I want in the begging to select player. The players will be 8. I will insert a separate photo of each player in assets. And when the user selects a player, that specific player will be playing
Code edit (2 edits merged)
Please save this source code
User prompt
Make the ducks have more space between them
User prompt
In the end if I win, I want only just to be written "You Win!" and nothing more
User prompt
I also want it to be in the middle and a button saying "Play again"
===================================================================
--- original.js
+++ change.js
@@ -22,8 +22,9 @@
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;
}
@@ -32,37 +33,39 @@
// HzBar class representing the Hz bar
var HzBar = Container.expand(function () {
var self = Container.call(this);
var barGraphics = self.attachAsset('hzBar', {
- width: 0,
+ width: 100,
anchorX: 0.0,
anchorY: 0.5
});
- var maxPitch = 2000; // Maximum pitch expected
self.update = function () {
- if (facekit.volume > 0.5 && facekit.mouthOpen) {
- barGraphics.width = Math.min(1500, facekit.pitch / maxPitch * 1500);
- self.tint = interpolateColor(0x87CEEB, 0xFF0000, facekit.pitch / maxPitch);
+ // 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 * ((2048 - hzBar.x) / 900);
+ // Update the bar's color based on the pitch
+ if (facekit.pitch < 500) {
+ self.tint = 0x4b0082; // Indigo for low pitch
+ } else {
+ self.tint = 0xFF0000; // Red for high pitch
}
};
- function interpolateColor(color1, color2, factor) {
- var r = Math.round((color1 >> 16) * (1 - factor) + (color2 >> 16) * factor);
- var g = Math.round((color1 >> 8 & 0xFF) * (1 - factor) + (color2 >> 8 & 0xFF) * factor);
- var b = Math.round((color1 & 0xFF) * (1 - factor) + (color2 & 0xFF) * factor);
- return r << 16 | g << 8 | b;
- }
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 = 5;
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;
}
};
@@ -78,8 +81,9 @@
/****
* Game Code
****/
+// Initialize game elements
var ducks = [];
var countdown = 3;
var countdownText = new Text2(countdown.toString(), {
size: 150,
@@ -88,49 +92,57 @@
var volumeText = new Text2('Volume: 0', {
size: 50,
fill: 0xFFFFFF
});
-var hzBar = game.addChild(new HzBar());
-hzBar.x = 50;
-hzBar.y = 50;
volumeText.anchor.set(0.5, 0);
LK.gui.top.addChild(volumeText);
countdownText.anchor.set(0.5, 0);
-LK.gui.center.addChild(countdownText);
var startX = 100;
+LK.gui.center.addChild(countdownText);
for (var i = 0; i < 5; i++) {
var duck = game.addChild(new Duck());
- duck.x = startX;
- duck.y = 600 + i * 200 + i * 20;
+ duck.x = startX; // Starting position of the duck
+ duck.y = 600 + i * 200 + i * 20; // 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;
+ }
}
}
volumeText.setText('Volume: ' + Math.round(facekit.volume));
- hzBar.update(); // Update the Hz bar each frame
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++) {
- ducks[j].update();
- if (ducks[j].x > finishLine.x) {
+ var duck = ducks[j];
+ duck.update();
+ // Check if duck has reached the end of the track
+ if (duck.x > finishLine.x) {
LK.showGameOver();
}
}
};
\ No newline at end of file