/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Obstacle = Container.expand(function (type) {
var self = Container.call(this);
var obstacleGraphics;
self.type = type || 'step';
self.speed = 0;
if (self.type === 'step') {
obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'barrier') {
obstacleGraphics = self.attachAsset('barrier', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'firePit') {
obstacleGraphics = self.attachAsset('firePit', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (self.type === 'car') {
obstacleGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'newObstacle1') {
obstacleGraphics = self.attachAsset('newObstacle1', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'newObstacle2') {
obstacleGraphics = self.attachAsset('newObstacle2', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'newObstacle3') {
obstacleGraphics = self.attachAsset('newObstacle3', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'energyDrink') {
obstacleGraphics = self.attachAsset('energyDrink', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'powerUp') {
obstacleGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 1.0
});
}
self.update = function () {
self.x -= self.speed;
};
self.getCollisionBounds = function () {
return {
x: self.x - obstacleGraphics.width / 2,
y: self.y - obstacleGraphics.height,
width: obstacleGraphics.width,
height: obstacleGraphics.height
};
};
self.isLowObstacle = function () {
return self.type === 'step' || self.type === 'firePit';
};
self.isHighObstacle = function () {
return self.type === 'barrier';
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.update = function () {
if (self.isJumping) {
LK.getSound('moveSound').play();
self.y -= self.jumpSpeed;
self.jumpSpeed -= 1; // Gravity effect
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
}
}
};
self.getCollisionBounds = function () {
return {
x: self.x - playerGraphics.width / 2,
y: self.y - playerGraphics.height,
width: playerGraphics.width,
height: playerGraphics.height
};
};
self.isJumping = false;
self.jumpSpeed = 0;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = 40; // Decreased initial jump speed for lower jump height
LK.getSound('jumpSound').play();
}
};
return self;
});
var RoadLine = Container.expand(function () {
var self = Container.call(this);
var lineGraphics = self.attachAsset('roadLine', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.update = function () {
self.x -= self.speed;
if (self.x < -lineGraphics.width) {
self.x = 2048 + lineGraphics.width;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000,
width: 2048,
height: 2732
});
/****
* Game Code
****/
// Initialize background image asset
var player;
var obstacles = [];
var roadLines = [];
var gameSpeed = 10;
var spawnTimer = 0;
var minSpawnTime = 60;
var maxSpawnTime = 120;
var nextSpawnTime = 100;
var distance = 0;
var isGameOver = false;
var shouldSpawnObstacle = true;
// Add background theme
var background = game.addChild(LK.getAsset('backgroundTheme', {
anchorX: 0.5,
anchorY: 0.5
}));
background.x = 2048 / 2;
background.y = 2732 / 2;
// Create road
var road = game.addChild(LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0
}));
road.x = 2048 / 2;
road.y = 2732 - 600;
// Create player
player = game.addChild(new Player());
player.x = 300;
player.y = 2732 - 300;
player.groundY = player.y;
// Create road lines
for (var i = 0; i < 20; i++) {
var roadLine = new RoadLine();
roadLine.x = i * 200;
roadLine.y = 2732 - 600 + 300;
roadLine.speed = gameSpeed;
roadLines.push(roadLine);
game.addChild(roadLine);
}
// Score display
var scoreTxt = new Text2('0m', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Input Handling
function handleDown(x, y, obj) {
if (y < player.y - 100) {
// Detect swipe up
player.jump();
}
}
// Keyboard controls for testing
game.keyDown = function (key) {};
game.down = handleDown;
// Collision detection
function checkCollision(playerBounds, obstacleBounds) {
return !(playerBounds.x + playerBounds.width < obstacleBounds.x || playerBounds.x > obstacleBounds.x + obstacleBounds.width || playerBounds.y + playerBounds.height < obstacleBounds.y || playerBounds.y > obstacleBounds.y + obstacleBounds.height);
}
// Spawn obstacles
function spawnObstacle() {
if (!shouldSpawnObstacle) {
return;
}
var types = ['step', 'barrier', 'firePit', 'car', 'newObstacle1', 'newObstacle2', 'newObstacle3', 'energyDrink', 'powerUp'];
var type = types[Math.floor(Math.random() * types.length)];
var obstacle = new Obstacle(type);
obstacle.x = 2048 + 100;
obstacle.y = 2732 - 300; // Move obstacles closer to the road line
obstacle.speed = gameSpeed;
if (type === 'firePit') {
obstacle.y -= 150; // Move firepit position further up near the road line
}
if (type === 'car') {
obstacle.x += Math.random() * 500;
}
obstacles.push(obstacle);
game.addChild(obstacle);
nextSpawnTime = minSpawnTime + Math.floor(Math.random() * (maxSpawnTime - minSpawnTime));
spawnTimer = 0;
}
// Game over handling
function gameOver() {
if (!isGameOver) {
isGameOver = true;
shouldSpawnObstacle = false;
LK.getSound('crash').play();
var highScore = storage.highScore || 0;
if (distance > highScore) {
storage.highScore = distance;
}
LK.effects.flashScreen(0xff0000, 500);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
}
// Main game loop
game.update = function () {
if (isGameOver) {
return;
}
// Update score
distance += 1;
scoreTxt.setText(Math.floor(distance / 10) + "m");
// Increase difficulty
if (distance % 500 === 0 && gameSpeed < 25) {
gameSpeed += 0.5;
roadLines.forEach(function (line) {
return line.speed = gameSpeed;
});
obstacles.forEach(function (obs) {
return obs.speed = gameSpeed;
});
if (minSpawnTime > 30) {
minSpawnTime -= 2;
}
if (maxSpawnTime > 60) {
maxSpawnTime -= 5;
}
}
// Update road lines
roadLines.forEach(function (line) {
return line.update();
});
// Spawn obstacles
spawnTimer++;
if (spawnTimer >= nextSpawnTime) {
spawnObstacle();
}
// Update player
player.update();
// Check collisions
for (var j = obstacles.length - 1; j >= 0; j--) {
var obstacle = obstacles[j];
obstacle.update();
if (obstacle.x < -200) {
obstacle.destroy();
obstacles.splice(j, 1);
continue;
}
var playerBounds = player.getCollisionBounds();
var obstacleBounds = obstacle.getCollisionBounds();
var collisionDetected = false;
if (obstacle.isLowObstacle() || obstacle.isHighObstacle() || obstacle.type === 'car' || obstacle.type === 'newObstacle1' || obstacle.type === 'newObstacle2' || obstacle.type === 'newObstacle3' || obstacle.type === 'energyDrink' || obstacle.type === 'powerUp') {
collisionDetected = checkCollision(playerBounds, obstacleBounds);
}
if (collisionDetected) {
if (obstacle.type === 'energyDrink' || obstacle.type === 'powerUp') {
LK.getSound('energyDrinkCollect').play();
// Remove the energy drink from the game
obstacle.destroy();
obstacles.splice(j, 1);
// Increase player speed
var originalSpeed = gameSpeed;
gameSpeed += 5;
roadLines.forEach(function (line) {
line.speed = gameSpeed;
});
obstacles.forEach(function (obs) {
obs.speed = gameSpeed;
});
// Set a timeout to revert speed back to normal after 10 seconds
LK.setTimeout(function () {
gameSpeed = originalSpeed;
roadLines.forEach(function (line) {
line.speed = gameSpeed;
});
obstacles.forEach(function (obs) {
obs.speed = gameSpeed;
});
}, 10000);
} else if (obstacle.type === 'newObstacle3') {
gameOver();
obstacle.destroy();
obstacles.splice(j, 1);
} else {
tween(player, {
tint: 0xFF0000
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
gameOver();
}
});
break;
}
continue; // Ensure to continue to the next iteration
}
}
};
// Start music
LK.playMusic('bgmusic', {
fade: {
start: 0,
end: 0.5,
duration: 1000
}
});
// Debug controls /****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Obstacle = Container.expand(function (type) {
var self = Container.call(this);
var obstacleGraphics;
self.type = type || 'step';
self.speed = 0;
if (self.type === 'step') {
obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'barrier') {
obstacleGraphics = self.attachAsset('barrier', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'firePit') {
obstacleGraphics = self.attachAsset('firePit', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (self.type === 'car') {
obstacleGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'newObstacle1') {
obstacleGraphics = self.attachAsset('newObstacle1', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'newObstacle2') {
obstacleGraphics = self.attachAsset('newObstacle2', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'newObstacle3') {
obstacleGraphics = self.attachAsset('newObstacle3', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'energyDrink') {
obstacleGraphics = self.attachAsset('energyDrink', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'powerUp') {
obstacleGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 1.0
});
}
self.update = function () {
self.x -= self.speed;
};
self.getCollisionBounds = function () {
return {
x: self.x - obstacleGraphics.width / 2,
y: self.y - obstacleGraphics.height,
width: obstacleGraphics.width,
height: obstacleGraphics.height
};
};
self.isLowObstacle = function () {
return self.type === 'step' || self.type === 'firePit';
};
self.isHighObstacle = function () {
return self.type === 'barrier';
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.update = function () {
if (self.isJumping) {
LK.getSound('moveSound').play();
self.y -= self.jumpSpeed;
self.jumpSpeed -= 1; // Gravity effect
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
}
}
};
self.getCollisionBounds = function () {
return {
x: self.x - playerGraphics.width / 2,
y: self.y - playerGraphics.height,
width: playerGraphics.width,
height: playerGraphics.height
};
};
self.isJumping = false;
self.jumpSpeed = 0;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = 40; // Decreased initial jump speed for lower jump height
LK.getSound('jumpSound').play();
}
};
return self;
});
var RoadLine = Container.expand(function () {
var self = Container.call(this);
var lineGraphics = self.attachAsset('roadLine', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.update = function () {
self.x -= self.speed;
if (self.x < -lineGraphics.width) {
self.x = 2048 + lineGraphics.width;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000,
width: 2048,
height: 2732
});
/****
* Game Code
****/
// Initialize background image asset
var player;
var obstacles = [];
var roadLines = [];
var gameSpeed = 10;
var spawnTimer = 0;
var minSpawnTime = 60;
var maxSpawnTime = 120;
var nextSpawnTime = 100;
var distance = 0;
var isGameOver = false;
var shouldSpawnObstacle = true;
// Add background theme
var background = game.addChild(LK.getAsset('backgroundTheme', {
anchorX: 0.5,
anchorY: 0.5
}));
background.x = 2048 / 2;
background.y = 2732 / 2;
// Create road
var road = game.addChild(LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0
}));
road.x = 2048 / 2;
road.y = 2732 - 600;
// Create player
player = game.addChild(new Player());
player.x = 300;
player.y = 2732 - 300;
player.groundY = player.y;
// Create road lines
for (var i = 0; i < 20; i++) {
var roadLine = new RoadLine();
roadLine.x = i * 200;
roadLine.y = 2732 - 600 + 300;
roadLine.speed = gameSpeed;
roadLines.push(roadLine);
game.addChild(roadLine);
}
// Score display
var scoreTxt = new Text2('0m', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Input Handling
function handleDown(x, y, obj) {
if (y < player.y - 100) {
// Detect swipe up
player.jump();
}
}
// Keyboard controls for testing
game.keyDown = function (key) {};
game.down = handleDown;
// Collision detection
function checkCollision(playerBounds, obstacleBounds) {
return !(playerBounds.x + playerBounds.width < obstacleBounds.x || playerBounds.x > obstacleBounds.x + obstacleBounds.width || playerBounds.y + playerBounds.height < obstacleBounds.y || playerBounds.y > obstacleBounds.y + obstacleBounds.height);
}
// Spawn obstacles
function spawnObstacle() {
if (!shouldSpawnObstacle) {
return;
}
var types = ['step', 'barrier', 'firePit', 'car', 'newObstacle1', 'newObstacle2', 'newObstacle3', 'energyDrink', 'powerUp'];
var type = types[Math.floor(Math.random() * types.length)];
var obstacle = new Obstacle(type);
obstacle.x = 2048 + 100;
obstacle.y = 2732 - 300; // Move obstacles closer to the road line
obstacle.speed = gameSpeed;
if (type === 'firePit') {
obstacle.y -= 150; // Move firepit position further up near the road line
}
if (type === 'car') {
obstacle.x += Math.random() * 500;
}
obstacles.push(obstacle);
game.addChild(obstacle);
nextSpawnTime = minSpawnTime + Math.floor(Math.random() * (maxSpawnTime - minSpawnTime));
spawnTimer = 0;
}
// Game over handling
function gameOver() {
if (!isGameOver) {
isGameOver = true;
shouldSpawnObstacle = false;
LK.getSound('crash').play();
var highScore = storage.highScore || 0;
if (distance > highScore) {
storage.highScore = distance;
}
LK.effects.flashScreen(0xff0000, 500);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
}
// Main game loop
game.update = function () {
if (isGameOver) {
return;
}
// Update score
distance += 1;
scoreTxt.setText(Math.floor(distance / 10) + "m");
// Increase difficulty
if (distance % 500 === 0 && gameSpeed < 25) {
gameSpeed += 0.5;
roadLines.forEach(function (line) {
return line.speed = gameSpeed;
});
obstacles.forEach(function (obs) {
return obs.speed = gameSpeed;
});
if (minSpawnTime > 30) {
minSpawnTime -= 2;
}
if (maxSpawnTime > 60) {
maxSpawnTime -= 5;
}
}
// Update road lines
roadLines.forEach(function (line) {
return line.update();
});
// Spawn obstacles
spawnTimer++;
if (spawnTimer >= nextSpawnTime) {
spawnObstacle();
}
// Update player
player.update();
// Check collisions
for (var j = obstacles.length - 1; j >= 0; j--) {
var obstacle = obstacles[j];
obstacle.update();
if (obstacle.x < -200) {
obstacle.destroy();
obstacles.splice(j, 1);
continue;
}
var playerBounds = player.getCollisionBounds();
var obstacleBounds = obstacle.getCollisionBounds();
var collisionDetected = false;
if (obstacle.isLowObstacle() || obstacle.isHighObstacle() || obstacle.type === 'car' || obstacle.type === 'newObstacle1' || obstacle.type === 'newObstacle2' || obstacle.type === 'newObstacle3' || obstacle.type === 'energyDrink' || obstacle.type === 'powerUp') {
collisionDetected = checkCollision(playerBounds, obstacleBounds);
}
if (collisionDetected) {
if (obstacle.type === 'energyDrink' || obstacle.type === 'powerUp') {
LK.getSound('energyDrinkCollect').play();
// Remove the energy drink from the game
obstacle.destroy();
obstacles.splice(j, 1);
// Increase player speed
var originalSpeed = gameSpeed;
gameSpeed += 5;
roadLines.forEach(function (line) {
line.speed = gameSpeed;
});
obstacles.forEach(function (obs) {
obs.speed = gameSpeed;
});
// Set a timeout to revert speed back to normal after 10 seconds
LK.setTimeout(function () {
gameSpeed = originalSpeed;
roadLines.forEach(function (line) {
line.speed = gameSpeed;
});
obstacles.forEach(function (obs) {
obs.speed = gameSpeed;
});
}, 10000);
} else if (obstacle.type === 'newObstacle3') {
gameOver();
obstacle.destroy();
obstacles.splice(j, 1);
} else {
tween(player, {
tint: 0xFF0000
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
gameOver();
}
});
break;
}
continue; // Ensure to continue to the next iteration
}
}
};
// Start music
LK.playMusic('bgmusic', {
fade: {
start: 0,
end: 0.5,
duration: 1000
}
});
// Debug controls