/****
* 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;
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;
self.destroy();
};
});
// 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 () {
// Jeep movement logic
if (isTouching) {
var dx = touchPosition.x - self.x;
var dy = touchPosition.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;
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;
// Function to initialize game elements
function initGame() {
// Create and position the Jeep
jeep = game.addChild(new Jeep());
jeep.x = 2048 / 2;
jeep.y = 2732 - 200;
// Initialize score display
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
// Initialize bugs, fastBugs and trees arrays
bugs = [];
fastBugs = [];
trees = [];
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
}
// Event handler for moving the Jeep
var isTouching = false;
var touchPosition = {
x: 0,
y: 0
};
game.move = function (x, y, obj) {
isTouching = true;
touchPosition.x = x;
touchPosition.y = y;
};
game.up = function (x, y, obj) {
isTouching = false;
};
game.update = function () {
if (isTouching) {
var dx = touchPosition.x - jeep.x;
var dy = touchPosition.y - jeep.y;
var angle = Math.atan2(dy, dx);
jeep.x += jeep.speed * Math.cos(angle);
jeep.y += jeep.speed * Math.sin(angle);
}
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();
if (bugs[i].intersects(jeep)) {
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();
if (fastBugs[i].intersects(jeep)) {
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].intersects(jeep)) {
LK.showGameOver();
}
if (trees[i].y > 2732) {
trees[i].destroy();
trees.splice(i, 1);
}
}
};
// Initialize the game
initGame();