/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.hasShield = false;
self.shieldAlpha = 1;
self.activateShield = function () {
self.hasShield = true;
self.shieldAlpha = 1;
};
self.update = function () {
if (self.hasShield) {
self.shieldAlpha -= 0.008;
if (self.shieldAlpha <= 0) {
self.hasShield = false;
carGraphics.alpha = 1;
} else {
carGraphics.alpha = 0.5 + self.shieldAlpha * 0.5;
carGraphics.tint = 0x00D9FF;
}
} else {
carGraphics.alpha = 1;
carGraphics.tint = 0xFFFFFF;
}
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.update = function () {
self.y += self.speed;
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
self.type = 'speed';
var powerUpGraphics = self.attachAsset('powerUpSpeed', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 6;
self.rotation = 0;
self.update = function () {
self.y += self.speed;
self.rotation += 0.05;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2400;
var obstacles = [];
var powerups = [];
var gameSpeed = 8;
var gameSpeedMax = 18;
var spawnRate = 40;
var score = 0;
var isGameActive = true;
var hasCollided = false;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: '#FFFFFF'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var speedTxt = new Text2('Speed: 1.0x', {
size: 80,
fill: '#FFD700'
});
speedTxt.anchor.set(0.5, 0);
speedTxt.y = 120;
LK.gui.top.addChild(speedTxt);
var trackLeft = 200;
var trackRight = 1848;
var laneWidth = (trackRight - trackLeft) / 3;
var touchX = car.x;
game.down = function (x, y, obj) {
touchX = x;
};
game.move = function (x, y, obj) {
if (!isGameActive) return;
touchX = x;
var newX = touchX;
if (newX < trackLeft) newX = trackLeft;
if (newX > trackRight) newX = trackRight;
car.x = newX;
};
game.up = function (x, y, obj) {};
function spawnObstacle() {
var newObstacle = new Obstacle();
var lane = Math.floor(Math.random() * 3);
newObstacle.x = trackLeft + lane * laneWidth + laneWidth / 2;
newObstacle.y = -100;
newObstacle.speed = gameSpeed;
newObstacle.lastY = newObstacle.y;
newObstacle.lastIntersecting = false;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
function spawnPowerUp() {
var newPowerUp = new PowerUp();
var lane = Math.floor(Math.random() * 3);
newPowerUp.x = trackLeft + lane * laneWidth + laneWidth / 2;
newPowerUp.y = -100;
newPowerUp.speed = gameSpeed;
var powerType = Math.random();
if (powerType < 0.6) {
newPowerUp.type = 'speed';
newPowerUp.attachAsset('powerUpSpeed', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
newPowerUp.type = 'shield';
newPowerUp.attachAsset('powerUpShield', {
anchorX: 0.5,
anchorY: 0.5
});
}
newPowerUp.lastY = newPowerUp.y;
newPowerUp.lastIntersecting = false;
powerups.push(newPowerUp);
game.addChild(newPowerUp);
}
game.update = function () {
if (!isGameActive) return;
score += 1;
scoreTxt.setText('Score: ' + Math.floor(score / 10));
if (LK.ticks % 600 === 0 && gameSpeed < gameSpeedMax) {
gameSpeed += 1;
spawnRate = Math.max(25, spawnRate - 2);
}
var speedMultiplier = gameSpeed / 8;
speedTxt.setText('Speed: ' + speedMultiplier.toFixed(1) + 'x');
if (LK.ticks % spawnRate === 0) {
spawnObstacle();
}
if (LK.ticks % 150 === 0) {
spawnPowerUp();
}
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
if (obstacle.lastY === undefined) obstacle.lastY = obstacle.y;
if (obstacle.lastIntersecting === undefined) obstacle.lastIntersecting = false;
if (obstacle.lastY >= -50 && obstacle.y < -50) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
var currentIntersecting = obstacle.intersects(car);
if (!obstacle.lastIntersecting && currentIntersecting) {
if (!car.hasShield) {
isGameActive = false;
LK.getSound('collision').play();
LK.effects.flashScreen(0xFF0000, 500);
LK.setScore(Math.floor(score / 10));
LK.showGameOver();
return;
} else {
car.hasShield = false;
car.shieldAlpha = 0;
LK.getSound('powerup').play();
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
}
obstacle.lastY = obstacle.y;
obstacle.lastIntersecting = currentIntersecting;
}
for (var j = powerups.length - 1; j >= 0; j--) {
var powerup = powerups[j];
if (powerup.lastY === undefined) powerup.lastY = powerup.y;
if (powerup.lastIntersecting === undefined) powerup.lastIntersecting = false;
if (powerup.lastY >= -50 && powerup.y < -50) {
powerup.destroy();
powerups.splice(j, 1);
continue;
}
var powerupIntersecting = powerup.intersects(car);
if (!powerup.lastIntersecting && powerupIntersecting) {
if (powerup.type === 'speed') {
gameSpeed = Math.min(gameSpeedMax, gameSpeed + 3);
} else if (powerup.type === 'shield') {
car.activateShield();
}
LK.getSound('powerup').play();
score += 50;
powerup.destroy();
powerups.splice(j, 1);
continue;
}
powerup.lastY = powerup.y;
powerup.lastIntersecting = powerupIntersecting;
}
};
LK.playMusic('bgmusic', {
loop: true
}); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.hasShield = false;
self.shieldAlpha = 1;
self.activateShield = function () {
self.hasShield = true;
self.shieldAlpha = 1;
};
self.update = function () {
if (self.hasShield) {
self.shieldAlpha -= 0.008;
if (self.shieldAlpha <= 0) {
self.hasShield = false;
carGraphics.alpha = 1;
} else {
carGraphics.alpha = 0.5 + self.shieldAlpha * 0.5;
carGraphics.tint = 0x00D9FF;
}
} else {
carGraphics.alpha = 1;
carGraphics.tint = 0xFFFFFF;
}
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.update = function () {
self.y += self.speed;
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
self.type = 'speed';
var powerUpGraphics = self.attachAsset('powerUpSpeed', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 6;
self.rotation = 0;
self.update = function () {
self.y += self.speed;
self.rotation += 0.05;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2400;
var obstacles = [];
var powerups = [];
var gameSpeed = 8;
var gameSpeedMax = 18;
var spawnRate = 40;
var score = 0;
var isGameActive = true;
var hasCollided = false;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: '#FFFFFF'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var speedTxt = new Text2('Speed: 1.0x', {
size: 80,
fill: '#FFD700'
});
speedTxt.anchor.set(0.5, 0);
speedTxt.y = 120;
LK.gui.top.addChild(speedTxt);
var trackLeft = 200;
var trackRight = 1848;
var laneWidth = (trackRight - trackLeft) / 3;
var touchX = car.x;
game.down = function (x, y, obj) {
touchX = x;
};
game.move = function (x, y, obj) {
if (!isGameActive) return;
touchX = x;
var newX = touchX;
if (newX < trackLeft) newX = trackLeft;
if (newX > trackRight) newX = trackRight;
car.x = newX;
};
game.up = function (x, y, obj) {};
function spawnObstacle() {
var newObstacle = new Obstacle();
var lane = Math.floor(Math.random() * 3);
newObstacle.x = trackLeft + lane * laneWidth + laneWidth / 2;
newObstacle.y = -100;
newObstacle.speed = gameSpeed;
newObstacle.lastY = newObstacle.y;
newObstacle.lastIntersecting = false;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
function spawnPowerUp() {
var newPowerUp = new PowerUp();
var lane = Math.floor(Math.random() * 3);
newPowerUp.x = trackLeft + lane * laneWidth + laneWidth / 2;
newPowerUp.y = -100;
newPowerUp.speed = gameSpeed;
var powerType = Math.random();
if (powerType < 0.6) {
newPowerUp.type = 'speed';
newPowerUp.attachAsset('powerUpSpeed', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
newPowerUp.type = 'shield';
newPowerUp.attachAsset('powerUpShield', {
anchorX: 0.5,
anchorY: 0.5
});
}
newPowerUp.lastY = newPowerUp.y;
newPowerUp.lastIntersecting = false;
powerups.push(newPowerUp);
game.addChild(newPowerUp);
}
game.update = function () {
if (!isGameActive) return;
score += 1;
scoreTxt.setText('Score: ' + Math.floor(score / 10));
if (LK.ticks % 600 === 0 && gameSpeed < gameSpeedMax) {
gameSpeed += 1;
spawnRate = Math.max(25, spawnRate - 2);
}
var speedMultiplier = gameSpeed / 8;
speedTxt.setText('Speed: ' + speedMultiplier.toFixed(1) + 'x');
if (LK.ticks % spawnRate === 0) {
spawnObstacle();
}
if (LK.ticks % 150 === 0) {
spawnPowerUp();
}
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
if (obstacle.lastY === undefined) obstacle.lastY = obstacle.y;
if (obstacle.lastIntersecting === undefined) obstacle.lastIntersecting = false;
if (obstacle.lastY >= -50 && obstacle.y < -50) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
var currentIntersecting = obstacle.intersects(car);
if (!obstacle.lastIntersecting && currentIntersecting) {
if (!car.hasShield) {
isGameActive = false;
LK.getSound('collision').play();
LK.effects.flashScreen(0xFF0000, 500);
LK.setScore(Math.floor(score / 10));
LK.showGameOver();
return;
} else {
car.hasShield = false;
car.shieldAlpha = 0;
LK.getSound('powerup').play();
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
}
obstacle.lastY = obstacle.y;
obstacle.lastIntersecting = currentIntersecting;
}
for (var j = powerups.length - 1; j >= 0; j--) {
var powerup = powerups[j];
if (powerup.lastY === undefined) powerup.lastY = powerup.y;
if (powerup.lastIntersecting === undefined) powerup.lastIntersecting = false;
if (powerup.lastY >= -50 && powerup.y < -50) {
powerup.destroy();
powerups.splice(j, 1);
continue;
}
var powerupIntersecting = powerup.intersects(car);
if (!powerup.lastIntersecting && powerupIntersecting) {
if (powerup.type === 'speed') {
gameSpeed = Math.min(gameSpeedMax, gameSpeed + 3);
} else if (powerup.type === 'shield') {
car.activateShield();
}
LK.getSound('powerup').play();
score += 50;
powerup.destroy();
powerups.splice(j, 1);
continue;
}
powerup.lastY = powerup.y;
powerup.lastIntersecting = powerupIntersecting;
}
};
LK.playMusic('bgmusic', {
loop: true
});