Code edit (1 edits merged)
Please save this source code
Code edit (12 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: oconutsInTree is not defined' in or related to this line: 'oconutsInTree.push(newCoconut);' Line Number: 338
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (19 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: coconut is not defined' in or related to this line: 'scoreTest = "Zone contact = " + zoneIntersectParasol(coconut[i]);' Line Number: 434
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (16 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: livesLeftGraphics[i] is undefined' in or related to this line: 'livesLeftGraphics[i].destroy();' Line Number: 264
Code edit (1 edits merged)
Please save this source code
Code edit (7 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Sprite is not a constructor' in or related to this line: 'var newLife = new Sprite('life');' Line Number: 267
Code edit (12 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: scor is not defined' in or related to this line: 'scor += 1;' Line Number: 120
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: OptionsZone is undefined' in or related to this line: 'parasol.y = OptionsZone.y - 400;' Line Number: 189
Code edit (1 edits merged)
Please save this source code
Code edit (23 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: abs is not defined' in or related to this line: 'var deltaX = abs(coconuts[i].x - coconuts[i].X1);' Line Number: 307
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
/****
* Classes
****/
// Class for the Coconut
var Coconut = Container.expand(function () {
var self = Container.call(this);
var coconutGraphics = self.attachAsset('coconut', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.trajectory = new Trajectory();
self.update = function () {
scoreTest = "TypeTrajectory: " + self.trajectory.typeTrajectory;
if (self.trajectory.typeTrajectory == 0) {
self.trajectory.updateLinear();
}
if (self.trajectory.typeTrajectory == 1) {
self.trajectory.updateParabol();
}
self.y = self.trajectory.y;
self.x = self.trajectory.x;
if (self.y > 2732) {
self.destroy();
}
}; //fin update
});
//<Assets used in the game will automatically appear here>
// Class for the Monkey
var Monkey = Container.expand(function () {
var self = Container.call(this);
var monkeyGraphics = self.attachAsset('monkey', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Monkey logic can be added here
};
});
// Class for the Parasol
var Parasol = Container.expand(function () {
var self = Container.call(this);
var parasolGraphics = self.attachAsset('parasol', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Parasol logic can be added here
};
});
//Class for the trajectory of the coconut
//Gestion d'une trajectoire parabolique de sommet ((X1 + X2)/2, Yh)
//et de point de départ (X1, Y0) et d'arrivée (X2, Y0) avec Y0 > Yh et X1 < X2 et Yh != Y0 != 0 et X1 != X2 != 0
//la fonction de mise à jour calcule la position du point suivant en fonction de speed .
//L'equation de la trajectoire est de la forme y = a(x - X1)(x - X2) + Y0 avec a = 4(Y0 - Yh)/((X1 - X2)(X1 - X2))
//Pour la trajectoire linéaire, le point de départ est (X1, Yh) et le point d'arrivée est (X1, Y0)
//Le temps de parcours est totalTicks que se soit pour la trajectoire linéaire ou parabolique
var Trajectory = Container.expand(function () {
var self = Container.call(this);
var trajectoryGraphics = self.attachAsset('trajectory', {
anchorX: 0.5,
anchorY: 0.5
});
self.typeTrajectory = 0; //0: linear, 1: parabolic
self.X1 = 0;
self.X2 = 0;
self.Y0 = 0;
self.Yh = 0;
self.a = 0;
self.x = 0;
self.y = 0;
self.totalTicks = 0; //Nombre total de ticks pour passer de X1 à X2
self.distPerTick = 0; //Distance parcourue par tick
self.isStarted = false; //Indique si la trajectoire a commencé
self.setParameters = function (typeTrajectory, X1, X2, Y0, Yh, totalTicks) {
//Initialisation des paramètres de la trajectoire
self.typeTrajectory = typeTrajectory;
self.X1 = X1;
self.X2 = X2;
self.Y0 = Y0;
self.Yh = Yh;
self.totalTicks = totalTicks;
//scoreTest = "X1: " + self.X1 + " X2: " + self.X2 + " Y0: " + self.Y0 + " Yh: " + self.Yh + " totalTicks: " + self.totalTicks;
}; //fin setParameters
self.updateLinear = function () {
// Trajectory logic can be added here
if (self.typeTrajectory == 0 && !self.isStarted) {
self.x = self.X1;
self.y = self.Yh;
distPerTick = (self.Y0 - self.Yh) / self.totalTicks;
self.isStarted = true;
//scoreTest = "x= " + self.x + " y= " + self.y + " distPerTick= " + distPerTick;
} else {
if (self.y < self.Y0) {
//scoreTest = "y= " + self.y + " Y0= " + self.Y0;
self.y += distPerTick;
//scoreTest = "y= " + self.y;
//scoreTest += 1;
}
}
}; //fin updateLinear
self.updateParabol = function () {
//scoreTest += 1;
// Trajectory logic can be added here
if (self.typeTrajectory == 1 && !self.isStarted) {
self.x = self.X1;
self.y = self.Y0;
self.a = 4 * (self.Y0 - self.Yh) / ((self.X1 - self.X2) * (self.X1 - self.X2));
self.distPerTick = (self.X2 - self.X1) / self.totalTicks;
self.isStarted = true;
} else {
if (self.x < self.X2) {
self.x += self.xPerTick;
self.y = self.a * (self.x - self.X1) * (self.x - self.X2) + self.Y0;
}
}
}; //fin updateParabol
});
/****
* Initialize Game
****/
//fin class Trajectory
//fin class Trajectory
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
/****
* GAME DESCRIPTION:
* Game Principle:
* NAME: COCO MONKEY V1.0 by Dalhem 2024
* -There is beach where people are enjoying the sun by the shadow of palm trees.
* -Above them, three palm trees are growing coconuts.
* -In the trees, a monkey is throwing coconuts from the top of the trees, thus three points of departure.
* -The player controls a guy who holding a parasol to protect everyone from the coconuts.
* -The coconuts are falling at a regular speed, falling in straight lines.
* -If a coconut touches the parasol, the player earns points.
* -If a coconut touches the ground, the player loses a life.
* -The game is over when the player has no more lives.
* Game Areas:
* The screen is divided into 3 main zones in the following descending order:
1. Score area: displays the player's score.
2. The main game area: where the game takes place, it is divided into 3 zones corresponding to the 3 points of departure of the coconuts :
* The left zone: the coconuts fall from the left tree.
* The center zone: the coconuts fall from the center tree.
* The right zone: the coconuts fall from the right tree.
3. Options area (or advertisement).
* Game Progression:
* At the beginning of the game, the player has 0 points.
* The player has 3 lives.
* The player can move the parasol by dragging it.
* The monkey throws coconuts at a regular interval randomly from the top of the trees.
* When a coconut touches the parasol, it bounces up following a parabolic trajectory.
* The parabolic trajectory depends on the angle of the parasol:
* The more the parasol is inclined, the more the coconut will bounce back :
* If the parasol is in the left zone, the coconut may bounce following a parabolic trajectory :
* to the left, thus outside the screen.
* to the right, thus towards the center zone.
* to the extrem right, thus towards the right zone.
* If the parasol is in the center zone, the coconut may bounce following a parabolic trajectory :
* to the left, thus towards the left zone.
* to the extrem left, thus outside the screen.
* to the right, thus towards the right zone.
* to the extrem right, thus outside the screen.
* If the parasol is in the right zone, the coconut may bounce following a parabolic trajectory :
* to the left, thus towards the center zone.
* to the extrem left, thus towards the left zone.
* to the right, thus outside the screen.
* the parasol has three zones of influence:
* The left zone: the coconut will bounce to the extrem left.
* The center zone: the coconut will randomly bounce to the left or to the right.
* The right zone: the coconut will bounce to the extrem right.
****/
var monkey = game.addChild(new Monkey());
monkey.x = 2048 / 2;
monkey.y = 200;
var parasol = game.addChild(new Parasol());
parasol.x = 2048 / 2;
parasol.y = 2500;
var coconuts = [];
var nombreCoconuts = 1;
var score = 0;
var scoreTest = 0;
/****
* Game zones and backgrounds
****/
var ScoreZone = {
x: 0,
y: 0,
width: game.width,
height: 200 * game.height / 2732
};
var MainZone = {
x: 0,
y: ScoreZone.height,
width: game.width,
height: game.height - 2 * ScoreZone.height
};
var OptionsZone = {
x: 0,
y: game.height - ScoreZone.height,
width: game.width,
height: 200 * game.height / 2732
};
/****
* Score
****/
var scoreTestText = new Text2('0', {
size: 30,
fill: "#000000",
anchorX: 0.5,
anchorY: 0
});
LK.gui.topLeft.addChild(scoreTestText);
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff",
anchorX: 0.5,
anchorY: 0
});
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
parasol.x = x;
parasol.y = y;
};
game.move = function (x, y, obj) {
parasol.x = x;
parasol.y = y;
};
// Define the startPoints array
var startPoints = [2048 / 4, 2048 / 2, 2048 / 4 * 3];
/****
* Functions
****/
function updateScoreTest(nouveauScore) {
scoreTestText.setText(nouveauScore);
} //fin updateScoreTest
function updateScore(nouveauScore) {
scoreTxt.setText(nouveauScore);
} //fin updateScore
//Fonction chooseNextStartPoint: choisit aléatoirement un point de départ d'un coconut
//Renvoie un objet Coconut
function chooseNextStartPoint() {
var newCoconut = new Coconut();
// Choisir aléatoirement un point de départ
var randomIndex = Math.floor(Math.random() * startPoints.length);
var typeTrajectory = 0;
var X1 = startPoints[randomIndex];
var X2 = X1;
var Y0 = OptionsZone.y;
var Yh = monkey.y;
var totalTicks = 500;
newCoconut.trajectory.setParameters(typeTrajectory, X1, X2, Y0, Yh, totalTicks);
coconuts.push(newCoconut);
return newCoconut;
} //fin chooseNextStartPoint
/****
* Main loop
****/
game.update = function () {
//Mise à jour score
updateScoreTest(scoreTest);
updateScore(score);
//Autres actions à effectuer sans urgence
if (LK.ticks % 60 == 0) {
// Créer un nouveau coco
if (coconuts.length < nombreCoconuts) {
var newCoconut = chooseNextStartPoint();
game.addChild(newCoconut);
}
}
for (var i = coconuts.length - 1; i >= 0; i--) {
if (coconuts[i].intersects(parasol)) {
score += 1;
coconuts[i].destroy();
coconuts.splice(i, 1);
} else if (coconuts[i].y > coconuts[i].Y0) {
LK.effects.flashScreen(0xff0000, 1000);
//LK.showGameOver();
coconuts[i].trajectory.setParameters(1, coconuts[i].x, coconuts[i].x + 512, coconuts[i].Y0, ScoreZone.y, 500);
}
}
};