User prompt
make arrow controler
Code edit (1 edits merged)
Please save this source code
User prompt
Catch the Falling Stars
Initial prompt
Create a game called 'Catch the Falling Stars'. Stars fall randomly from the top of the screen. The player controls a basket at the bottom and moves it left and right to catch them. Each star gives points. If three stars are missed, the game ends.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Basket = Container.expand(function () {
var self = Container.call(this);
var basketGraphics = self.attachAsset('basket', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 3 + 2; // Random speed between 2-5
self.caught = false;
self.update = function () {
if (!self.caught) {
self.y += self.speed;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x001122
});
/****
* Game Code
****/
// Game variables
var basket;
var stars = [];
var missedStars = 0;
var starSpawnTimer = 0;
var dragNode = null;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var missedTxt = new Text2('Missed: 0/3', {
size: 60,
fill: 0xFF6666
});
missedTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(missedTxt);
// Create basket
basket = game.addChild(new Basket());
basket.x = 2048 / 2;
basket.y = 2732 - 150;
// Event handlers
game.down = function (x, y, obj) {
dragNode = basket;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
};
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = Math.max(100, Math.min(1948, x)); // Keep basket within screen bounds
}
}
game.move = handleMove;
// Main game update
game.update = function () {
// Spawn stars
starSpawnTimer++;
if (starSpawnTimer >= 60) {
// Spawn every second (60 ticks)
starSpawnTimer = Math.random() * 30; // Add some randomness to spawn timing
var newStar = new Star();
newStar.x = Math.random() * (2048 - 160) + 80; // Random position across screen width
newStar.y = -40;
newStar.lastY = newStar.y;
newStar.lastIntersecting = false;
stars.push(newStar);
game.addChild(newStar);
}
// Update stars
for (var i = stars.length - 1; i >= 0; i--) {
var star = stars[i];
// Initialize tracking variables if needed
if (star.lastY === undefined) star.lastY = star.y;
if (star.lastIntersecting === undefined) star.lastIntersecting = false;
// Check if star hit bottom of screen
if (star.lastY < 2732 + 50 && star.y >= 2732 + 50 && !star.caught) {
// Star missed
missedStars++;
missedTxt.setText('Missed: ' + missedStars + '/3');
LK.getSound('miss').play();
LK.effects.flashScreen(0xff0000, 500);
if (missedStars >= 3) {
LK.showGameOver();
return;
}
star.destroy();
stars.splice(i, 1);
continue;
}
// Check collision with basket
var currentIntersecting = star.intersects(basket);
if (!star.lastIntersecting && currentIntersecting && !star.caught) {
// Star caught
star.caught = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('catch').play();
LK.effects.flashObject(star, 0xffffff, 300);
// Animate star disappearing
tween(star, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
onFinish: function onFinish() {
star.destroy();
}
});
stars.splice(i, 1);
continue;
}
// Update tracking variables
star.lastY = star.y;
star.lastIntersecting = currentIntersecting;
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,143 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Basket = Container.expand(function () {
+ var self = Container.call(this);
+ var basketGraphics = self.attachAsset('basket', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ return self;
+});
+var Star = Container.expand(function () {
+ var self = Container.call(this);
+ var starGraphics = self.attachAsset('star', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = Math.random() * 3 + 2; // Random speed between 2-5
+ self.caught = false;
+ self.update = function () {
+ if (!self.caught) {
+ self.y += self.speed;
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x001122
+});
+
+/****
+* Game Code
+****/
+// Game variables
+var basket;
+var stars = [];
+var missedStars = 0;
+var starSpawnTimer = 0;
+var dragNode = null;
+// UI Elements
+var scoreTxt = new Text2('Score: 0', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+var missedTxt = new Text2('Missed: 0/3', {
+ size: 60,
+ fill: 0xFF6666
+});
+missedTxt.anchor.set(1, 0);
+LK.gui.topRight.addChild(missedTxt);
+// Create basket
+basket = game.addChild(new Basket());
+basket.x = 2048 / 2;
+basket.y = 2732 - 150;
+// Event handlers
+game.down = function (x, y, obj) {
+ dragNode = basket;
+ handleMove(x, y, obj);
+};
+game.up = function (x, y, obj) {
+ dragNode = null;
+};
+function handleMove(x, y, obj) {
+ if (dragNode) {
+ dragNode.x = Math.max(100, Math.min(1948, x)); // Keep basket within screen bounds
+ }
+}
+game.move = handleMove;
+// Main game update
+game.update = function () {
+ // Spawn stars
+ starSpawnTimer++;
+ if (starSpawnTimer >= 60) {
+ // Spawn every second (60 ticks)
+ starSpawnTimer = Math.random() * 30; // Add some randomness to spawn timing
+ var newStar = new Star();
+ newStar.x = Math.random() * (2048 - 160) + 80; // Random position across screen width
+ newStar.y = -40;
+ newStar.lastY = newStar.y;
+ newStar.lastIntersecting = false;
+ stars.push(newStar);
+ game.addChild(newStar);
+ }
+ // Update stars
+ for (var i = stars.length - 1; i >= 0; i--) {
+ var star = stars[i];
+ // Initialize tracking variables if needed
+ if (star.lastY === undefined) star.lastY = star.y;
+ if (star.lastIntersecting === undefined) star.lastIntersecting = false;
+ // Check if star hit bottom of screen
+ if (star.lastY < 2732 + 50 && star.y >= 2732 + 50 && !star.caught) {
+ // Star missed
+ missedStars++;
+ missedTxt.setText('Missed: ' + missedStars + '/3');
+ LK.getSound('miss').play();
+ LK.effects.flashScreen(0xff0000, 500);
+ if (missedStars >= 3) {
+ LK.showGameOver();
+ return;
+ }
+ star.destroy();
+ stars.splice(i, 1);
+ continue;
+ }
+ // Check collision with basket
+ var currentIntersecting = star.intersects(basket);
+ if (!star.lastIntersecting && currentIntersecting && !star.caught) {
+ // Star caught
+ star.caught = true;
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ LK.getSound('catch').play();
+ LK.effects.flashObject(star, 0xffffff, 300);
+ // Animate star disappearing
+ tween(star, {
+ alpha: 0,
+ scaleX: 2,
+ scaleY: 2
+ }, {
+ duration: 300,
+ onFinish: function onFinish() {
+ star.destroy();
+ }
+ });
+ stars.splice(i, 1);
+ continue;
+ }
+ // Update tracking variables
+ star.lastY = star.y;
+ star.lastIntersecting = currentIntersecting;
+ }
+};
\ No newline at end of file