/****
* Classes
****/
// Define the Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
// Update method for the obstacle
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
// Assuming obstacle is off-screen at x < -100
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Runner class
var Runner = Container.expand(function () {
var self = Container.call(this);
var runnerGraphics = self.attachAsset('runner', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.jumpHeight = 40;
self.isJumping = false;
self.gravity = 1.5;
self.velocityY = 0;
// Update method for the runner
self.update = function () {
if (self.isJumping) {
self.velocityY -= self.gravity * 0.5; // Adjust gravity to slow down the fall
self.y -= self.velocityY;
if (self.y >= 1366) {
self.y = 1366;
self.isJumping = false;
self.velocityY = 0;
}
}
};
// Method to make the runner jump
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocityY = self.jumpHeight * 0.7; // Adjust initial jump velocity for a slower ascent
}
};
});
/****
* Initialize Game
****/
// Add a background city image
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Add a background city image
var background = game.attachAsset('Background', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732
});
// Initialize runner
var runner = game.addChild(new Runner());
runner.x = 2048 / 4; // Position runner on the left part of the screen
runner.y = 1366; // Set runner to ground level
// Array to hold obstacles
var obstacles = [];
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048; // Position obstacle on the right part of the screen
obstacle.y = 2732 / 2; // Ground level
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Initialize score
var score = 0;
// Initialize score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize level
var level = 1;
// Initialize level display
var levelTxt = new Text2('1', {
size: 150,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(levelTxt);
// Handle game updates
game.update = function () {
runner.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (runner.intersects(obstacles[i])) {
// Handle collision
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new obstacles periodically
if (LK.ticks % 120 === 0) {
// Every 2 seconds at 60 FPS
spawnObstacle();
// Increase score
score++;
// Update level and obstacle speed every 10 points
if (score % 10 === 0) {
level++;
// Update level display
levelTxt.setText(level);
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].speed += level;
}
}
// Update score display
scoreTxt.setText(score);
}
};
// Handle touch events for jumping
game.down = function (x, y, obj) {
runner.jump();
}; /****
* Classes
****/
// Define the Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
// Update method for the obstacle
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
// Assuming obstacle is off-screen at x < -100
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Runner class
var Runner = Container.expand(function () {
var self = Container.call(this);
var runnerGraphics = self.attachAsset('runner', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.jumpHeight = 40;
self.isJumping = false;
self.gravity = 1.5;
self.velocityY = 0;
// Update method for the runner
self.update = function () {
if (self.isJumping) {
self.velocityY -= self.gravity * 0.5; // Adjust gravity to slow down the fall
self.y -= self.velocityY;
if (self.y >= 1366) {
self.y = 1366;
self.isJumping = false;
self.velocityY = 0;
}
}
};
// Method to make the runner jump
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocityY = self.jumpHeight * 0.7; // Adjust initial jump velocity for a slower ascent
}
};
});
/****
* Initialize Game
****/
// Add a background city image
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Add a background city image
var background = game.attachAsset('Background', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732
});
// Initialize runner
var runner = game.addChild(new Runner());
runner.x = 2048 / 4; // Position runner on the left part of the screen
runner.y = 1366; // Set runner to ground level
// Array to hold obstacles
var obstacles = [];
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048; // Position obstacle on the right part of the screen
obstacle.y = 2732 / 2; // Ground level
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Initialize score
var score = 0;
// Initialize score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize level
var level = 1;
// Initialize level display
var levelTxt = new Text2('1', {
size: 150,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(levelTxt);
// Handle game updates
game.update = function () {
runner.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (runner.intersects(obstacles[i])) {
// Handle collision
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new obstacles periodically
if (LK.ticks % 120 === 0) {
// Every 2 seconds at 60 FPS
spawnObstacle();
// Increase score
score++;
// Update level and obstacle speed every 10 points
if (score % 10 === 0) {
level++;
// Update level display
levelTxt.setText(level);
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].speed += level;
}
}
// Update score display
scoreTxt.setText(score);
}
};
// Handle touch events for jumping
game.down = function (x, y, obj) {
runner.jump();
};