User prompt
Add a new global nbFishPerLevel = 10
User prompt
In spawnFish check if fishType argument is less or equal to nbFishTypes, if no, force it to nbFishTypes
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: Can't find variable: updateFish1Collisions' in or related to this line: 'updateFish1Collisions();' Line Number: 287
Code edit (1 edits merged)
Please save this source code
User prompt
Move background1 declaration with other globals
User prompt
Move global variables declarations to one unique place
User prompt
Play level up when shark gains a level
User prompt
In spawnFishes, instead of 1,2,3 use sharkLevel, sharkLevel+1 and sharkLevel+2
User prompt
Now remove spawnFish3, fish3s and updateFish3Collisions and Fish3 class but not fish3 asset
User prompt
Please fix the bug: 'ReferenceError: Can't find variable: updateFish2Collisions' in or related to this line: 'updateFish2Collisions();' Line Number: 336
User prompt
Now remove spawnFish2, fish2s and updateFish2Collisions and Fish2 class but not fish2 asset
User prompt
Now remove spawnFish2, fish2s and updateFish2Collisions
User prompt
Now use spawnFish(3); to replace spawnFish3(); call
User prompt
Now use spawnFish(2); to replace spawnFish2(); call
User prompt
Create a new updateFishCollisions that uses the fishes array. If it detects a collision, it compares the fishType to current shark level to determine if baby shark eats or gets eaten
User prompt
Create the new spawnFish function that takes a fishType argument and then uses Fish class and fishes array
User prompt
Rename current spawnFish to spawnFishes
User prompt
Now Create a spawnFish function that uses Fish class and fishes array
User prompt
Create a global array fishes =[]
User prompt
Create a pooling system for the Fish class to avoid destroying
User prompt
Add a parameter fishType to Fish class, store it and use it for the asset : 1 => fish1, 2=>fish2,…
User prompt
Play crunch sound when eat a fish
User prompt
Ok so they are similar…add a new similar class named “Fish”
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Baby Shark class var BabyShark = Container.expand(function () { var self = Container.call(this); self.currentWidth = 200; // Initialize currentSize property self.heightRatio = 124.0 / 200; // Define height ratio property self.speed = 10; self.velocityY = 0; self.shark = self.attachAsset('babyShark', { anchorX: 0.5, anchorY: 0.5, width: self.currentWidth, height: self.currentWidth * self.heightRatio }); self.collisionBody = self.attachAsset('collisionBody', { anchorX: 0.5, anchorY: 0.5, alpha: 0.7, x: self.currentWidth * 0.25, y: 10, width: self.shark.width * 0.5, height: self.shark.width * 0.5 }); self.update = function () { self.velocityY += 0.4; // Further reduced gravity effect self.y += self.velocityY; if (self.y >= 2732 - self.shark.height * 0.5) { self.y = 2732 - self.shark.height * 0.5; self.velocityY = 0; } if (self.y <= self.shark.height * 0.5) { self.y = self.shark.height * 0.5; self.velocityY = 0; } // Update logic for Baby Shark if (isDebug) { debugTxt.setText("Y: ".concat(self.y.toFixed(0), ", Height: ").concat(self.shark.height)); } }; self.grow = function () { self.currentWidth += 50; self.shark.width = self.currentWidth; self.shark.height = self.currentWidth * self.heightRatio; //self.collisionBody.width = self.height / 2; //self.collisionBody.height = self.height / 2; self.collisionBody.width = self.currentWidth * 0.45; self.collisionBody.height = self.currentWidth * 0.45; self.collisionBody.x = self.shark.width * 0.25; self.collisionBody.y = self.height * 0.05; }; }); // Fish class var Fish = Container.expand(function (fishType) { var self = Container.call(this); self.fishType = fishType; self.fish = self.attachAsset('fish' + fishType, { anchorX: 0.5, anchorY: 0.5 }); self.collisionBody = self.attachAsset('collisionBody', { anchorX: 0.5, anchorY: 0.5, alpha: 0.7, x: -self.fish.width * 0.25, width: self.fish.width * 0.5, height: self.fish.width * 0.5 }); self.speed = 5; self.update = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); // Fish1 class var Fish1 = Container.expand(function () { var self = Container.call(this); self.fish = self.attachAsset('fish1', { anchorX: 0.5, anchorY: 0.5 }); self.collisionBody = self.attachAsset('collisionBody', { anchorX: 0.5, anchorY: 0.5, alpha: 0.7, x: -self.fish.width * 0.25, width: self.fish.width * 0.5, height: self.fish.width * 0.5 }); self.speed = 5; self.collided = false; self.update = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x0000ff // Init game with blue background }); /**** * Game Code ****/ function spawnFish(fishType) { var fish = new Fish(fishType); fish.x = 2048 + 50; fish.y = Math.random() * (2732 - fish.height) + fish.height / 2; fishes.push(fish); game.addChild(fish); } var nbFishTypes = 6; function checkCollision(obj1, obj2) { // Calculate the center positions of the collision bodies var obj1CenterX = obj1.x + obj1.collisionBody.x; var obj1CenterY = obj1.y + obj1.collisionBody.y; var obj2CenterX = obj2.x + obj2.collisionBody.x; var obj2CenterY = obj2.y + obj2.collisionBody.y; // Calculate the distance between the centers var dx = obj1CenterX - obj2CenterX; var dy = obj1CenterY - obj2CenterY; var distance = Math.sqrt(dx * dx + dy * dy); // Calculate the sum of the radii var obj1Radius = obj1.collisionBody.width / 2; var obj2Radius = obj2.collisionBody.width / 2; var sumRadii = obj1Radius + obj2Radius; // Return true if the distance is less than the sum of the radii return distance < sumRadii; } var isDebug = true; // Global variable for debugging mode var sharkLevel = 1; var currentProgress = 0; var lastCollisionCheckTimeFish1 = 0; // Global variable to track the last collision check time for Fish1 var collisionCheckDelay = 128; // Global variable for collision check delay in milliseconds function spawnFishes() { if (LK.ticks % 60 == 0) { spawnFish(1); } if (LK.ticks % 300 == 0) { spawnFish(3); } if (LK.ticks % 180 == 0) { spawnFish(2); } } function updateFish1Collisions() { var currentTime = Date.now(); if (currentTime - lastCollisionCheckTimeFish1 < collisionCheckDelay) { return; } lastCollisionCheckTimeFish1 = currentTime; var offset = babyShark.shark.width * 1.5; // Define the x offset range based on baby shark's width for (var i = fish1s.length - 1; i >= 0; i--) { if (Math.abs(babyShark.x - fish1s[i].x) > offset) { continue; // Skip fishes outside the x offset range } if (checkCollision(babyShark, fish1s[i])) { // Play crunch sound LK.getSound('crunch').play(); score += 10; scoreTxt.setText(score); currentProgress += 1; currentProgressTxt.setText(currentProgress + "/10"); if (currentProgress > 1) { sharkLevel += 1; currentProgress = 0; babyShark.grow(); } fish1s[i].destroy(); fish1s.splice(i, 1); return; } } } function updateBackgrounds() { background1.x -= 2; background2.x -= 2; if (background1.x <= -2732 / 2) { background1.x = 2732 + 2732 / 2; } if (background2.x <= -2732 / 2) { background2.x = 2732 + 2732 / 2; } } var background1; function gameInitialize() { background1 = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2732 / 2, y: 2732 / 2 }); background2 = LK.getAsset('background2', { anchorX: 0.5, anchorY: 0.5, x: 2732 + 2732 / 2, y: 2732 / 2 }); game.addChild(background1); game.addChild(background2); babyShark = game.addChild(new BabyShark()); babyShark.x = 150; babyShark.y = 2732 / 2; babyShark.velocityY = 0; babyShark.collisionBody.visible = isDebug; // Show collision body if debugging fish1s = []; fish2s = []; score = 0; scoreTxt = new Text2('0', { size: 100, fill: "#ffffff", dropShadow: true, dropShadowColor: "#000000", dropShadowBlur: 4, dropShadowDistance: 6 }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); currentProgress = 0; currentProgressTxt = new Text2('0/10', { size: 100, fill: "#ffffff", dropShadow: true, dropShadowColor: "#000000", dropShadowBlur: 4, dropShadowDistance: 6 }); currentProgressTxt.anchor.set(1, 0); LK.gui.topRight.addChild(currentProgressTxt); debugTxt = new Text2('Debug Mode', { size: 50, fill: "#ff0000", dropShadow: true, dropShadowColor: "#000000", dropShadowBlur: 4, dropShadowDistance: 6 }); debugTxt.anchor.set(0.5, 1); LK.gui.bottom.addChild(debugTxt); } //background2.scale.x = -1; var babyShark; var fish1s; var fishes = []; var score; var scoreTxt; var currentProgressTxt; var debugTxt; game.down = function (x, y, obj) { babyShark.velocityY = -20; }; game.update = function () { updateBackgrounds(); updateFish1Collisions(); spawnFishes(); updateFishCollisions(); }; gameInitialize(); function updateFishCollisions() { var currentTime = Date.now(); if (currentTime - lastCollisionCheckTime < collisionCheckDelay) { return; } lastCollisionCheckTime = currentTime; var offset = babyShark.shark.width * 1.5; // Define the x offset range based on baby shark's width for (var i = fishes.length - 1; i >= 0; i--) { if (Math.abs(babyShark.x - fishes[i].x) > offset) { continue; // Skip fishes outside the x offset range } if (checkCollision(babyShark, fishes[i])) { // Play crunch sound LK.getSound('crunch').play(); if (fishes[i].fishType <= sharkLevel) { // Baby shark eats the fish score += 10; scoreTxt.setText(score); currentProgress += 1; currentProgressTxt.setText(currentProgress + "/10"); if (currentProgress >= 10) { sharkLevel += 1; currentProgress = 0; babyShark.grow(); } fishes[i].destroy(); fishes.splice(i, 1); } else { // Baby shark gets eaten LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } return; } } } var lastCollisionCheckTime = 0; // Global variable to track the last collision check time for fishes
===================================================================
--- original.js
+++ change.js
@@ -99,31 +99,8 @@
self.destroy();
}
};
});
-// Fish3 class
-var Fish3 = Container.expand(function () {
- var self = Container.call(this);
- self.fish = self.attachAsset('fish3', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.collisionBody = self.attachAsset('collisionBody', {
- anchorX: 0.5,
- anchorY: 0.5,
- alpha: 0.7,
- x: -self.fish.width * 0.25,
- width: self.fish.width * 0.5,
- height: self.fish.width * 0.5
- });
- self.speed = 5;
- self.update = function () {
- self.x -= self.speed;
- if (self.x < -self.width) {
- self.destroy();
- }
- };
-});
/****
* Initialize Game
****/
@@ -158,47 +135,13 @@
var sumRadii = obj1Radius + obj2Radius;
// Return true if the distance is less than the sum of the radii
return distance < sumRadii;
}
-function updateFish3Collisions() {
- var currentTime = Date.now();
- if (currentTime - lastCollisionCheckTimeFish3 < collisionCheckDelay) {
- return;
- }
- lastCollisionCheckTimeFish3 = currentTime;
- var offset = babyShark.shark.width * 1.5; // Define the x offset range based on baby shark's width
- for (var k = fish3s.length - 1; k >= 0; k--) {
- if (Math.abs(babyShark.x - fish3s[k].x) > offset) {
- continue; // Skip fishes outside the x offset range
- }
- if (checkCollision(babyShark, fish3s[k])) {
- // Play crunch sound
- LK.getSound('crunch').play();
- if (fish3s[k].x - fish3s[k].width / 3 < babyShark.x) {
- // Bounce baby shark off the bigger fish
- babyShark.velocityY = -10;
- } else {
- // Lose a life
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- }
- }
-}
-function spawnFish3() {
- var fish3 = new Fish3();
- fish3.x = 2048 + 50;
- fish3.y = Math.random() * (2732 - fish3.height) + fish3.height / 2;
- fish3s.push(fish3);
- game.addChild(fish3);
-}
var isDebug = true; // Global variable for debugging mode
var sharkLevel = 1;
var currentProgress = 0;
var lastCollisionCheckTimeFish1 = 0; // Global variable to track the last collision check time for Fish1
var collisionCheckDelay = 128; // Global variable for collision check delay in milliseconds
-var lastCollisionCheckTimeFish3 = 0; // Global variable to track the last collision check time for Fish3
-var fish3s = []; // Array to store Fish3 instances
function spawnFishes() {
if (LK.ticks % 60 == 0) {
spawnFish(1);
}
Orange Baby fish lateral view. 2024 game style
Pink Baby fish lateral view. 2024 game style
Empty Under water. water only. 2024 game style
Cute Baby shark.. . 2024 game style. Side view. Photorealistic
Classic fish lateral view.. 2024 game style
Classic fish with fangs lateral view.. 2024 game style. Photorealistic
Classic fish with fangs mouth closed lateral view.. 2024 game style. Photorealistic. Full side view.
Cachalot. Horizontal closed lateral view.... 2024 game style. Photorealistic. Full side view.
Straight horizontal Orca. Mouth open. 2024 game style. Photorealistic. Full side view
Start button in the shape of a shark mouth.
beluga swimming mouth open. 2024 game style. Photorealistic. Entire lateral profile view, perfectly horizontal.
North Pacific right whale. Horizontal complete lateral view..... 2024 game style. Photorealistic. Full side view.
Decomposition of Horizontal Swimming movement of a woman with a snorkel.. 2024 game style. 2 frames sprite sheet. Photorealistic
Very minimalist skeleton of a fish with a fin and cute shark head... 2d. Black background. High contrast. No shadows.