/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Dino = Container.expand(function () {
var self = Container.call(this);
var dinoGraphics = self.attachAsset('dino', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.isDucking = false;
self.velocityY = 0;
self.groundY = 0;
self.jumpForce = -25;
self.gravity = 0.8;
self.normalHeight = 120;
self.duckHeight = 60;
self.jump = function () {
if (!self.isJumping && !self.isDucking) {
self.isJumping = true;
self.velocityY = self.jumpForce;
LK.getSound('jump').play();
}
};
self.duck = function () {
if (!self.isJumping && !self.isDucking) {
self.isDucking = true;
dinoGraphics.height = self.duckHeight;
dinoGraphics.scaleY = 0.5;
LK.getSound('duck').play();
}
};
self.stopDuck = function () {
if (self.isDucking) {
self.isDucking = false;
dinoGraphics.height = self.normalHeight;
dinoGraphics.scaleY = 1.0;
}
};
self.update = function () {
if (self.isJumping) {
self.velocityY += self.gravity;
self.y += self.velocityY;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocityY = 0;
self.isJumping = false;
}
}
};
return self;
});
var Obstacle = Container.expand(function (type) {
var self = Container.call(this);
self.type = type;
self.speed = 0;
if (type === 'cactus') {
var obstacleGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 1.0
});
self.y = 0;
} else if (type === 'pterodactyl') {
var obstacleGraphics = self.attachAsset('pterodactyl', {
anchorX: 0.5,
anchorY: 0.5
});
self.y = -150;
}
self.update = function () {
self.x -= self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Game variables
var dino;
var obstacles = [];
var gameSpeed = 8;
var baseSpeed = 8;
var maxSpeed = 20;
var speedIncrease = 0.005;
var obstacleSpawnTimer = 0;
var obstacleSpawnInterval = 120;
var groundY = 2732 - 200;
var distance = 0;
var isGameRunning = true;
var isPressed = false;
// Create ground
var ground = LK.getAsset('ground', {
anchorX: 0,
anchorY: 1.0,
x: 0,
y: groundY + 100
});
game.addChild(ground);
// Create dino
dino = new Dino();
dino.x = 300;
dino.y = groundY;
dino.groundY = groundY;
game.addChild(dino);
// Score display
var scoreText = new Text2('0', {
size: 120,
fill: 0x000000
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Distance display
var distanceText = new Text2('Distance: 0m', {
size: 80,
fill: 0x000000
});
distanceText.anchor.set(1, 0);
distanceText.x = -50;
distanceText.y = 50;
LK.gui.topRight.addChild(distanceText);
// High score display
var highScoreText = new Text2('Best: ' + storage.highScore, {
size: 80,
fill: 0x000000
});
highScoreText.anchor.set(1, 0);
highScoreText.x = -50;
highScoreText.y = 150;
LK.gui.topRight.addChild(highScoreText);
function spawnObstacle() {
var obstacleType = Math.random() < 0.7 ? 'cactus' : 'pterodactyl';
var obstacle = new Obstacle(obstacleType);
obstacle.x = 2048 + 100;
obstacle.y = obstacleType === 'cactus' ? groundY : groundY - 150;
obstacle.speed = gameSpeed;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function checkCollisions() {
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
if (dino.intersects(obstacle)) {
LK.getSound('hit').play();
isGameRunning = false;
// Check and update high score
if (distance > storage.highScore) {
storage.highScore = distance;
highScoreText.setText('Best: ' + storage.highScore);
}
LK.showGameOver();
return;
}
}
}
function updateScore() {
distance = Math.floor(LK.ticks / 6);
LK.setScore(distance);
scoreText.setText(distance.toString());
distanceText.setText('Distance: ' + distance + 'm');
}
function updateGameSpeed() {
gameSpeed = Math.min(maxSpeed, baseSpeed + distance * speedIncrease);
// Adjust spawn interval based on speed
obstacleSpawnInterval = Math.max(60, 120 - distance * 0.1);
}
// Input handling
game.down = function (x, y, obj) {
if (isGameRunning) {
isPressed = true;
dino.jump();
}
};
game.up = function (x, y, obj) {
if (isGameRunning) {
isPressed = false;
dino.stopDuck();
}
};
// Main game loop
game.update = function () {
if (!isGameRunning) return;
// Handle ducking
if (isPressed && !dino.isJumping) {
dino.duck();
}
// Update game speed
updateGameSpeed();
// Spawn obstacles
obstacleSpawnTimer++;
if (obstacleSpawnTimer >= obstacleSpawnInterval) {
spawnObstacle();
obstacleSpawnTimer = 0;
}
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.speed = gameSpeed;
// Remove obstacles that are off-screen
if (obstacle.x < -200) {
obstacle.destroy();
obstacles.splice(i, 1);
}
}
// Check collisions
checkCollisions();
// Update score
updateScore();
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Dino = Container.expand(function () {
var self = Container.call(this);
var dinoGraphics = self.attachAsset('dino', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.isDucking = false;
self.velocityY = 0;
self.groundY = 0;
self.jumpForce = -25;
self.gravity = 0.8;
self.normalHeight = 120;
self.duckHeight = 60;
self.jump = function () {
if (!self.isJumping && !self.isDucking) {
self.isJumping = true;
self.velocityY = self.jumpForce;
LK.getSound('jump').play();
}
};
self.duck = function () {
if (!self.isJumping && !self.isDucking) {
self.isDucking = true;
dinoGraphics.height = self.duckHeight;
dinoGraphics.scaleY = 0.5;
LK.getSound('duck').play();
}
};
self.stopDuck = function () {
if (self.isDucking) {
self.isDucking = false;
dinoGraphics.height = self.normalHeight;
dinoGraphics.scaleY = 1.0;
}
};
self.update = function () {
if (self.isJumping) {
self.velocityY += self.gravity;
self.y += self.velocityY;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocityY = 0;
self.isJumping = false;
}
}
};
return self;
});
var Obstacle = Container.expand(function (type) {
var self = Container.call(this);
self.type = type;
self.speed = 0;
if (type === 'cactus') {
var obstacleGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 1.0
});
self.y = 0;
} else if (type === 'pterodactyl') {
var obstacleGraphics = self.attachAsset('pterodactyl', {
anchorX: 0.5,
anchorY: 0.5
});
self.y = -150;
}
self.update = function () {
self.x -= self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Game variables
var dino;
var obstacles = [];
var gameSpeed = 8;
var baseSpeed = 8;
var maxSpeed = 20;
var speedIncrease = 0.005;
var obstacleSpawnTimer = 0;
var obstacleSpawnInterval = 120;
var groundY = 2732 - 200;
var distance = 0;
var isGameRunning = true;
var isPressed = false;
// Create ground
var ground = LK.getAsset('ground', {
anchorX: 0,
anchorY: 1.0,
x: 0,
y: groundY + 100
});
game.addChild(ground);
// Create dino
dino = new Dino();
dino.x = 300;
dino.y = groundY;
dino.groundY = groundY;
game.addChild(dino);
// Score display
var scoreText = new Text2('0', {
size: 120,
fill: 0x000000
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Distance display
var distanceText = new Text2('Distance: 0m', {
size: 80,
fill: 0x000000
});
distanceText.anchor.set(1, 0);
distanceText.x = -50;
distanceText.y = 50;
LK.gui.topRight.addChild(distanceText);
// High score display
var highScoreText = new Text2('Best: ' + storage.highScore, {
size: 80,
fill: 0x000000
});
highScoreText.anchor.set(1, 0);
highScoreText.x = -50;
highScoreText.y = 150;
LK.gui.topRight.addChild(highScoreText);
function spawnObstacle() {
var obstacleType = Math.random() < 0.7 ? 'cactus' : 'pterodactyl';
var obstacle = new Obstacle(obstacleType);
obstacle.x = 2048 + 100;
obstacle.y = obstacleType === 'cactus' ? groundY : groundY - 150;
obstacle.speed = gameSpeed;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function checkCollisions() {
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
if (dino.intersects(obstacle)) {
LK.getSound('hit').play();
isGameRunning = false;
// Check and update high score
if (distance > storage.highScore) {
storage.highScore = distance;
highScoreText.setText('Best: ' + storage.highScore);
}
LK.showGameOver();
return;
}
}
}
function updateScore() {
distance = Math.floor(LK.ticks / 6);
LK.setScore(distance);
scoreText.setText(distance.toString());
distanceText.setText('Distance: ' + distance + 'm');
}
function updateGameSpeed() {
gameSpeed = Math.min(maxSpeed, baseSpeed + distance * speedIncrease);
// Adjust spawn interval based on speed
obstacleSpawnInterval = Math.max(60, 120 - distance * 0.1);
}
// Input handling
game.down = function (x, y, obj) {
if (isGameRunning) {
isPressed = true;
dino.jump();
}
};
game.up = function (x, y, obj) {
if (isGameRunning) {
isPressed = false;
dino.stopDuck();
}
};
// Main game loop
game.update = function () {
if (!isGameRunning) return;
// Handle ducking
if (isPressed && !dino.isJumping) {
dino.duck();
}
// Update game speed
updateGameSpeed();
// Spawn obstacles
obstacleSpawnTimer++;
if (obstacleSpawnTimer >= obstacleSpawnInterval) {
spawnObstacle();
obstacleSpawnTimer = 0;
}
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.speed = gameSpeed;
// Remove obstacles that are off-screen
if (obstacle.x < -200) {
obstacle.destroy();
obstacles.splice(i, 1);
}
}
// Check collisions
checkCollisions();
// Update score
updateScore();
};