User prompt
scroll horizontal stripes with shades of green to represent the grass ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
flash the screen when a bug is clicked ↪💡 Consider importing and using the following plugins: @upit/tween.v1
Code edit (1 edits merged)
Please save this source code
User prompt
when game over, display a white text below score with the reason of death
User prompt
collision with tree means colliding with its trunk, not its crown
User prompt
ignore collisions with animated bugs
User prompt
complete the TODO inside game.down
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: touchPosition is not defined' in or related to this line: 'var dx = touchPosition.x - self.x;' Line Number: 87
Code edit (1 edits merged)
Please save this source code
User prompt
change so that the player can tap to a position and the car moves at constant speed towards that point without needing to hold, but the car won't move if the touch point is a bug
User prompt
touching a bug needs to set "isTouching" to false so that the car won't move in the bugs direction
Code edit (1 edits merged)
Please save this source code
User prompt
Bit darker background color
User prompt
Make the background color as grass green
User prompt
Add a new type of insect that scrolls down twice as fast, make it appear less frequently
User prompt
Occasionally, a tree obstacle must scroll from the top. When it hits the jeep, it’s game over
User prompt
AnimatedBug must fly diagonally to a random side, three times as fast
User prompt
When a insect is clicked, replace it with an AnimatedBug that flies to a random top side of the screen and disappears
User prompt
When an insect is clicked, the score increments and the insect disappears with an animation to random top corner
User prompt
Add insects that appear from the top and travel to the bottom. When they collide with jeep, it’s game over
User prompt
Please fix the bug: 'ReferenceError: Can't find variable: bugs' in or related to this line: 'for (var i = 0; i < bugs.length; i++) {' Line Number: 28
User prompt
Remove all the bug appearances and leave just the jeep code
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'if (bugs[i].intersects(jeep)) {' Line Number: 149
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'if (bugs[i].intersects(jeep)) {' Line Number: 149
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// AnimatedBug class to represent the bug that flies to the top of the screen
var AnimatedBug = Container.expand(function () {
var self = Container.call(this);
var bugGraphics = self.attachAsset('bug', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Determine the direction to fly to (left or right)
var direction = Math.random() < 0.5 ? -1 : 1;
// Update the x and y position
self.x += self.speed * direction * 3;
self.y -= self.speed * 3;
// Destroy the bug when it flies off the screen
if (self.y < 0 || self.x < 0 || self.x > 2048) {
self.destroy();
}
};
});
var Bug = Container.expand(function () {
var self = Container.call(this);
var bugGraphics = self.attachAsset('bug', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
self.down = function (x, y, obj) {
// Increment score
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText('Score: ' + LK.getScore());
// Replace bug with AnimatedBug
var animatedBug = game.addChild(new AnimatedBug());
animatedBug.x = self.x;
animatedBug.y = self.y;
isTouching = false; // Prevent car from moving when touching a bug
self.destroy();
};
});
// FastBug class to represent the fast bug that scrolls down the screen
var FastBug = Container.expand(function () {
var self = Container.call(this);
var bugGraphics = self.attachAsset('fastBug', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
};
self.down = function (x, y, obj) {
// Increment score
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText('Score: ' + LK.getScore());
// Replace bug with AnimatedBug
var animatedBug = game.addChild(new AnimatedBug());
animatedBug.x = self.x;
animatedBug.y = self.y;
isTouching = false; // Prevent car from moving when touching a bug
self.destroy();
};
});
// GrassStripe class for representing the scrolling grass stripes
var GrassStripe = Container.expand(function () {
var self = Container.call(this);
self.isDark = false; // Track if this is a dark stripe
var stripeGraphics = self.attachAsset('grassStripe', {
anchorX: 0.5,
anchorY: 0.5
});
// Set speed of scrolling
self.speed = 5;
// Method to toggle between light and dark stripe
self.setDark = function (isDark) {
self.isDark = isDark;
if (isDark) {
stripeGraphics = self.attachAsset('grassStripeDark', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
stripeGraphics = self.attachAsset('grassStripe', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.update = function () {
self.y += self.speed;
// If stripe moves off screen, reset to top
if (self.y > 2732 + self.height / 2) {
self.y = -self.height / 2;
}
};
return self;
});
// Jeep class to represent the player's vehicle
var Jeep = Container.expand(function () {
var self = Container.call(this);
var jeepGraphics = self.attachAsset('jeep', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
var dx = targetPosition.x - self.x;
var dy = targetPosition.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += self.speed * Math.cos(angle);
self.y += self.speed * Math.sin(angle);
};
});
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
// Create a trunk hit box for collision detection
self.trunkWidth = 60; // width of trunk collision area
self.trunkHeight = 200; // height of trunk collision area
self.trunkOffsetY = 50; // offset from center of tree
// Check if point is within the trunk area
self.trunkIntersects = function (obj) {
// Calculate trunk boundaries
var trunkLeft = self.x - self.trunkWidth / 2;
var trunkRight = self.x + self.trunkWidth / 2;
var trunkTop = self.y + self.trunkOffsetY - self.trunkHeight / 2;
var trunkBottom = self.y + self.trunkOffsetY + self.trunkHeight / 2;
// Get object boundaries
var objLeft = obj.x - obj.width / 2;
var objRight = obj.x + obj.width / 2;
var objTop = obj.y - obj.height / 2;
var objBottom = obj.y + obj.height / 2;
// Check for intersection
return !(objRight < trunkLeft || objLeft > trunkRight || objBottom < trunkTop || objTop > trunkBottom);
};
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x006400 //Init game with darker green background
});
/****
* Game Code
****/
// Initialize game variables
var jeep;
var scoreTxt;
var deathReasonTxt;
var deathReason = "";
var isTouching = false;
var grassStripes = [];
// Function to create scrolling grass stripes
function createGrassStripes() {
// Clear existing stripes if any
for (var i = 0; i < grassStripes.length; i++) {
if (grassStripes[i]) {
grassStripes[i].destroy();
}
}
grassStripes = [];
// Create new stripes to fill the screen
var stripeHeight = 200;
var numStripes = Math.ceil(2732 / stripeHeight) + 1; // +1 for seamless scrolling
for (var i = 0; i < numStripes; i++) {
var stripe = new GrassStripe();
stripe.x = 2048 / 2;
stripe.y = i * stripeHeight;
stripe.setDark(i % 2 === 0); // Alternate between light and dark
grassStripes.push(stripe);
game.addChild(stripe);
// Make sure stripes are added at the bottom of the display list (background)
game.swapChildren(stripe, game.getChildAt(0));
}
}
// Function to initialize game elements
function initGame() {
// Initialize grass stripes
createGrassStripes();
// Create and position the Jeep
jeep = game.addChild(new Jeep());
jeep.x = 2048 / 2;
jeep.y = 2732 - 200;
targetPosition.x = jeep.x;
targetPosition.y = jeep.y;
// Initialize score display
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
// Initialize death reason display
deathReasonTxt = new Text2('', {
size: 80,
fill: 0xFFFFFF
});
// Initialize bugs, fastBugs and trees arrays
bugs = [];
fastBugs = [];
trees = [];
scoreTxt.anchor.set(0.5, 0);
deathReasonTxt.anchor.set(0.5, 0);
deathReasonTxt.y = 120; // Position below score text
LK.gui.top.addChild(scoreTxt);
LK.gui.top.addChild(deathReasonTxt);
}
var targetPosition = {
x: 0,
y: 0
};
game.down = function (x, y, obj) {
// Check if the touch is on a bug, if so ignore and return
for (var i = 0; i < bugs.length; i++) {
if (x >= bugs[i].x - 100 && x <= bugs[i].x + 100 && y >= bugs[i].y - 100 && y <= bugs[i].y + 100) {
return;
}
}
// Check if the touch is on a fast bug, if so ignore and return
for (var i = 0; i < fastBugs.length; i++) {
if (x >= fastBugs[i].x - 100 && x <= fastBugs[i].x + 100 && y >= fastBugs[i].y - 100 && y <= fastBugs[i].y + 100) {
return;
}
}
// Set target position for the jeep movement
targetPosition.x = x;
targetPosition.y = y;
};
game.update = function () {
// Update grass stripes
for (var i = 0; i < grassStripes.length; i++) {
grassStripes[i].update();
}
jeep.update();
// Bug and tree spawning logic
if (LK.ticks % 60 == 0) {
var newBug = new Bug();
newBug.x = Math.random() * 2048;
newBug.y = 0;
bugs.push(newBug);
game.addChild(newBug);
}
if (LK.ticks % 180 == 0) {
var newFastBug = new FastBug();
newFastBug.x = Math.random() * 2048;
newFastBug.y = 0;
fastBugs.push(newFastBug);
game.addChild(newFastBug);
}
if (LK.ticks % 180 == 0) {
var newTree = new Tree();
newTree.x = Math.random() * 2048;
newTree.y = 0;
trees.push(newTree);
game.addChild(newTree);
}
// Bug and tree movement and collision detection logic
for (var i = bugs.length - 1; i >= 0; i--) {
bugs[i].update();
// Only check collision if it's not an AnimatedBug
if (bugs[i].intersects(jeep) && !(bugs[i] instanceof AnimatedBug)) {
deathReason = "Hit by a bug!";
deathReasonTxt.setText(deathReason);
LK.showGameOver();
}
if (bugs[i].y > 2732) {
bugs[i].destroy();
bugs.splice(i, 1);
}
}
for (var i = fastBugs.length - 1; i >= 0; i--) {
fastBugs[i].update();
// Only check collision if it's not an AnimatedBug
if (fastBugs[i].intersects(jeep) && !(fastBugs[i] instanceof AnimatedBug)) {
deathReason = "Hit by a fast bug!";
deathReasonTxt.setText(deathReason);
LK.showGameOver();
}
if (fastBugs[i].y > 2732) {
fastBugs[i].destroy();
fastBugs.splice(i, 1);
}
}
for (var i = trees.length - 1; i >= 0; i--) {
trees[i].update();
if (trees[i].trunkIntersects(jeep)) {
deathReason = "Crashed into a tree!";
deathReasonTxt.setText(deathReason);
LK.showGameOver();
}
if (trees[i].y > 2732) {
trees[i].destroy();
trees.splice(i, 1);
}
}
};
// Initialize the game
initGame();
// Import the tween plugin ===================================================================
--- original.js
+++ change.js
@@ -41,10 +41,8 @@
// Increment score
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText('Score: ' + LK.getScore());
- // Flash the screen when bug is clicked
- LK.effects.flashScreen(0x00ff00, 300); // Green flash for 300ms
// Replace bug with AnimatedBug
var animatedBug = game.addChild(new AnimatedBug());
animatedBug.x = self.x;
animatedBug.y = self.y;
@@ -67,18 +65,50 @@
// Increment score
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText('Score: ' + LK.getScore());
- // Flash the screen when fast bug is clicked
- LK.effects.flashScreen(0xff0000, 300); // Red flash for 300ms
// Replace bug with AnimatedBug
var animatedBug = game.addChild(new AnimatedBug());
animatedBug.x = self.x;
animatedBug.y = self.y;
isTouching = false; // Prevent car from moving when touching a bug
self.destroy();
};
});
+// GrassStripe class for representing the scrolling grass stripes
+var GrassStripe = Container.expand(function () {
+ var self = Container.call(this);
+ self.isDark = false; // Track if this is a dark stripe
+ var stripeGraphics = self.attachAsset('grassStripe', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Set speed of scrolling
+ self.speed = 5;
+ // Method to toggle between light and dark stripe
+ self.setDark = function (isDark) {
+ self.isDark = isDark;
+ if (isDark) {
+ stripeGraphics = self.attachAsset('grassStripeDark', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ } else {
+ stripeGraphics = self.attachAsset('grassStripe', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ }
+ };
+ self.update = function () {
+ self.y += self.speed;
+ // If stripe moves off screen, reset to top
+ if (self.y > 2732 + self.height / 2) {
+ self.y = -self.height / 2;
+ }
+ };
+ return self;
+});
// Jeep class to represent the player's vehicle
var Jeep = Container.expand(function () {
var self = Container.call(this);
var jeepGraphics = self.attachAsset('jeep', {
@@ -134,17 +164,42 @@
/****
* Game Code
****/
-// Import tween plugin for animations
// Initialize game variables
var jeep;
var scoreTxt;
var deathReasonTxt;
var deathReason = "";
var isTouching = false;
+var grassStripes = [];
+// Function to create scrolling grass stripes
+function createGrassStripes() {
+ // Clear existing stripes if any
+ for (var i = 0; i < grassStripes.length; i++) {
+ if (grassStripes[i]) {
+ grassStripes[i].destroy();
+ }
+ }
+ grassStripes = [];
+ // Create new stripes to fill the screen
+ var stripeHeight = 200;
+ var numStripes = Math.ceil(2732 / stripeHeight) + 1; // +1 for seamless scrolling
+ for (var i = 0; i < numStripes; i++) {
+ var stripe = new GrassStripe();
+ stripe.x = 2048 / 2;
+ stripe.y = i * stripeHeight;
+ stripe.setDark(i % 2 === 0); // Alternate between light and dark
+ grassStripes.push(stripe);
+ game.addChild(stripe);
+ // Make sure stripes are added at the bottom of the display list (background)
+ game.swapChildren(stripe, game.getChildAt(0));
+ }
+}
// Function to initialize game elements
function initGame() {
+ // Initialize grass stripes
+ createGrassStripes();
// Create and position the Jeep
jeep = game.addChild(new Jeep());
jeep.x = 2048 / 2;
jeep.y = 2732 - 200;
@@ -191,8 +246,12 @@
targetPosition.x = x;
targetPosition.y = y;
};
game.update = function () {
+ // Update grass stripes
+ for (var i = 0; i < grassStripes.length; i++) {
+ grassStripes[i].update();
+ }
jeep.update();
// Bug and tree spawning logic
if (LK.ticks % 60 == 0) {
var newBug = new Bug();
@@ -255,5 +314,6 @@
}
}
};
// Initialize the game
-initGame();
\ No newline at end of file
+initGame();
+// Import the tween plugin
\ No newline at end of file
Giant insect, facing down, open wings, cartoony. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Jeep car cartoony, top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
cartoony heart for game hud. In-Game asset. 2d. High contrast. No shadows
ladybug insect. In-Game asset. 2d. High contrast. No shadows
larvae bug. In-Game asset. 2d. High contrast. No shadows