Code edit (5 edits merged)
Please save this source code
User prompt
Make a fish class that swims randomly around the screen
Code edit (1 edits merged)
Please save this source code
Code edit (14 edits merged)
Please save this source code
User prompt
Make an add fish button on the bottom left
Code edit (1 edits merged)
Please save this source code
User prompt
Bowlfish
Initial prompt
Bowlfish: relaxing idle game about collecting fishes.
/****
* Classes
****/
var Button = Container.expand(function (onPress, size, text) {
var self = Container.call(this);
self.onPress = onPress;
self.isPressed = false;
var background = self.attachAsset('buttonBackground', {
width: size,
height: size
});
var backgroundPressed = self.attachAsset('buttonBackgroundPressed', {
visible: false,
width: size,
height: size
});
var buttonText = new Text2(text, {
size: size / 5,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = size / 2;
buttonText.y = size / 2;
self.addChild(buttonText);
self.down = function (x, y, obj) {
self.isPressed = true;
background.visible = false;
backgroundPressed.visible = true;
currentButton = self;
if (self.onPress) {
self.onPress();
}
};
self.up = function (x, y, obj) {
self.isPressed = false;
background.visible = true;
backgroundPressed.visible = false;
};
return self;
});
var Fish = Container.expand(function (fishType) {
var self = Container.call(this);
// Store fish type
self.fishType = fishType || 'blueFish';
// Create fish visual
var fishGraphics = self.attachAsset(self.fishType, {
anchorX: 0.5,
anchorY: 0.5
});
// Swimming properties
self.speed = 2 + Math.random() * 3; // Random speed between 2-5
self.direction = Math.random() * Math.PI * 2; // Random initial direction
self.turnSpeed = 0.02 + Math.random() * 0.03; // How fast fish can turn
self.targetDirection = self.direction;
self.wobbleAmount = 0.1 + Math.random() * 0.1; // Slight wobble for natural movement
self.wobbleSpeed = 0.05 + Math.random() * 0.05;
self.wobbleOffset = Math.random() * Math.PI * 2;
// Boundaries with margin
self.margin = 100;
// Initialize position
self.x = self.margin + Math.random() * (2048 - self.margin * 2);
self.y = self.margin + Math.random() * (2732 - self.margin * 2);
self.update = function () {
// Add wobble to movement
var wobble = Math.sin(LK.ticks * self.wobbleSpeed + self.wobbleOffset) * self.wobbleAmount;
// Gradually turn towards target direction
var directionDiff = self.targetDirection - self.direction;
// Normalize angle difference to -PI to PI
while (directionDiff > Math.PI) directionDiff -= Math.PI * 2;
while (directionDiff < -Math.PI) directionDiff += Math.PI * 2;
self.direction += directionDiff * self.turnSpeed;
// Move fish
self.x += Math.cos(self.direction + wobble) * self.speed;
self.y += Math.sin(self.direction + wobble) * self.speed;
// Face the direction of movement
fishGraphics.rotation = self.direction;
// Check boundaries and change direction
var turnAwayForce = 0.1;
var boundaryDistance = 200;
if (self.x < self.margin + boundaryDistance) {
self.targetDirection = 0; // Turn right
} else if (self.x > 2048 - self.margin - boundaryDistance) {
self.targetDirection = Math.PI; // Turn left
}
if (self.y < self.margin + boundaryDistance) {
self.targetDirection = Math.PI / 2; // Turn down
} else if (self.y > 2732 - self.margin - boundaryDistance) {
self.targetDirection = -Math.PI / 2; // Turn up
}
// Occasionally change direction randomly
if (Math.random() < 0.005) {
// 0.5% chance per frame
self.targetDirection = Math.random() * Math.PI * 2;
}
// Keep fish within bounds (hard limit)
self.x = Math.max(self.margin, Math.min(2048 - self.margin, self.x));
self.y = Math.max(self.margin, Math.min(2732 - self.margin, self.y));
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x83C7FF
});
/****
* Game Code
****/
game.attachAsset('waterBackground', {});
// Array to keep track of all fish
var fishes = [];
// Fish types available
var fishTypes = ['blueFish', 'greenFish', 'pinkFish', 'redFish'];
// Create add fish button
var addFishButton = new Button(function () {
// Create a new fish with random type
var randomType = fishTypes[Math.floor(Math.random() * fishTypes.length)];
var newFish = new Fish(randomType);
fishes.push(newFish);
game.addChild(newFish);
}, 250, "Add Fish");
// Position button in bottom left with margin
addFishButton.x = 2048 - 300;
addFishButton.y = 2732 - 300;
game.addChild(addFishButton);
// Game update loop
game.update = function () {
// All fish will update automatically through their own update methods
}; ===================================================================
--- original.js
+++ change.js
@@ -37,8 +37,68 @@
backgroundPressed.visible = false;
};
return self;
});
+var Fish = Container.expand(function (fishType) {
+ var self = Container.call(this);
+ // Store fish type
+ self.fishType = fishType || 'blueFish';
+ // Create fish visual
+ var fishGraphics = self.attachAsset(self.fishType, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Swimming properties
+ self.speed = 2 + Math.random() * 3; // Random speed between 2-5
+ self.direction = Math.random() * Math.PI * 2; // Random initial direction
+ self.turnSpeed = 0.02 + Math.random() * 0.03; // How fast fish can turn
+ self.targetDirection = self.direction;
+ self.wobbleAmount = 0.1 + Math.random() * 0.1; // Slight wobble for natural movement
+ self.wobbleSpeed = 0.05 + Math.random() * 0.05;
+ self.wobbleOffset = Math.random() * Math.PI * 2;
+ // Boundaries with margin
+ self.margin = 100;
+ // Initialize position
+ self.x = self.margin + Math.random() * (2048 - self.margin * 2);
+ self.y = self.margin + Math.random() * (2732 - self.margin * 2);
+ self.update = function () {
+ // Add wobble to movement
+ var wobble = Math.sin(LK.ticks * self.wobbleSpeed + self.wobbleOffset) * self.wobbleAmount;
+ // Gradually turn towards target direction
+ var directionDiff = self.targetDirection - self.direction;
+ // Normalize angle difference to -PI to PI
+ while (directionDiff > Math.PI) directionDiff -= Math.PI * 2;
+ while (directionDiff < -Math.PI) directionDiff += Math.PI * 2;
+ self.direction += directionDiff * self.turnSpeed;
+ // Move fish
+ self.x += Math.cos(self.direction + wobble) * self.speed;
+ self.y += Math.sin(self.direction + wobble) * self.speed;
+ // Face the direction of movement
+ fishGraphics.rotation = self.direction;
+ // Check boundaries and change direction
+ var turnAwayForce = 0.1;
+ var boundaryDistance = 200;
+ if (self.x < self.margin + boundaryDistance) {
+ self.targetDirection = 0; // Turn right
+ } else if (self.x > 2048 - self.margin - boundaryDistance) {
+ self.targetDirection = Math.PI; // Turn left
+ }
+ if (self.y < self.margin + boundaryDistance) {
+ self.targetDirection = Math.PI / 2; // Turn down
+ } else if (self.y > 2732 - self.margin - boundaryDistance) {
+ self.targetDirection = -Math.PI / 2; // Turn up
+ }
+ // Occasionally change direction randomly
+ if (Math.random() < 0.005) {
+ // 0.5% chance per frame
+ self.targetDirection = Math.random() * Math.PI * 2;
+ }
+ // Keep fish within bounds (hard limit)
+ self.x = Math.max(self.margin, Math.min(2048 - self.margin, self.x));
+ self.y = Math.max(self.margin, Math.min(2732 - self.margin, self.y));
+ };
+ return self;
+});
/****
* Initialize Game
****/
@@ -49,14 +109,24 @@
/****
* Game Code
****/
game.attachAsset('waterBackground', {});
-;
+// Array to keep track of all fish
+var fishes = [];
+// Fish types available
+var fishTypes = ['blueFish', 'greenFish', 'pinkFish', 'redFish'];
// Create add fish button
var addFishButton = new Button(function () {
- // Function to add a fish will be implemented when fish spawning is added
- console.log("Add fish button pressed");
-}, 400, "Add Fish");
+ // Create a new fish with random type
+ var randomType = fishTypes[Math.floor(Math.random() * fishTypes.length)];
+ var newFish = new Fish(randomType);
+ fishes.push(newFish);
+ game.addChild(newFish);
+}, 250, "Add Fish");
// Position button in bottom left with margin
-addFishButton.x = 2048 - 450;
-addFishButton.y = 2732 - 450;
-game.addChild(addFishButton);
\ No newline at end of file
+addFishButton.x = 2048 - 300;
+addFishButton.y = 2732 - 300;
+game.addChild(addFishButton);
+// Game update loop
+game.update = function () {
+ // All fish will update automatically through their own update methods
+};
\ No newline at end of file