User prompt
Fait en sorte que pas plus de 90 étoiles s'affichent en même temps dans le jeu, mais que de nouvelles étoiles soient générer des que le joueur les a toutes collectés/attrapes
User prompt
Je veux personnalisé le fond du jeu avec mon propre asset
User prompt
Ajoute une musique de fond en boucle au jeu
User prompt
Garde en tête cela : 📈 **Système de niveaux :** * Tu commences au **niveau 1** avec l’objectif de collecter **3 étoiles**. * À chaque niveau, le nombre d’étoiles à récupérer **double** par rapport au précédent. Modifie le jeu pour que cela corresponde à ça !
Code edit (1 edits merged)
Please save this source code
User prompt
Attrape les étoiles
Initial prompt
Crée ce jeu : ## ✨ Attrape les étoiles ✨ Bienvenue dans **Attrape les étoiles**, un jeu 2D léger et relaxant dans lequel tu incarnes un personnage qui explore un jardin magique à la recherche d’étoiles brillantes ! 🕹️ **Contrôles :** Utilise les boutons directionnels pour déplacer ton personnage dans toutes les directions (gauche, droite, haut, bas) et pars à la chasse aux étoiles qui apparaissent de façon aléatoire un peu partout dans le jardin 🌟 **Objectif :** Entre en collision avec les étoiles pour les attraper et gagner des points. Chaque étoile te rapproche du niveau suivant ! 📈 **Système de niveaux :** * Tu commences au **niveau 1** avec l’objectif de collecter **3 étoiles**. * À chaque niveau, le nombre d’étoiles à récupérer **double** par rapport au précédent. → Jusqu’où iras-tu ?!
/****
* 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
****/
// We need tween for smooth movement and button feedback
// --- Global variables ---
var player;
var stars = [];
var collected = 0;
var level = 1;
var starsToCollect = 1;
var scoreTxt;
var levelTxt;
var collectedTxt;
var dirButtons = {};
var gameActive = true;
// --- Asset initialization (shapes) ---
// --- 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) {
for (var i = 0; i < n; 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;
starsToCollect = Math.pow(2, lvl - 1);
collected = 0;
// Remove any existing stars
for (var i = 0; i < stars.length; i++) {
stars[i].destroy();
}
stars = [];
// Spawn new stars
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++;
collectedTxt.setText(collected + ' / ' + starsToCollect);
// Add to score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
// Level complete?
if (collected >= starsToCollect) {
winLevel();
}
}
}
};
// --- Start the game ---
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)
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,330 @@
-/****
+/****
+* 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: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x1a2a1a // Deep green for magical garden
+});
+
+/****
+* Game Code
+****/
+// We need tween for smooth movement and button feedback
+// --- Global variables ---
+var player;
+var stars = [];
+var collected = 0;
+var level = 1;
+var starsToCollect = 1;
+var scoreTxt;
+var levelTxt;
+var collectedTxt;
+var dirButtons = {};
+var gameActive = true;
+// --- Asset initialization (shapes) ---
+// --- 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) {
+ for (var i = 0; i < n; 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;
+ starsToCollect = Math.pow(2, lvl - 1);
+ collected = 0;
+ // Remove any existing stars
+ for (var i = 0; i < stars.length; i++) {
+ stars[i].destroy();
+ }
+ stars = [];
+ // Spawn new stars
+ 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++;
+ collectedTxt.setText(collected + ' / ' + starsToCollect);
+ // Add to score
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ // Level complete?
+ if (collected >= starsToCollect) {
+ winLevel();
+ }
+ }
+ }
+};
+// --- Start the game ---
+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)
+};
\ No newline at end of file