Code edit (2 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Move the fisherman a little to the left
User prompt
A shark can collide with the hook only once
User prompt
If a sharks mouth touches the hook when the score is zero, then the game is over
User prompt
Flip the shark horizontally if it is moving to the right
User prompt
Flip the shark horizontally if it is moving right
User prompt
If a shark is moving right the flip it horizontally
User prompt
Sharks can move in both directions horizontally
User prompt
Spawn sharks that initially move to the left as well
User prompt
Decrease the number of sharks spawned
User prompt
The shark should move along with the backgrounds
User prompt
The shark should be spawned in the same way as the fish
User prompt
Create a shark asset that eats all the fish on the hook when its mouth touches the hook and resets the score to zero
User prompt
Please fix the bug: 'ReferenceError: fisherman is not defined' in or related to this line: 'fisherman.y -= 4;' Line Number: 149
User prompt
Please fix the bug: 'fisherman is not defined' in or related to this line: 'fisherman.castLine();' Line Number: 221
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'fishingLine')' in or related to this line: 'self.x = game.fisherman.fishingLine.x; // Stick to the hook's x position' Line Number: 42
User prompt
Fish should stick to the hook once caught
User prompt
Update the score only once per fish caught
User prompt
Keep score of the number of fishes caught and display it on the top right
User prompt
The fishing hook should not teleport
User prompt
The fish should get stuck at the base of the fishing hook
User prompt
The fish should get stuck at the bottom of the fishing hook tilted 90 degrees upward
User prompt
If the fishing hook touches a fish's mouth, the fish gets stuck to the fishing hook
/****
* Classes
****/
// FishingLine class to represent the fishing line
var Fish = Container.expand(function () {
var self = Container.call(this);
var fishGraphics = self.attachAsset('fish', {
anchorX: 0.5,
anchorY: 0.5
});
// Define the direction variable
var direction = Math.random() > 0.5 ? 1 : -1;
// Flip the fish image horizontally if it's moving to the left
if (direction == -1) {
fishGraphics.scale.x *= -1;
}
// Initialize lastIntersecting state
self.lastIntersecting = false;
self.update = function () {
// If the fish hits the edge of the screen, change direction
if (self.x <= 0 || self.x >= 2048 - fishGraphics.width) {
direction *= -1;
// Flip the fish image horizontally when it changes direction
fishGraphics.scale.x *= -1;
}
self.x += 5 * direction; // Move the fish horizontally by 5 pixels per frame in the chosen direction
if (!self.stuckToHook) {
self.y -= 4; // Move the fish up along with the backgrounds
} else {
self.x = game.fisherman.fishingLine.x; // Stick to the hook's x position
self.y = game.fisherman.fishingLine.y + game.fisherman.fishingLine.height; // Stick to the hook's y position
}
};
});
var Fisherman = Container.expand(function () {
var self = Container.call(this);
// Attach fisherman asset
var fishermanGraphics = self.attachAsset('fisherman', {
anchorX: 0.5,
anchorY: 0.5
});
// Method to cast the fishing line
self.castLine = function () {
self.fishingLine = new FishingLine();
self.fishingLine.x = self.x;
self.fishingLine.y = self.y;
game.addChild(self.fishingLine);
};
});
var FishingLine = Container.expand(function () {
var self = Container.call(this);
// Attach fishing line asset
var lineGraphics = self.attachAsset('fishingLine', {
anchorX: 0.5,
anchorY: 0.0
});
// Update method to move the line downwards
self.update = function () {
self.y += 5; // Move the line down by 5 pixels per frame
// Stop the line just above the bottom of the screen
if (self.y >= 2100 - lineGraphics.height) {
self.y = 2100 - lineGraphics.height;
// Start moving the backgrounds once the fishing line has stopped moving
game.startBackgroundMovement = true;
}
// Check for collision with fish
game.children.forEach(function (child) {
if (child instanceof Fish && !child.lastIntersecting && self.intersects(child)) {
// Fish gets stuck to the base of the fishing hook and tilts 90 degrees upward
child.x = self.x;
child.y = self.y + lineGraphics.height;
child.rotation = Math.PI / 2; // Tilt the fish 90 degrees upward
child.stuckToHook = true; // Mark the fish as stuck to the hook
// Increase score when a fish is caught
score += 1;
updateScoreDisplay();
// Update lastIntersecting state
child.lastIntersecting = true;
}
});
};
});
var Shark = Container.expand(function () {
var self = Container.call(this);
var sharkGraphics = self.attachAsset('shark', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Move the shark horizontally
// Define the direction variable for shark movement
if (self.direction === undefined) {
self.direction = Math.random() > 0.5 ? 1 : -1;
}
// Move the shark horizontally based on its direction
self.x += 5 * self.direction;
// If the shark hits the edge of the screen, change direction
if (self.x <= 0 || self.x >= 2048 - sharkGraphics.width) {
self.direction *= -1;
// Flip the shark image horizontally when it changes direction
sharkGraphics.scale.x *= -1;
}
self.y -= 4; // Move the shark up along with the backgrounds
// Check if the shark's mouth touches the hook
if (self.intersects(game.fisherman.fishingLine)) {
// Eat all the fish on the hook
game.children.forEach(function (child) {
if (child instanceof Fish && child.stuckToHook) {
child.destroy();
}
});
// Reset the score to zero
score = 0;
updateScoreDisplay();
}
// Reset shark position if it goes off screen
if (self.x > 2048) {
self.x = -200;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize score display
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0); // Anchor to the top-right corner
LK.gui.topRight.addChild(scoreTxt);
// Initialize score variable
var score = 0;
// Function to update score display
function updateScoreDisplay() {
scoreTxt.setText(score);
}
game.move = function (x, y, obj) {
// Move the fishing line horizontally based on player finger position
// Smoothly move the fishing line horizontally based on player finger position
game.fisherman.fishingLine.x += (x - game.fisherman.fishingLine.x) * 0.1;
};
game.camera = {
x: 0,
y: 0
};
// Flag to control when the background should start moving
game.startBackgroundMovement = false;
// Update method to move backgrounds up and loop using the second background
game.update = function () {
if (game.startBackgroundMovement) {
// Add shark to the game if not already added
if (!game.shark) {
game.shark = new Shark();
game.shark.x = -200; // Start off-screen
game.shark.y = 150; // Position near the top
game.addChild(game.shark);
}
if (LK.ticks % 180 == 0) {
var fish = new Fish();
fish.x = Math.random() * 2048; // Spawn the fish at a random x position
fish.y = 2732; // Spawn the fish out of view
game.addChild(fish);
// Spawn shark in the same way as fish
var shark = new Shark();
shark.x = Math.random() * 2048; // Spawn the shark at a random x position
shark.y = 2732; // Spawn the shark out of view
game.addChild(shark);
}
// Rest of the code...
}
if (game.startBackgroundMovement) {
// Move all backgrounds up
game.background.y -= 4;
game.background2.y -= 4;
game.background3.y -= 4;
game.background4.y -= 4;
game.background5.y -= 4;
// Move the fisherman up
game.fisherman.y -= 4;
// Check if the second background is partially in view
if (game.background2.y <= -380) {
// Reset the second background to be below the previous background2
game.background2.y = game.background5.y + 832;
}
if (game.background3.y <= -380) {
// Reset the fourth background to be below the third background
game.background3.y = game.background2.y + 832;
}
if (game.background4.y <= -380) {
// Reset the fifth background to be below the fourth background
game.background4.y = game.background3.y + 832;
//game.background5.y = game.background4.y + 832;
}
if (game.background5.y <= -380) {
// Reset the fifth background to be below the fourth background
game.background5.y = game.background4.y + 832;
}
}
};
// Initialize the background position
var backgroundGraphics = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundGraphics.x = 1024; // Center horizontally
backgroundGraphics.y = 1366; // Center vertically
game.addChild(backgroundGraphics);
game.background = backgroundGraphics;
// Initialize the second background position
var backgroundGraphics2 = LK.getAsset('background2', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundGraphics2.x = 1024; // Center horizontally
backgroundGraphics2.y = 1366 + 1440; // Position below the first background
game.addChild(backgroundGraphics2);
game.background2 = backgroundGraphics2;
// Initialize the third background position
var backgroundGraphics3 = LK.getAsset('background3', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundGraphics3.x = 1024; // Center horizontally
backgroundGraphics3.y = 1366 + 1440 + 832; // Position below the second background
game.addChild(backgroundGraphics3);
game.background3 = backgroundGraphics3;
// Initialize the fourth background position
var backgroundGraphics4 = LK.getAsset('background4', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundGraphics4.x = 1024; // Center horizontally
backgroundGraphics4.y = 1366 + 1440 + 832 + 832; // Position below the third background
game.addChild(backgroundGraphics4);
game.background4 = backgroundGraphics4;
// Initialize the fifth background position
var backgroundGraphics5 = LK.getAsset('background5', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundGraphics5.x = 1024; // Center horizontally
backgroundGraphics5.y = 1366 + 1440 + 832 + 832 + 832; // Position below the fourth background
game.addChild(backgroundGraphics5);
game.background5 = backgroundGraphics5;
// Initialize the fisherman and add to the game
game.fisherman = new Fisherman();
game.fisherman.x = 1024; // Center horizontally
game.fisherman.y = 100; // Position near the top
game.addChild(game.fisherman);
// Cast the fishing line at the start of the game
game.fisherman.castLine();
; ===================================================================
--- original.js
+++ change.js
@@ -96,8 +96,10 @@
self.x += 5 * self.direction;
// If the shark hits the edge of the screen, change direction
if (self.x <= 0 || self.x >= 2048 - sharkGraphics.width) {
self.direction *= -1;
+ // Flip the shark image horizontally when it changes direction
+ sharkGraphics.scale.x *= -1;
}
self.y -= 4; // Move the shark up along with the backgrounds
// Check if the shark's mouth touches the hook
if (self.intersects(game.fisherman.fishingLine)) {
Fish. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Fishing hook. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Shark. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Man fishing on boat. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows