User prompt
Oye le puedes poner un fondo así como de un bosque
Code edit (1 edits merged)
Please save this source code
User prompt
Josh's Trail - Find Your Owner
Initial prompt
Se va a tratar de un perro llamado Josh que tiene que encontrar a su dueño y la única pista que tiene es rastro de palomitas de maíz
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Josh = Container.expand(function () {
var self = Container.call(this);
var joshGraphics = self.attachAsset('josh', {
anchorX: 0.5,
anchorY: 0.5
});
self.isMoving = false;
self.targetX = 0;
self.targetY = 0;
self.speed = 8;
self.moveTo = function (x, y) {
if (!self.isMoving) {
self.targetX = x;
self.targetY = y;
self.isMoving = true;
var distance = Math.sqrt(Math.pow(x - self.x, 2) + Math.pow(y - self.y, 2));
var duration = distance / self.speed * 16.67; // 60fps timing
tween(self, {
x: x,
y: y
}, {
duration: duration,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isMoving = false;
}
});
}
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.moveSpeed = 2;
self.direction = 1;
self.startX = 0;
self.range = 200;
self.update = function () {
self.x += self.moveSpeed * self.direction;
if (self.x > self.startX + self.range || self.x < self.startX - self.range) {
self.direction *= -1;
}
};
return self;
});
var Popcorn = Container.expand(function () {
var self = Container.call(this);
var popcornGraphics = self.attachAsset('popcorn', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.collect = function () {
if (!self.collected) {
self.collected = true;
tween(self, {
scaleX: 1.5,
scaleY: 1.5,
alpha: 0
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.destroy();
}
});
}
};
return self;
});
var Treat = Container.expand(function () {
var self = Container.call(this);
var treatGraphics = self.attachAsset('treat', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.points = 50;
self.collect = function () {
if (!self.collected) {
self.collected = true;
tween(self, {
scaleX: 2,
scaleY: 2,
alpha: 0
}, {
duration: 400,
easing: tween.bounceOut,
onFinish: function onFinish() {
self.destroy();
}
});
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x90EE90
});
/****
* Game Code
****/
// Game state variables
var josh;
var popcornKernels = [];
var obstacles = [];
var treats = [];
var level = 1;
var popcornCollected = 0;
var popcornNeeded = 5;
var gameWon = false;
var lastJoshIntersecting = {};
// UI elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0x2F4F2F
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
var levelTxt = new Text2('Level: 1', {
size: 80,
fill: 0x2F4F2F
});
levelTxt.anchor.set(0, 0);
levelTxt.x = -300;
LK.gui.topRight.addChild(levelTxt);
var instructionTxt = new Text2('Tap to guide Josh to the popcorn!', {
size: 60,
fill: 0x8B4513
});
instructionTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionTxt);
instructionTxt.y = 150;
// Initialize Josh
josh = game.addChild(new Josh());
josh.x = 200;
josh.y = 2400;
// Generate initial popcorn trail
function generatePopcornTrail() {
// Clear existing popcorn
for (var i = popcornKernels.length - 1; i >= 0; i--) {
if (!popcornKernels[i].collected) {
popcornKernels[i].destroy();
}
popcornKernels.splice(i, 1);
}
var startX = josh.x;
var startY = josh.y;
var segments = 8;
for (var i = 0; i < segments; i++) {
var popcorn = new Popcorn();
var angle = (Math.random() - 0.5) * Math.PI * 0.5;
var distance = 200 + Math.random() * 150;
popcorn.x = startX + Math.cos(angle) * distance;
popcorn.y = startY - distance * 0.8 - i * 100;
// Keep within bounds
popcorn.x = Math.max(100, Math.min(1948, popcorn.x));
popcorn.y = Math.max(200, Math.min(2600, popcorn.y));
popcornKernels.push(popcorn);
game.addChild(popcorn);
startX = popcorn.x;
startY = popcorn.y;
}
}
// Generate obstacles
function generateObstacles() {
// Clear existing obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
var numObstacles = 3 + level;
for (var i = 0; i < numObstacles; i++) {
var obstacle = new Obstacle();
obstacle.x = 300 + Math.random() * 1400;
obstacle.y = 800 + Math.random() * 1200;
obstacle.startX = obstacle.x;
obstacle.moveSpeed = 1 + Math.random() * 2;
obstacles.push(obstacle);
game.addChild(obstacle);
// Initialize last intersecting state
lastJoshIntersecting[obstacle] = false;
}
}
// Generate treats
function generateTreats() {
// Clear existing treats
for (var i = treats.length - 1; i >= 0; i--) {
if (!treats[i].collected) {
treats[i].destroy();
}
treats.splice(i, 1);
}
var numTreats = 2 + Math.floor(level / 2);
for (var i = 0; i < numTreats; i++) {
var treat = new Treat();
treat.x = 200 + Math.random() * 1648;
treat.y = 400 + Math.random() * 1800;
treats.push(treat);
game.addChild(treat);
// Initialize last intersecting state
lastJoshIntersecting[treat] = false;
}
}
// Initialize level
function initializeLevel() {
popcornCollected = 0;
popcornNeeded = 5 + level * 2;
generatePopcornTrail();
generateObstacles();
generateTreats();
levelTxt.setText('Level: ' + level);
}
// Start first level
initializeLevel();
// Game input handlers
game.down = function (x, y, obj) {
if (!josh.isMoving && !gameWon) {
josh.moveTo(x, y);
if (Math.random() < 0.3) {
LK.getSound('bark').play();
}
}
};
// Main game loop
game.update = function () {
if (gameWon) return;
// Check popcorn collection
for (var i = popcornKernels.length - 1; i >= 0; i--) {
var popcorn = popcornKernels[i];
if (!popcorn.collected && josh.intersects(popcorn)) {
popcorn.collect();
popcornCollected++;
LK.setScore(LK.getScore() + 100);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('collect').play();
// Check level completion
if (popcornCollected >= popcornNeeded) {
level++;
if (level > 5) {
gameWon = true;
LK.showYouWin();
} else {
// Flash screen green for level complete
LK.effects.flashScreen(0x00FF00, 500);
LK.setTimeout(function () {
initializeLevel();
}, 600);
}
}
}
}
// Check treat collection
for (var i = treats.length - 1; i >= 0; i--) {
var treat = treats[i];
if (lastJoshIntersecting[treat] === undefined) {
lastJoshIntersecting[treat] = false;
}
var currentIntersecting = josh.intersects(treat);
if (!lastJoshIntersecting[treat] && currentIntersecting && !treat.collected) {
treat.collect();
LK.setScore(LK.getScore() + treat.points);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('bonus').play();
}
lastJoshIntersecting[treat] = currentIntersecting;
}
// Check obstacle collisions
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
if (lastJoshIntersecting[obstacle] === undefined) {
lastJoshIntersecting[obstacle] = false;
}
var currentIntersecting = josh.intersects(obstacle);
if (!lastJoshIntersecting[obstacle] && currentIntersecting) {
// Flash screen red and show game over
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
return;
}
lastJoshIntersecting[obstacle] = currentIntersecting;
}
// Update score display
scoreTxt.setText('Score: ' + LK.getScore());
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,302 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Josh = Container.expand(function () {
+ var self = Container.call(this);
+ var joshGraphics = self.attachAsset('josh', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.isMoving = false;
+ self.targetX = 0;
+ self.targetY = 0;
+ self.speed = 8;
+ self.moveTo = function (x, y) {
+ if (!self.isMoving) {
+ self.targetX = x;
+ self.targetY = y;
+ self.isMoving = true;
+ var distance = Math.sqrt(Math.pow(x - self.x, 2) + Math.pow(y - self.y, 2));
+ var duration = distance / self.speed * 16.67; // 60fps timing
+ tween(self, {
+ x: x,
+ y: y
+ }, {
+ duration: duration,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ self.isMoving = false;
+ }
+ });
+ }
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var obstacleGraphics = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.moveSpeed = 2;
+ self.direction = 1;
+ self.startX = 0;
+ self.range = 200;
+ self.update = function () {
+ self.x += self.moveSpeed * self.direction;
+ if (self.x > self.startX + self.range || self.x < self.startX - self.range) {
+ self.direction *= -1;
+ }
+ };
+ return self;
+});
+var Popcorn = Container.expand(function () {
+ var self = Container.call(this);
+ var popcornGraphics = self.attachAsset('popcorn', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.collected = false;
+ self.collect = function () {
+ if (!self.collected) {
+ self.collected = true;
+ tween(self, {
+ scaleX: 1.5,
+ scaleY: 1.5,
+ alpha: 0
+ }, {
+ duration: 300,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ self.destroy();
+ }
+ });
+ }
+ };
+ return self;
+});
+var Treat = Container.expand(function () {
+ var self = Container.call(this);
+ var treatGraphics = self.attachAsset('treat', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.collected = false;
+ self.points = 50;
+ self.collect = function () {
+ if (!self.collected) {
+ self.collected = true;
+ tween(self, {
+ scaleX: 2,
+ scaleY: 2,
+ alpha: 0
+ }, {
+ duration: 400,
+ easing: tween.bounceOut,
+ onFinish: function onFinish() {
+ self.destroy();
+ }
+ });
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x90EE90
+});
+
+/****
+* Game Code
+****/
+// Game state variables
+var josh;
+var popcornKernels = [];
+var obstacles = [];
+var treats = [];
+var level = 1;
+var popcornCollected = 0;
+var popcornNeeded = 5;
+var gameWon = false;
+var lastJoshIntersecting = {};
+// UI elements
+var scoreTxt = new Text2('Score: 0', {
+ size: 80,
+ fill: 0x2F4F2F
+});
+scoreTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(scoreTxt);
+var levelTxt = new Text2('Level: 1', {
+ size: 80,
+ fill: 0x2F4F2F
+});
+levelTxt.anchor.set(0, 0);
+levelTxt.x = -300;
+LK.gui.topRight.addChild(levelTxt);
+var instructionTxt = new Text2('Tap to guide Josh to the popcorn!', {
+ size: 60,
+ fill: 0x8B4513
+});
+instructionTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(instructionTxt);
+instructionTxt.y = 150;
+// Initialize Josh
+josh = game.addChild(new Josh());
+josh.x = 200;
+josh.y = 2400;
+// Generate initial popcorn trail
+function generatePopcornTrail() {
+ // Clear existing popcorn
+ for (var i = popcornKernels.length - 1; i >= 0; i--) {
+ if (!popcornKernels[i].collected) {
+ popcornKernels[i].destroy();
+ }
+ popcornKernels.splice(i, 1);
+ }
+ var startX = josh.x;
+ var startY = josh.y;
+ var segments = 8;
+ for (var i = 0; i < segments; i++) {
+ var popcorn = new Popcorn();
+ var angle = (Math.random() - 0.5) * Math.PI * 0.5;
+ var distance = 200 + Math.random() * 150;
+ popcorn.x = startX + Math.cos(angle) * distance;
+ popcorn.y = startY - distance * 0.8 - i * 100;
+ // Keep within bounds
+ popcorn.x = Math.max(100, Math.min(1948, popcorn.x));
+ popcorn.y = Math.max(200, Math.min(2600, popcorn.y));
+ popcornKernels.push(popcorn);
+ game.addChild(popcorn);
+ startX = popcorn.x;
+ startY = popcorn.y;
+ }
+}
+// Generate obstacles
+function generateObstacles() {
+ // Clear existing obstacles
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ obstacles[i].destroy();
+ obstacles.splice(i, 1);
+ }
+ var numObstacles = 3 + level;
+ for (var i = 0; i < numObstacles; i++) {
+ var obstacle = new Obstacle();
+ obstacle.x = 300 + Math.random() * 1400;
+ obstacle.y = 800 + Math.random() * 1200;
+ obstacle.startX = obstacle.x;
+ obstacle.moveSpeed = 1 + Math.random() * 2;
+ obstacles.push(obstacle);
+ game.addChild(obstacle);
+ // Initialize last intersecting state
+ lastJoshIntersecting[obstacle] = false;
+ }
+}
+// Generate treats
+function generateTreats() {
+ // Clear existing treats
+ for (var i = treats.length - 1; i >= 0; i--) {
+ if (!treats[i].collected) {
+ treats[i].destroy();
+ }
+ treats.splice(i, 1);
+ }
+ var numTreats = 2 + Math.floor(level / 2);
+ for (var i = 0; i < numTreats; i++) {
+ var treat = new Treat();
+ treat.x = 200 + Math.random() * 1648;
+ treat.y = 400 + Math.random() * 1800;
+ treats.push(treat);
+ game.addChild(treat);
+ // Initialize last intersecting state
+ lastJoshIntersecting[treat] = false;
+ }
+}
+// Initialize level
+function initializeLevel() {
+ popcornCollected = 0;
+ popcornNeeded = 5 + level * 2;
+ generatePopcornTrail();
+ generateObstacles();
+ generateTreats();
+ levelTxt.setText('Level: ' + level);
+}
+// Start first level
+initializeLevel();
+// Game input handlers
+game.down = function (x, y, obj) {
+ if (!josh.isMoving && !gameWon) {
+ josh.moveTo(x, y);
+ if (Math.random() < 0.3) {
+ LK.getSound('bark').play();
+ }
+ }
+};
+// Main game loop
+game.update = function () {
+ if (gameWon) return;
+ // Check popcorn collection
+ for (var i = popcornKernels.length - 1; i >= 0; i--) {
+ var popcorn = popcornKernels[i];
+ if (!popcorn.collected && josh.intersects(popcorn)) {
+ popcorn.collect();
+ popcornCollected++;
+ LK.setScore(LK.getScore() + 100);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ LK.getSound('collect').play();
+ // Check level completion
+ if (popcornCollected >= popcornNeeded) {
+ level++;
+ if (level > 5) {
+ gameWon = true;
+ LK.showYouWin();
+ } else {
+ // Flash screen green for level complete
+ LK.effects.flashScreen(0x00FF00, 500);
+ LK.setTimeout(function () {
+ initializeLevel();
+ }, 600);
+ }
+ }
+ }
+ }
+ // Check treat collection
+ for (var i = treats.length - 1; i >= 0; i--) {
+ var treat = treats[i];
+ if (lastJoshIntersecting[treat] === undefined) {
+ lastJoshIntersecting[treat] = false;
+ }
+ var currentIntersecting = josh.intersects(treat);
+ if (!lastJoshIntersecting[treat] && currentIntersecting && !treat.collected) {
+ treat.collect();
+ LK.setScore(LK.getScore() + treat.points);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ LK.getSound('bonus').play();
+ }
+ lastJoshIntersecting[treat] = currentIntersecting;
+ }
+ // Check obstacle collisions
+ for (var i = 0; i < obstacles.length; i++) {
+ var obstacle = obstacles[i];
+ if (lastJoshIntersecting[obstacle] === undefined) {
+ lastJoshIntersecting[obstacle] = false;
+ }
+ var currentIntersecting = josh.intersects(obstacle);
+ if (!lastJoshIntersecting[obstacle] && currentIntersecting) {
+ // Flash screen red and show game over
+ LK.effects.flashScreen(0xFF0000, 1000);
+ LK.showGameOver();
+ return;
+ }
+ lastJoshIntersecting[obstacle] = currentIntersecting;
+ }
+ // Update score display
+ scoreTxt.setText('Score: ' + LK.getScore());
+};
\ No newline at end of file