/****
* Classes
****/
// Class for the bike
var Bike = Container.expand(function () {
var self = Container.call(this);
var bikeGraphics = self.attachAsset('bike', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// No movement
};
});
var Bush = Container.expand(function () {
var self = Container.call(this);
var bushGraphics = self.attachAsset('bush', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move the bush downwards
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to the top
}
};
});
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// No movement
};
});
var FallingObject = Container.expand(function () {
var self = Container.call(this);
var fallingObjectGraphics = self.attachAsset('fallingObject', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + self.height) {
self.y = -self.height;
self.x = Math.random() * 1648 + 200; // Random x position within the lane
self.attachAsset('yellowSquare', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
});
// Class for the green square
var GreenSquare = Container.expand(function () {
var self = Container.call(this);
var greenSquareGraphics = self.attachAsset('greenSquare', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Lateral movement logic
if (self.x < 200) {
self.x = 200; // Prevent moving out of the left boundary
} else if (self.x > 1848) {
self.x = 1848; // Prevent moving out of the right boundary
}
};
});
var Herb = Container.expand(function () {
var self = Container.call(this);
var herbGraphics = self.attachAsset('herb', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move the herb downwards
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to the top
}
};
});
// Create and position herbs
var LaneLine = Container.expand(function () {
var self = Container.call(this);
var laneLineGraphics = self.attachAsset('laneLine', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5; // Match the speed of plants
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to maintain equal distance
}
// self.y += self.speed;
// if (self.y > 2732 + self.height) {
// self.y = -self.height;
// }
};
});
// Class for the road
var Road = Container.expand(function () {
var self = Container.call(this);
var roadGraphics = self.attachAsset('road', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// self.y += self.speed;
// if (self.y > 2732 + self.height) {
// self.y = -self.height + 800;
// }
};
});
// Class for the side road
var SideRoad = Container.expand(function () {
var self = Container.call(this);
var sideRoadGraphics = self.attachAsset('sideRoad', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// No movement
};
});
// Class for the tree
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move the tree downwards
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to the top
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Removed bikeAsset initialization
// Create and position two road segments
var road1 = game.addChild(new Road());
road1.x = 1024;
road1.y = 1366;
var road2 = game.addChild(new Road());
road2.x = 1024;
road2.y = 1366 + road1.height;
// Create and position lane lines
var laneLines = [];
for (var i = 0; i < 10; i++) {
var laneLine = game.addChild(new LaneLine());
laneLine.x = 1024;
laneLine.y = i * 300; // Ensure equal distance
laneLines.push(laneLine);
}
// Create and position side roads
var leftSideRoad = game.addChild(new SideRoad());
leftSideRoad.x = 200;
var rightSideRoad = game.addChild(new SideRoad());
rightSideRoad.x = 1848;
leftSideRoad.y = 1366;
rightSideRoad.y = 1366;
// Create and position the green square
var greenSquare = game.addChild(new GreenSquare());
greenSquare.x = 1024;
greenSquare.y = 2000;
// Create and position the car
var car = game.addChild(new Car());
car.x = 1024;
car.y = 2000;
// Create and position falling objects
var fallingObjects = [];
for (var i = 0; i < 5; i++) {
var fallingObject = game.addChild(new FallingObject());
fallingObject.x = Math.random() * 1648 + 200; // Random x position within the lane
fallingObject.y = Math.random() * -2732; // Random y position above the screen
fallingObjects.push(fallingObject);
}
// Add event listeners for moving the main box
var dragNode = null;
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
// Prevent moving out of the boundaries
if (dragNode && dragNode.x < 200) {
dragNode.x = 200;
} else if (dragNode && dragNode.x > 1848) {
dragNode.x = 1848;
}
}
game.down = function (x, y, obj) {
dragNode = greenSquare;
handleMove(x, y, obj); // Call move handler right away to make effect instant
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
handleMove(x, y, obj); // Call move handler to update position
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Update function for the game
game.update = function () {
// road1.update();
// road2.update();
// leftSideRoad.update();
// rightSideRoad.update();
car.update();
greenSquare.update();
greenSquare.update();
fallingObjects.forEach(function (fallingObject) {
fallingObject.update();
});
bushes.forEach(function (bush) {
bush.update();
});
laneLines.forEach(function (laneLine) {
laneLine.speed = 5; // Match the speed of plants
laneLine.update();
});
};
// Create and position bushes
var bushes = [];
for (var i = 0; i < 10; i++) {
var bush = game.addChild(new Bush());
bush.x = Math.random() * 200; // Random x position outside the road on the left
bush.y = Math.random() * -2732; // Random y position above the screen
bushes.push(bush);
}
for (var i = 0; i < 10; i++) {
var bush = game.addChild(new Bush());
bush.x = 1848 + Math.random() * 200; // Random x position outside the road on the right
bush.y = Math.random() * -2732; // Random y position above the screen
bushes.push(bush);
} /****
* Classes
****/
// Class for the bike
var Bike = Container.expand(function () {
var self = Container.call(this);
var bikeGraphics = self.attachAsset('bike', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// No movement
};
});
var Bush = Container.expand(function () {
var self = Container.call(this);
var bushGraphics = self.attachAsset('bush', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move the bush downwards
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to the top
}
};
});
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// No movement
};
});
var FallingObject = Container.expand(function () {
var self = Container.call(this);
var fallingObjectGraphics = self.attachAsset('fallingObject', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + self.height) {
self.y = -self.height;
self.x = Math.random() * 1648 + 200; // Random x position within the lane
self.attachAsset('yellowSquare', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
});
// Class for the green square
var GreenSquare = Container.expand(function () {
var self = Container.call(this);
var greenSquareGraphics = self.attachAsset('greenSquare', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Lateral movement logic
if (self.x < 200) {
self.x = 200; // Prevent moving out of the left boundary
} else if (self.x > 1848) {
self.x = 1848; // Prevent moving out of the right boundary
}
};
});
var Herb = Container.expand(function () {
var self = Container.call(this);
var herbGraphics = self.attachAsset('herb', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move the herb downwards
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to the top
}
};
});
// Create and position herbs
var LaneLine = Container.expand(function () {
var self = Container.call(this);
var laneLineGraphics = self.attachAsset('laneLine', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5; // Match the speed of plants
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to maintain equal distance
}
// self.y += self.speed;
// if (self.y > 2732 + self.height) {
// self.y = -self.height;
// }
};
});
// Class for the road
var Road = Container.expand(function () {
var self = Container.call(this);
var roadGraphics = self.attachAsset('road', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// self.y += self.speed;
// if (self.y > 2732 + self.height) {
// self.y = -self.height + 800;
// }
};
});
// Class for the side road
var SideRoad = Container.expand(function () {
var self = Container.call(this);
var sideRoadGraphics = self.attachAsset('sideRoad', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// No movement
};
});
// Class for the tree
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move the tree downwards
if (self.y > 2732 + self.height) {
self.y = -self.height; // Reset position to the top
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Removed bikeAsset initialization
// Create and position two road segments
var road1 = game.addChild(new Road());
road1.x = 1024;
road1.y = 1366;
var road2 = game.addChild(new Road());
road2.x = 1024;
road2.y = 1366 + road1.height;
// Create and position lane lines
var laneLines = [];
for (var i = 0; i < 10; i++) {
var laneLine = game.addChild(new LaneLine());
laneLine.x = 1024;
laneLine.y = i * 300; // Ensure equal distance
laneLines.push(laneLine);
}
// Create and position side roads
var leftSideRoad = game.addChild(new SideRoad());
leftSideRoad.x = 200;
var rightSideRoad = game.addChild(new SideRoad());
rightSideRoad.x = 1848;
leftSideRoad.y = 1366;
rightSideRoad.y = 1366;
// Create and position the green square
var greenSquare = game.addChild(new GreenSquare());
greenSquare.x = 1024;
greenSquare.y = 2000;
// Create and position the car
var car = game.addChild(new Car());
car.x = 1024;
car.y = 2000;
// Create and position falling objects
var fallingObjects = [];
for (var i = 0; i < 5; i++) {
var fallingObject = game.addChild(new FallingObject());
fallingObject.x = Math.random() * 1648 + 200; // Random x position within the lane
fallingObject.y = Math.random() * -2732; // Random y position above the screen
fallingObjects.push(fallingObject);
}
// Add event listeners for moving the main box
var dragNode = null;
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
// Prevent moving out of the boundaries
if (dragNode && dragNode.x < 200) {
dragNode.x = 200;
} else if (dragNode && dragNode.x > 1848) {
dragNode.x = 1848;
}
}
game.down = function (x, y, obj) {
dragNode = greenSquare;
handleMove(x, y, obj); // Call move handler right away to make effect instant
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
handleMove(x, y, obj); // Call move handler to update position
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Update function for the game
game.update = function () {
// road1.update();
// road2.update();
// leftSideRoad.update();
// rightSideRoad.update();
car.update();
greenSquare.update();
greenSquare.update();
fallingObjects.forEach(function (fallingObject) {
fallingObject.update();
});
bushes.forEach(function (bush) {
bush.update();
});
laneLines.forEach(function (laneLine) {
laneLine.speed = 5; // Match the speed of plants
laneLine.update();
});
};
// Create and position bushes
var bushes = [];
for (var i = 0; i < 10; i++) {
var bush = game.addChild(new Bush());
bush.x = Math.random() * 200; // Random x position outside the road on the left
bush.y = Math.random() * -2732; // Random y position above the screen
bushes.push(bush);
}
for (var i = 0; i < 10; i++) {
var bush = game.addChild(new Bush());
bush.x = 1848 + Math.random() * 200; // Random x position outside the road on the right
bush.y = Math.random() * -2732; // Random y position above the screen
bushes.push(bush);
}