/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Motorcycle = Container.expand(function () {
var self = Container.call(this);
var motorcycleGraphics = self.attachAsset('motorcycle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.5,
scaleY: 2.5
});
self.speed = 15;
self.moveLeft = false;
self.moveRight = false;
self.moveBackward = false;
self.moveForward = true;
self.laneWidth = 512;
self.currentLane = 1;
self.minX = 256;
self.maxX = 1792;
self.velocity = {
x: 0,
y: 0
};
self.update = function () {};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.0,
scaleY: 1.2
});
self.baseSpeed = 12;
self.speed = 12;
self.lastY = self.y;
self.baseScaleX = 1.0;
self.baseScaleY = 1.2;
self.setDifficulty = function (difficultyLevel) {
self.speed = self.baseSpeed + (difficultyLevel - 1) * 2;
var newScaleX = self.baseScaleX + (difficultyLevel - 1) * 0.15;
var newScaleY = self.baseScaleY + (difficultyLevel - 1) * 0.2;
tween(obstacleGraphics, {
scaleX: newScaleX,
scaleY: newScaleY
}, {
duration: 500,
easing: tween.easeOut
});
};
self.update = function () {
self.lastY = self.y;
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var roadGraphic = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.0
});
roadGraphic.x = 1024;
roadGraphic.y = 0;
game.addChild(roadGraphic);
var motorcycle = game.addChild(new Motorcycle());
motorcycle.x = 1024;
motorcycle.y = 2000;
var obstacles = [];
var distance = 0;
var gameActive = true;
var spawnRate = 60;
var spawnCounter = 0;
var difficulty = 1;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
size: 100,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 120;
var lanePositions = [256, 768, 1280, 1792];
game.move = function (x, y, obj) {
if (!gameActive) return;
var targetX = x;
targetX = Math.max(motorcycle.minX, Math.min(motorcycle.maxX, targetX));
motorcycle.velocity.x = (targetX - motorcycle.x) * 0.15;
motorcycle.x += motorcycle.velocity.x;
};
game.update = function () {
if (!gameActive) return;
motorcycle.update();
spawnCounter++;
if (spawnCounter >= Math.max(30, spawnRate - Math.floor(difficulty))) {
spawnCounter = 0;
var randomLane = Math.floor(Math.random() * 4);
var newObstacle = new Obstacle();
newObstacle.x = lanePositions[randomLane];
newObstacle.y = -100;
newObstacle.lastY = newObstacle.y;
newObstacle.lastWasColliding = false;
newObstacle.hasBeenScored = false;
newObstacle.setDifficulty(difficulty);
obstacles.push(newObstacle);
game.addChild(newObstacle);
LK.getSound('whoosh').play();
}
distance++;
var previousDifficulty = difficulty;
difficulty = 1 + Math.floor(distance / 500);
if (difficulty > previousDifficulty) {
for (var j = 0; j < obstacles.length; j++) {
obstacles[j].setDifficulty(difficulty);
}
}
var currentScore = LK.getScore();
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
if (obstacle.lastY <= 2732 && obstacle.y > 2732) {
currentScore = currentScore + 1;
LK.setScore(currentScore);
scoreTxt.setText('Score: ' + currentScore);
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
var isColliding = motorcycle.intersects(obstacle);
if (!obstacle.lastWasColliding && isColliding) {
gameActive = false;
LK.getSound('crash').play();
LK.effects.flashScreen(0xFF0000, 500);
if (currentScore > storage.highScore) {
storage.highScore = currentScore;
highScoreTxt.setText('High Score: ' + storage.highScore);
}
LK.showGameOver();
return;
}
obstacle.lastWasColliding = isColliding;
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Motorcycle = Container.expand(function () {
var self = Container.call(this);
var motorcycleGraphics = self.attachAsset('motorcycle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.5,
scaleY: 2.5
});
self.speed = 15;
self.moveLeft = false;
self.moveRight = false;
self.moveBackward = false;
self.moveForward = true;
self.laneWidth = 512;
self.currentLane = 1;
self.minX = 256;
self.maxX = 1792;
self.velocity = {
x: 0,
y: 0
};
self.update = function () {};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.0,
scaleY: 1.2
});
self.baseSpeed = 12;
self.speed = 12;
self.lastY = self.y;
self.baseScaleX = 1.0;
self.baseScaleY = 1.2;
self.setDifficulty = function (difficultyLevel) {
self.speed = self.baseSpeed + (difficultyLevel - 1) * 2;
var newScaleX = self.baseScaleX + (difficultyLevel - 1) * 0.15;
var newScaleY = self.baseScaleY + (difficultyLevel - 1) * 0.2;
tween(obstacleGraphics, {
scaleX: newScaleX,
scaleY: newScaleY
}, {
duration: 500,
easing: tween.easeOut
});
};
self.update = function () {
self.lastY = self.y;
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var roadGraphic = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.0
});
roadGraphic.x = 1024;
roadGraphic.y = 0;
game.addChild(roadGraphic);
var motorcycle = game.addChild(new Motorcycle());
motorcycle.x = 1024;
motorcycle.y = 2000;
var obstacles = [];
var distance = 0;
var gameActive = true;
var spawnRate = 60;
var spawnCounter = 0;
var difficulty = 1;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
size: 100,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 120;
var lanePositions = [256, 768, 1280, 1792];
game.move = function (x, y, obj) {
if (!gameActive) return;
var targetX = x;
targetX = Math.max(motorcycle.minX, Math.min(motorcycle.maxX, targetX));
motorcycle.velocity.x = (targetX - motorcycle.x) * 0.15;
motorcycle.x += motorcycle.velocity.x;
};
game.update = function () {
if (!gameActive) return;
motorcycle.update();
spawnCounter++;
if (spawnCounter >= Math.max(30, spawnRate - Math.floor(difficulty))) {
spawnCounter = 0;
var randomLane = Math.floor(Math.random() * 4);
var newObstacle = new Obstacle();
newObstacle.x = lanePositions[randomLane];
newObstacle.y = -100;
newObstacle.lastY = newObstacle.y;
newObstacle.lastWasColliding = false;
newObstacle.hasBeenScored = false;
newObstacle.setDifficulty(difficulty);
obstacles.push(newObstacle);
game.addChild(newObstacle);
LK.getSound('whoosh').play();
}
distance++;
var previousDifficulty = difficulty;
difficulty = 1 + Math.floor(distance / 500);
if (difficulty > previousDifficulty) {
for (var j = 0; j < obstacles.length; j++) {
obstacles[j].setDifficulty(difficulty);
}
}
var currentScore = LK.getScore();
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
if (obstacle.lastY <= 2732 && obstacle.y > 2732) {
currentScore = currentScore + 1;
LK.setScore(currentScore);
scoreTxt.setText('Score: ' + currentScore);
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
var isColliding = motorcycle.intersects(obstacle);
if (!obstacle.lastWasColliding && isColliding) {
gameActive = false;
LK.getSound('crash').play();
LK.effects.flashScreen(0xFF0000, 500);
if (currentScore > storage.highScore) {
storage.highScore = currentScore;
highScoreTxt.setText('High Score: ' + storage.highScore);
}
LK.showGameOver();
return;
}
obstacle.lastWasColliding = isColliding;
}
};