/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bomb = Container.expand(function () {
var self = Container.call(this);
var bombGraphics = self.attachAsset('bomb', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Crocodile = Container.expand(function () {
var self = Container.call(this);
var crocodileGraphics = self.attachAsset('crocodile', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 30;
self.getSpeed = function () {
return 30;
};
return self;
});
var Watermelon = Container.expand(function () {
var self = Container.call(this);
var watermelonGraphics = self.attachAsset('watermelon', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Create background layers
var forestBackground = LK.getAsset('forest', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
game.addChild(forestBackground);
var waterBackground = LK.getAsset('water', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 1366
});
game.addChild(waterBackground);
var crocodile;
var watermelons = [];
var bombs = [];
var dragNode = null;
var spawnTimer = 0;
var difficultyLevel = 1;
var baseSpawnRate = 60;
var gameState = 'menu'; // 'menu' or 'playing'
var menuContainer;
var titleText;
var startButton;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF,
fontWeight: 'bold'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create high score display
var highScoreTxt = new Text2('High Score: 0', {
size: 80,
fill: 0xFFFF00,
fontWeight: 'bold'
});
highScoreTxt.anchor.set(0.5, 0);
highScoreTxt.y = 160; // Position below main score
LK.gui.top.addChild(highScoreTxt);
// Initialize high score from storage
var highScore = storage.highScore || 0;
highScoreTxt.setText('High Score: ' + highScore);
// Create menu container
menuContainer = new Container();
game.addChild(menuContainer);
// Create title text
titleText = new Text2('CROCODILE CATCH', {
size: 180,
fill: 0xFF0000,
fontWeight: 'bold'
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 2732 / 2 - 300;
menuContainer.addChild(titleText);
// Create instruction text
var instructionText = new Text2('Tap to Start!', {
size: 140,
fill: 0xFF00FF,
fontWeight: 'bold'
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 2048 / 2;
instructionText.y = 2732 / 2 + 200;
menuContainer.addChild(instructionText);
// Initially hide score
scoreTxt.visible = false;
highScoreTxt.visible = false;
crocodile = game.addChild(new Crocodile());
crocodile.x = 2048 / 2;
crocodile.y = 2732 - 150;
crocodile.visible = false; // Hide crocodile in menu
function handleMove(x, y, obj) {
if (dragNode) {
var targetX = Math.max(210, Math.min(2048 - 210, x));
var speed = dragNode.getSpeed ? dragNode.getSpeed() : 20;
var deltaX = targetX - dragNode.x;
if (Math.abs(deltaX) > speed) {
dragNode.x += deltaX > 0 ? speed : -speed;
} else {
dragNode.x = targetX;
}
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
if (gameState === 'menu') {
// Start the game
gameState = 'playing';
menuContainer.visible = false;
crocodile.visible = true;
scoreTxt.visible = true;
highScoreTxt.visible = true;
// Play background music on loop
LK.playMusic('Bosque', {
loop: true
});
} else if (gameState === 'playing') {
dragNode = crocodile;
handleMove(x, y, obj);
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
function spawnWatermelon() {
var watermelon = new Watermelon();
watermelon.x = Math.random() * (2048 - 360) + 180;
watermelon.y = -60;
watermelon.lastY = watermelon.y;
watermelon.lastIntersecting = false;
watermelons.push(watermelon);
game.addChild(watermelon);
}
function spawnBomb() {
var bomb = new Bomb();
bomb.x = Math.random() * (2048 - 400) + 200;
bomb.y = -75;
bomb.lastY = bomb.y;
bomb.lastIntersecting = false;
bombs.push(bomb);
game.addChild(bomb);
}
game.update = function () {
// Only run game logic when actually playing
if (gameState !== 'playing') {
return;
}
spawnTimer++;
difficultyLevel = Math.floor(LK.getScore() / 5) + 1;
var currentSpawnRate = Math.max(30, baseSpawnRate - difficultyLevel * 3);
if (spawnTimer >= currentSpawnRate) {
spawnTimer = 0;
if (Math.random() < 0.5) {
spawnWatermelon();
} else {
spawnBomb();
}
}
for (var i = watermelons.length - 1; i >= 0; i--) {
var watermelon = watermelons[i];
if (watermelon.lastY === undefined) watermelon.lastY = watermelon.y;
if (watermelon.lastIntersecting === undefined) watermelon.lastIntersecting = false;
if (watermelon.lastY < 2732 + 50 && watermelon.y >= 2732 + 50) {
watermelon.destroy();
watermelons.splice(i, 1);
continue;
}
var currentIntersecting = watermelon.intersects(crocodile);
if (!watermelon.lastIntersecting && currentIntersecting) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
// Check and update high score
if (LK.getScore() > highScore) {
highScore = LK.getScore();
storage.highScore = highScore;
highScoreTxt.setText('High Score: ' + highScore);
}
LK.effects.flashObject(crocodile, 0x00FF00, 300);
LK.getSound('eat').play();
watermelon.destroy();
watermelons.splice(i, 1);
continue;
}
watermelon.lastY = watermelon.y;
watermelon.lastIntersecting = currentIntersecting;
}
for (var j = bombs.length - 1; j >= 0; j--) {
var bomb = bombs[j];
if (bomb.lastY === undefined) bomb.lastY = bomb.y;
if (bomb.lastIntersecting === undefined) bomb.lastIntersecting = false;
if (bomb.lastY < 2732 + 50 && bomb.y >= 2732 + 50) {
bomb.destroy();
bombs.splice(j, 1);
continue;
}
var currentIntersecting = bomb.intersects(crocodile);
if (!bomb.lastIntersecting && currentIntersecting) {
LK.effects.flashScreen(0xFF0000, 1000);
LK.getSound('explode').play();
LK.showGameOver();
return;
}
bomb.lastY = bomb.y;
bomb.lastIntersecting = currentIntersecting;
}
for (var w = 0; w < watermelons.length; w++) {
watermelons[w].speed = 10 + LK.getScore() * 0.3;
}
for (var b = 0; b < bombs.length; b++) {
bombs[b].speed = 12 + LK.getScore() * 0.3;
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bomb = Container.expand(function () {
var self = Container.call(this);
var bombGraphics = self.attachAsset('bomb', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Crocodile = Container.expand(function () {
var self = Container.call(this);
var crocodileGraphics = self.attachAsset('crocodile', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 30;
self.getSpeed = function () {
return 30;
};
return self;
});
var Watermelon = Container.expand(function () {
var self = Container.call(this);
var watermelonGraphics = self.attachAsset('watermelon', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Create background layers
var forestBackground = LK.getAsset('forest', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
game.addChild(forestBackground);
var waterBackground = LK.getAsset('water', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 1366
});
game.addChild(waterBackground);
var crocodile;
var watermelons = [];
var bombs = [];
var dragNode = null;
var spawnTimer = 0;
var difficultyLevel = 1;
var baseSpawnRate = 60;
var gameState = 'menu'; // 'menu' or 'playing'
var menuContainer;
var titleText;
var startButton;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF,
fontWeight: 'bold'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create high score display
var highScoreTxt = new Text2('High Score: 0', {
size: 80,
fill: 0xFFFF00,
fontWeight: 'bold'
});
highScoreTxt.anchor.set(0.5, 0);
highScoreTxt.y = 160; // Position below main score
LK.gui.top.addChild(highScoreTxt);
// Initialize high score from storage
var highScore = storage.highScore || 0;
highScoreTxt.setText('High Score: ' + highScore);
// Create menu container
menuContainer = new Container();
game.addChild(menuContainer);
// Create title text
titleText = new Text2('CROCODILE CATCH', {
size: 180,
fill: 0xFF0000,
fontWeight: 'bold'
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 2732 / 2 - 300;
menuContainer.addChild(titleText);
// Create instruction text
var instructionText = new Text2('Tap to Start!', {
size: 140,
fill: 0xFF00FF,
fontWeight: 'bold'
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 2048 / 2;
instructionText.y = 2732 / 2 + 200;
menuContainer.addChild(instructionText);
// Initially hide score
scoreTxt.visible = false;
highScoreTxt.visible = false;
crocodile = game.addChild(new Crocodile());
crocodile.x = 2048 / 2;
crocodile.y = 2732 - 150;
crocodile.visible = false; // Hide crocodile in menu
function handleMove(x, y, obj) {
if (dragNode) {
var targetX = Math.max(210, Math.min(2048 - 210, x));
var speed = dragNode.getSpeed ? dragNode.getSpeed() : 20;
var deltaX = targetX - dragNode.x;
if (Math.abs(deltaX) > speed) {
dragNode.x += deltaX > 0 ? speed : -speed;
} else {
dragNode.x = targetX;
}
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
if (gameState === 'menu') {
// Start the game
gameState = 'playing';
menuContainer.visible = false;
crocodile.visible = true;
scoreTxt.visible = true;
highScoreTxt.visible = true;
// Play background music on loop
LK.playMusic('Bosque', {
loop: true
});
} else if (gameState === 'playing') {
dragNode = crocodile;
handleMove(x, y, obj);
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
function spawnWatermelon() {
var watermelon = new Watermelon();
watermelon.x = Math.random() * (2048 - 360) + 180;
watermelon.y = -60;
watermelon.lastY = watermelon.y;
watermelon.lastIntersecting = false;
watermelons.push(watermelon);
game.addChild(watermelon);
}
function spawnBomb() {
var bomb = new Bomb();
bomb.x = Math.random() * (2048 - 400) + 200;
bomb.y = -75;
bomb.lastY = bomb.y;
bomb.lastIntersecting = false;
bombs.push(bomb);
game.addChild(bomb);
}
game.update = function () {
// Only run game logic when actually playing
if (gameState !== 'playing') {
return;
}
spawnTimer++;
difficultyLevel = Math.floor(LK.getScore() / 5) + 1;
var currentSpawnRate = Math.max(30, baseSpawnRate - difficultyLevel * 3);
if (spawnTimer >= currentSpawnRate) {
spawnTimer = 0;
if (Math.random() < 0.5) {
spawnWatermelon();
} else {
spawnBomb();
}
}
for (var i = watermelons.length - 1; i >= 0; i--) {
var watermelon = watermelons[i];
if (watermelon.lastY === undefined) watermelon.lastY = watermelon.y;
if (watermelon.lastIntersecting === undefined) watermelon.lastIntersecting = false;
if (watermelon.lastY < 2732 + 50 && watermelon.y >= 2732 + 50) {
watermelon.destroy();
watermelons.splice(i, 1);
continue;
}
var currentIntersecting = watermelon.intersects(crocodile);
if (!watermelon.lastIntersecting && currentIntersecting) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
// Check and update high score
if (LK.getScore() > highScore) {
highScore = LK.getScore();
storage.highScore = highScore;
highScoreTxt.setText('High Score: ' + highScore);
}
LK.effects.flashObject(crocodile, 0x00FF00, 300);
LK.getSound('eat').play();
watermelon.destroy();
watermelons.splice(i, 1);
continue;
}
watermelon.lastY = watermelon.y;
watermelon.lastIntersecting = currentIntersecting;
}
for (var j = bombs.length - 1; j >= 0; j--) {
var bomb = bombs[j];
if (bomb.lastY === undefined) bomb.lastY = bomb.y;
if (bomb.lastIntersecting === undefined) bomb.lastIntersecting = false;
if (bomb.lastY < 2732 + 50 && bomb.y >= 2732 + 50) {
bomb.destroy();
bombs.splice(j, 1);
continue;
}
var currentIntersecting = bomb.intersects(crocodile);
if (!bomb.lastIntersecting && currentIntersecting) {
LK.effects.flashScreen(0xFF0000, 1000);
LK.getSound('explode').play();
LK.showGameOver();
return;
}
bomb.lastY = bomb.y;
bomb.lastIntersecting = currentIntersecting;
}
for (var w = 0; w < watermelons.length; w++) {
watermelons[w].speed = 10 + LK.getScore() * 0.3;
}
for (var b = 0; b < bombs.length; b++) {
bombs[b].speed = 12 + LK.getScore() * 0.3;
}
};