/****
* Classes
****/
var Floor = Container.expand(function () {
var self = Container.call(this);
self.floorGraphics = self.attachAsset('floor', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Define the Garage1 class
var Garage1 = Container.expand(function () {
var self = Container.call(this);
var garage1Graphics = self.attachAsset('garage1', {
anchorX: 0.5,
anchorY: 0.0
});
self.speed = sharedSpeed;
self.update = function () {
self.x -= self.speed;
if (self.x < -garage1Graphics.width / 2) {
self.destroy();
}
};
});
var JumpParticle = Container.expand(function () {
var self = Container.call(this);
var particleGraphics = self.attachAsset('particle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 5 + 2;
self.direction = Math.random() * Math.PI * 2;
self.lifetime = 30;
self.update = function () {
self.x += Math.cos(self.direction) * self.speed;
self.y += Math.sin(self.direction) * self.speed;
self.lifetime--;
if (self.lifetime <= 0) {
self.destroy();
}
};
});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = sharedSpeed * 0.625; // Half the speed of the original obstacle speed
self.update = function () {
self.x -= self.speed * (1 + elapsedTime / 60); // Increase speed over time
if (self.x < 0) {
self.destroy();
}
};
});
// Define the Platform class
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('garage1', {
anchorX: 0.5,
anchorY: 0.0
});
self.speed = sharedSpeed;
self.update = function () {
self.x -= self.speed;
if (self.x < -platformGraphics.width / 2) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.gravity = 2.5;
self.velocity = 0;
self.isJumping = false;
self.speed = 10;
self.update = function () {
// Apply gravity
self.velocity += self.gravity;
self.y += self.velocity;
// Check for collision with the floor
if (self.y + playerGraphics.height / 2 >= floor.y - floor.floorGraphics.height / 2) {
self.y = floor.y - floor.floorGraphics.height / 2 - playerGraphics.height / 2;
if (self.isJumping && self.velocity > 10) {
for (var i = 0; i < 10; i++) {
var particle = new JumpParticle();
particle.x = self.x;
particle.y = self.y + playerGraphics.height / 2;
game.addChild(particle);
}
LK.getSound('land').play(); // Play landing sound effect
}
if (self.isJumping) {
for (var i = 0; i < 10; i++) {
var particle = new JumpParticle();
particle.x = self.x;
particle.y = self.y + playerGraphics.height / 2;
game.addChild(particle);
}
}
self.isJumping = false;
self.rotation = 0; // Reset rotation when on the platform
} else {
var onPlatform = false;
for (var j = platforms.length - 1; j >= 0; j--) {
if (self.intersects(platforms[j])) {
if (self.velocity > 0) {
self.y = platforms[j].y - playerGraphics.height / 2;
self.isJumping = false;
self.rotation = 0; // Reset rotation when on the platform
if (self.velocity > 3) {
LK.getSound('land').play(); // Play landing sound effect
}
self.velocity = 0;
}
onPlatform = true;
break;
}
}
if (!onPlatform) {
self.isJumping = true;
self.rotation += 0.1; // Rotate the player while in the air
}
}
};
self.moveLeft = function () {
self.x -= self.speed;
};
self.moveRight = function () {
self.x += self.speed;
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocity = -62.5;
LK.getSound('jump').play(); // Play jump sound effect
for (var i = 0; i < 10; i++) {
var particle = new JumpParticle();
particle.x = self.x;
particle.y = self.y + playerGraphics.height / 2;
game.addChild(particle);
}
}
};
});
var Point = Container.expand(function () {
var self = Container.call(this);
var pointGraphics = self.attachAsset('point', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = sharedSpeed;
self.update = function () {
self.x -= self.speed; // Move points to the left at the same speed as platforms
if (self.x < -pointGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
width: 2048,
height: 2732
});
var house1 = game.attachAsset('house1', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
// Define a globally shared speed variable
var sharedSpeed = 10;
// Initialize elapsed time
var elapsedTime = 0;
// Create and display the score text at the top center of the screen
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Center the score text horizontally
LK.gui.top.addChild(scoreTxt);
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 8;
player.y = 2732 - 200;
// Initialize floor
var floor = game.addChild(new Floor());
floor.x = 2048 / 2;
floor.y = 2732 - 25;
// Initialize obstacles array
var obstacles = [];
// Initialize platforms array
var platforms = [];
// Initialize points array
var points = [];
// Initialize points array
var points = [];
;
// Function to spawn platforms
function spawnPlatform() {
var platform = new Platform();
platform.x = 2048 + platform.width / 2;
platform.y = floor.y - floor.floorGraphics.height / 2 - platform.height + 25; // Position platform at the top of the floor
platforms.push(platform);
game.addChild(platform);
if (Math.random() < 0.5) {
var point = new Point();
point.x = platform.x;
point.y = platform.y - 100; // Adjust the y position of the point relative to the platform
points.push(point);
game.addChild(point);
}
}
// Set interval to spawn platforms
var platformInterval = LK.setInterval(spawnPlatform, 2000);
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048 + 50;
obstacle.y = 2732 - obstacle.height / 2; // Set obstacle's y position to be just above the floor
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Set interval to spawn obstacles
var obstacleInterval = LK.setInterval(spawnObstacle, 4000);
// Handle touch events for player movement
game.down = function (x, y, obj) {
player.jump();
};
// Update game logic
game.update = function () {
// Update elapsed time
elapsedTime += 1 / 60; // Assuming 60 FPS
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (player.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
for (var j = points.length - 1; j >= 0; j--) {
if (player.intersects(points[j])) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('pointPickup').play(); // Play point pickup sound effect
points[j].destroy();
points.splice(j, 1);
}
}
};