/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Directional button class var DirButton = Container.expand(function () { var self = Container.call(this); // Attach a semi-transparent gray ellipse as button var btnGfx = self.attachAsset('dirBtn', { anchorX: 0.5, anchorY: 0.5 }); btnGfx.width = 180; btnGfx.height = 180; btnGfx.color = 0x888888; btnGfx.alpha = 0.35; // Arrow indicator (using a box as a simple arrow) var arrow = self.attachAsset('arrowBox', { anchorX: 0.5, anchorY: 0.5 }); arrow.width = 60; arrow.height = 30; arrow.color = 0xffffff; arrow.alpha = 0.7; // Direction: 'up', 'down', 'left', 'right' self.dir = 'up'; // Set arrow rotation based on direction self.setDirection = function (dir) { self.dir = dir; if (dir === 'up') arrow.rotation = 0;else if (dir === 'down') arrow.rotation = Math.PI;else if (dir === 'left') arrow.rotation = -Math.PI / 2;else if (dir === 'right') arrow.rotation = Math.PI / 2; }; // Visual feedback on press self.flash = function () { tween(btnGfx, { alpha: 0.7 }, { duration: 80, onFinish: function onFinish() { tween(btnGfx, { alpha: 0.35 }, { duration: 120 }); } }); }; return self; }); // Player character class var Player = Container.expand(function () { var self = Container.call(this); // Attach a box asset for the player (red color, 120x120) var playerGfx = self.attachAsset('playerBox', { anchorX: 0.5, anchorY: 0.5 }); // Set initial size and color playerGfx.width = 120; playerGfx.height = 120; playerGfx.color = 0xd83318; // Movement speed in px per tick self.speed = 18; // Direction flags self.moveUp = false; self.moveDown = false; self.moveLeft = false; self.moveRight = false; // Update method called every tick self.update = function () { var dx = 0, dy = 0; if (self.moveUp) dy -= self.speed; if (self.moveDown) dy += self.speed; if (self.moveLeft) dx -= self.speed; if (self.moveRight) dx += self.speed; // Normalize diagonal movement if (dx !== 0 && dy !== 0) { dx *= 0.7071; dy *= 0.7071; } self.x += dx; self.y += dy; // Clamp to game area (player is 120x120, so keep inside 0..2048, 0..2732) if (self.x < 60) self.x = 60; if (self.x > 2048 - 60) self.x = 2048 - 60; if (self.y < 60) self.y = 60; if (self.y > 2732 - 60) self.y = 2732 - 60; }; return self; }); // Star collectible class var Star = Container.expand(function () { var self = Container.call(this); // Attach a yellow ellipse as the star var starGfx = self.attachAsset('starEllipse', { anchorX: 0.5, anchorY: 0.5 }); starGfx.width = 90; starGfx.height = 90; starGfx.color = 0xffe066; // For animation (twinkle) self.twinkleUp = true; self.twinkleTimer = 0; self.update = function () { // Twinkle effect: scale up and down self.twinkleTimer++; if (self.twinkleTimer % 30 === 0) { var targetScale = self.twinkleUp ? 1.2 : 1.0; tween(self, { scaleX: targetScale, scaleY: targetScale }, { duration: 200, easing: tween.easeInOut }); self.twinkleUp = !self.twinkleUp; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1a2a1a // Deep green for magical garden }); /**** * Game Code ****/ // --- Global variables --- // We need tween for smooth movement and button feedback // Custom background image asset (replace id with your own asset id) var player; var stars = []; var collected = 0; var level = 1; var starsToCollect = 3; var starsRemaining = 0; var scoreTxt; var levelTxt; var collectedTxt; var dirButtons = {}; var gameActive = true; // --- Asset initialization (shapes) --- // --- Add custom background image --- var bgImg = LK.getAsset('customBg', { anchorX: 0, anchorY: 0, x: 0, y: 0, width: 2048, height: 2732 }); game.addChild(bgImg); // --- UI Setup --- // Score text (top center) scoreTxt = new Text2('Score: 0', { size: 90, fill: 0xFFFBE6 }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Level text (top right) levelTxt = new Text2('Niveau: 1', { size: 70, fill: 0xB8E994 }); levelTxt.anchor.set(1, 0); LK.gui.topRight.addChild(levelTxt); // Collected/Total text (top left, but offset to avoid menu) collectedTxt = new Text2('0 / 1', { size: 70, fill: 0xFFE066 }); collectedTxt.anchor.set(0, 0); collectedTxt.x = 110; // Offset to avoid menu LK.gui.top.addChild(collectedTxt); // --- Player Setup --- player = new Player(); player.x = 2048 / 2; player.y = 2732 * 0.7; game.addChild(player); // --- Directional Buttons Setup (bottom left) --- var btnMargin = 40; var btnSize = 180; var baseX = btnMargin + btnSize / 2; var baseY = 2732 - btnMargin - btnSize / 2; // Up var btnUp = new DirButton(); btnUp.setDirection('up'); btnUp.x = baseX + btnSize; btnUp.y = baseY - btnSize; game.addChild(btnUp); dirButtons.up = btnUp; // Down var btnDown = new DirButton(); btnDown.setDirection('down'); btnDown.x = baseX + btnSize; btnDown.y = baseY + btnSize; game.addChild(btnDown); dirButtons.down = btnDown; // Left var btnLeft = new DirButton(); btnLeft.setDirection('left'); btnLeft.x = baseX; btnLeft.y = baseY; game.addChild(btnLeft); dirButtons.left = btnLeft; // Right var btnRight = new DirButton(); btnRight.setDirection('right'); btnRight.x = baseX + btnSize * 2; btnRight.y = baseY; game.addChild(btnRight); dirButtons.right = btnRight; // --- Button Event Handling --- function handleDirBtnDown(x, y, obj) { if (!gameActive) return; var btn = obj.target; btn.flash(); if (btn.dir === 'up') player.moveUp = true; if (btn.dir === 'down') player.moveDown = true; if (btn.dir === 'left') player.moveLeft = true; if (btn.dir === 'right') player.moveRight = true; } function handleDirBtnUp(x, y, obj) { var btn = obj.target; if (btn.dir === 'up') player.moveUp = false; if (btn.dir === 'down') player.moveDown = false; if (btn.dir === 'left') player.moveLeft = false; if (btn.dir === 'right') player.moveRight = false; } // Attach events to each button btnUp.down = handleDirBtnDown; btnUp.up = handleDirBtnUp; btnDown.down = handleDirBtnDown; btnDown.up = handleDirBtnUp; btnLeft.down = handleDirBtnDown; btnLeft.up = handleDirBtnUp; btnRight.down = handleDirBtnDown; btnRight.up = handleDirBtnUp; // --- Star Spawning --- function spawnStars(n) { // Limit to maximum 90 stars on screen var maxStars = 90; var starsToSpawn = Math.min(n, maxStars); for (var i = 0; i < starsToSpawn; i++) { var star = new Star(); // Place randomly, but not too close to player or edges var safeMargin = 160; var px = player.x, py = player.y; var tries = 0; do { star.x = safeMargin + Math.random() * (2048 - 2 * safeMargin); star.y = safeMargin + Math.random() * (2732 - 2 * safeMargin); tries++; } while (Math.abs(star.x - px) < 220 && Math.abs(star.y - py) < 220 && tries < 10); game.addChild(star); stars.push(star); } } // --- Level Progression --- function startLevel(lvl) { level = lvl; if (level === 1) { starsToCollect = 3; } else { starsToCollect = starsToCollect * 2; } collected = 0; starsRemaining = starsToCollect; // Remove any existing stars for (var i = 0; i < stars.length; i++) { stars[i].destroy(); } stars = []; // Spawn initial stars (max 90) spawnStars(starsToCollect); // Update UI levelTxt.setText('Niveau: ' + level); collectedTxt.setText(collected + ' / ' + starsToCollect); } // --- Game Over / Win --- function winLevel() { // Flash screen green LK.effects.flashScreen(0x83de44, 800); // Add score LK.setScore(LK.getScore() + starsToCollect); scoreTxt.setText('Score: ' + LK.getScore()); // Next level after short delay gameActive = false; LK.setTimeout(function () { gameActive = true; startLevel(level + 1); }, 1200); } // --- Main Game Update --- game.update = function () { if (!gameActive) return; // Update player player.update(); // Update stars for (var i = stars.length - 1; i >= 0; i--) { var star = stars[i]; star.update(); // Collision with player if (player.intersects(star)) { // Collect star LK.effects.flashObject(star, 0xffe066, 200); star.destroy(); stars.splice(i, 1); collected++; starsRemaining--; collectedTxt.setText(collected + ' / ' + starsToCollect); // Add to score LK.setScore(LK.getScore() + 1); scoreTxt.setText('Score: ' + LK.getScore()); // Level complete? if (collected >= starsToCollect) { winLevel(); } } } // Check if all stars on screen are collected but level not complete if (stars.length === 0 && collected < starsToCollect) { // Spawn more stars (remaining needed for level, max 90) var starsToSpawn = Math.min(starsRemaining, 90); spawnStars(starsToSpawn); } }; // --- Start the game --- // Initialize and play background music in a loop LK.playMusic('bgmusic'); LK.setScore(0); startLevel(1); // --- Touchscreen: stop movement when finger leaves button area --- game.up = function (x, y, obj) { // Stop all movement if finger lifted anywhere player.moveUp = false; player.moveDown = false; player.moveLeft = false; player.moveRight = false; }; // --- Prevent player from moving with drag (disable drag) --- game.down = function (x, y, obj) { // Do nothing (no drag) }; game.move = function (x, y, obj) { // Do nothing (no drag) };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Directional button class
var DirButton = Container.expand(function () {
var self = Container.call(this);
// Attach a semi-transparent gray ellipse as button
var btnGfx = self.attachAsset('dirBtn', {
anchorX: 0.5,
anchorY: 0.5
});
btnGfx.width = 180;
btnGfx.height = 180;
btnGfx.color = 0x888888;
btnGfx.alpha = 0.35;
// Arrow indicator (using a box as a simple arrow)
var arrow = self.attachAsset('arrowBox', {
anchorX: 0.5,
anchorY: 0.5
});
arrow.width = 60;
arrow.height = 30;
arrow.color = 0xffffff;
arrow.alpha = 0.7;
// Direction: 'up', 'down', 'left', 'right'
self.dir = 'up';
// Set arrow rotation based on direction
self.setDirection = function (dir) {
self.dir = dir;
if (dir === 'up') arrow.rotation = 0;else if (dir === 'down') arrow.rotation = Math.PI;else if (dir === 'left') arrow.rotation = -Math.PI / 2;else if (dir === 'right') arrow.rotation = Math.PI / 2;
};
// Visual feedback on press
self.flash = function () {
tween(btnGfx, {
alpha: 0.7
}, {
duration: 80,
onFinish: function onFinish() {
tween(btnGfx, {
alpha: 0.35
}, {
duration: 120
});
}
});
};
return self;
});
// Player character class
var Player = Container.expand(function () {
var self = Container.call(this);
// Attach a box asset for the player (red color, 120x120)
var playerGfx = self.attachAsset('playerBox', {
anchorX: 0.5,
anchorY: 0.5
});
// Set initial size and color
playerGfx.width = 120;
playerGfx.height = 120;
playerGfx.color = 0xd83318;
// Movement speed in px per tick
self.speed = 18;
// Direction flags
self.moveUp = false;
self.moveDown = false;
self.moveLeft = false;
self.moveRight = false;
// Update method called every tick
self.update = function () {
var dx = 0,
dy = 0;
if (self.moveUp) dy -= self.speed;
if (self.moveDown) dy += self.speed;
if (self.moveLeft) dx -= self.speed;
if (self.moveRight) dx += self.speed;
// Normalize diagonal movement
if (dx !== 0 && dy !== 0) {
dx *= 0.7071;
dy *= 0.7071;
}
self.x += dx;
self.y += dy;
// Clamp to game area (player is 120x120, so keep inside 0..2048, 0..2732)
if (self.x < 60) self.x = 60;
if (self.x > 2048 - 60) self.x = 2048 - 60;
if (self.y < 60) self.y = 60;
if (self.y > 2732 - 60) self.y = 2732 - 60;
};
return self;
});
// Star collectible class
var Star = Container.expand(function () {
var self = Container.call(this);
// Attach a yellow ellipse as the star
var starGfx = self.attachAsset('starEllipse', {
anchorX: 0.5,
anchorY: 0.5
});
starGfx.width = 90;
starGfx.height = 90;
starGfx.color = 0xffe066;
// For animation (twinkle)
self.twinkleUp = true;
self.twinkleTimer = 0;
self.update = function () {
// Twinkle effect: scale up and down
self.twinkleTimer++;
if (self.twinkleTimer % 30 === 0) {
var targetScale = self.twinkleUp ? 1.2 : 1.0;
tween(self, {
scaleX: targetScale,
scaleY: targetScale
}, {
duration: 200,
easing: tween.easeInOut
});
self.twinkleUp = !self.twinkleUp;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a2a1a // Deep green for magical garden
});
/****
* Game Code
****/
// --- Global variables ---
// We need tween for smooth movement and button feedback
// Custom background image asset (replace id with your own asset id)
var player;
var stars = [];
var collected = 0;
var level = 1;
var starsToCollect = 3;
var starsRemaining = 0;
var scoreTxt;
var levelTxt;
var collectedTxt;
var dirButtons = {};
var gameActive = true;
// --- Asset initialization (shapes) ---
// --- Add custom background image ---
var bgImg = LK.getAsset('customBg', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732
});
game.addChild(bgImg);
// --- UI Setup ---
// Score text (top center)
scoreTxt = new Text2('Score: 0', {
size: 90,
fill: 0xFFFBE6
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Level text (top right)
levelTxt = new Text2('Niveau: 1', {
size: 70,
fill: 0xB8E994
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
// Collected/Total text (top left, but offset to avoid menu)
collectedTxt = new Text2('0 / 1', {
size: 70,
fill: 0xFFE066
});
collectedTxt.anchor.set(0, 0);
collectedTxt.x = 110; // Offset to avoid menu
LK.gui.top.addChild(collectedTxt);
// --- Player Setup ---
player = new Player();
player.x = 2048 / 2;
player.y = 2732 * 0.7;
game.addChild(player);
// --- Directional Buttons Setup (bottom left) ---
var btnMargin = 40;
var btnSize = 180;
var baseX = btnMargin + btnSize / 2;
var baseY = 2732 - btnMargin - btnSize / 2;
// Up
var btnUp = new DirButton();
btnUp.setDirection('up');
btnUp.x = baseX + btnSize;
btnUp.y = baseY - btnSize;
game.addChild(btnUp);
dirButtons.up = btnUp;
// Down
var btnDown = new DirButton();
btnDown.setDirection('down');
btnDown.x = baseX + btnSize;
btnDown.y = baseY + btnSize;
game.addChild(btnDown);
dirButtons.down = btnDown;
// Left
var btnLeft = new DirButton();
btnLeft.setDirection('left');
btnLeft.x = baseX;
btnLeft.y = baseY;
game.addChild(btnLeft);
dirButtons.left = btnLeft;
// Right
var btnRight = new DirButton();
btnRight.setDirection('right');
btnRight.x = baseX + btnSize * 2;
btnRight.y = baseY;
game.addChild(btnRight);
dirButtons.right = btnRight;
// --- Button Event Handling ---
function handleDirBtnDown(x, y, obj) {
if (!gameActive) return;
var btn = obj.target;
btn.flash();
if (btn.dir === 'up') player.moveUp = true;
if (btn.dir === 'down') player.moveDown = true;
if (btn.dir === 'left') player.moveLeft = true;
if (btn.dir === 'right') player.moveRight = true;
}
function handleDirBtnUp(x, y, obj) {
var btn = obj.target;
if (btn.dir === 'up') player.moveUp = false;
if (btn.dir === 'down') player.moveDown = false;
if (btn.dir === 'left') player.moveLeft = false;
if (btn.dir === 'right') player.moveRight = false;
}
// Attach events to each button
btnUp.down = handleDirBtnDown;
btnUp.up = handleDirBtnUp;
btnDown.down = handleDirBtnDown;
btnDown.up = handleDirBtnUp;
btnLeft.down = handleDirBtnDown;
btnLeft.up = handleDirBtnUp;
btnRight.down = handleDirBtnDown;
btnRight.up = handleDirBtnUp;
// --- Star Spawning ---
function spawnStars(n) {
// Limit to maximum 90 stars on screen
var maxStars = 90;
var starsToSpawn = Math.min(n, maxStars);
for (var i = 0; i < starsToSpawn; i++) {
var star = new Star();
// Place randomly, but not too close to player or edges
var safeMargin = 160;
var px = player.x,
py = player.y;
var tries = 0;
do {
star.x = safeMargin + Math.random() * (2048 - 2 * safeMargin);
star.y = safeMargin + Math.random() * (2732 - 2 * safeMargin);
tries++;
} while (Math.abs(star.x - px) < 220 && Math.abs(star.y - py) < 220 && tries < 10);
game.addChild(star);
stars.push(star);
}
}
// --- Level Progression ---
function startLevel(lvl) {
level = lvl;
if (level === 1) {
starsToCollect = 3;
} else {
starsToCollect = starsToCollect * 2;
}
collected = 0;
starsRemaining = starsToCollect;
// Remove any existing stars
for (var i = 0; i < stars.length; i++) {
stars[i].destroy();
}
stars = [];
// Spawn initial stars (max 90)
spawnStars(starsToCollect);
// Update UI
levelTxt.setText('Niveau: ' + level);
collectedTxt.setText(collected + ' / ' + starsToCollect);
}
// --- Game Over / Win ---
function winLevel() {
// Flash screen green
LK.effects.flashScreen(0x83de44, 800);
// Add score
LK.setScore(LK.getScore() + starsToCollect);
scoreTxt.setText('Score: ' + LK.getScore());
// Next level after short delay
gameActive = false;
LK.setTimeout(function () {
gameActive = true;
startLevel(level + 1);
}, 1200);
}
// --- Main Game Update ---
game.update = function () {
if (!gameActive) return;
// Update player
player.update();
// Update stars
for (var i = stars.length - 1; i >= 0; i--) {
var star = stars[i];
star.update();
// Collision with player
if (player.intersects(star)) {
// Collect star
LK.effects.flashObject(star, 0xffe066, 200);
star.destroy();
stars.splice(i, 1);
collected++;
starsRemaining--;
collectedTxt.setText(collected + ' / ' + starsToCollect);
// Add to score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
// Level complete?
if (collected >= starsToCollect) {
winLevel();
}
}
}
// Check if all stars on screen are collected but level not complete
if (stars.length === 0 && collected < starsToCollect) {
// Spawn more stars (remaining needed for level, max 90)
var starsToSpawn = Math.min(starsRemaining, 90);
spawnStars(starsToSpawn);
}
};
// --- Start the game ---
// Initialize and play background music in a loop
LK.playMusic('bgmusic');
LK.setScore(0);
startLevel(1);
// --- Touchscreen: stop movement when finger leaves button area ---
game.up = function (x, y, obj) {
// Stop all movement if finger lifted anywhere
player.moveUp = false;
player.moveDown = false;
player.moveLeft = false;
player.moveRight = false;
};
// --- Prevent player from moving with drag (disable drag) ---
game.down = function (x, y, obj) {
// Do nothing (no drag)
};
game.move = function (x, y, obj) {
// Do nothing (no drag)
};