/****
* Classes
****/
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 1.0,
width: 100,
height: 100
});
self.speed = 10;
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Dinosaur class
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinoGraphics = self.attachAsset('dino', {
anchorX: 0.5,
anchorY: 1.0,
width: 200,
height: 200
});
self.isJumping = false;
self.jumpSpeed = 0;
self.gravity = 0.5;
self.groundY = ground.y - ground.height; // Ground level
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
// Check if dinosaur has jumped over an obstacle
for (var i = 0; i < obstacles.length; i++) {
if (obstacles[i].x < self.x && obstacles[i] instanceof Obstacle && !obstacles[i].jumpedOver) {
obstacles[i].jumpedOver = true;
}
}
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = -20;
LK.getSound('jump').play();
}
};
});
// Ground class
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('road', {
anchorX: 0.5,
anchorY: 1.0,
width: 4096
});
self.speed = 10;
self.update = function () {
self.x -= self.speed;
if (self.x < -self.width / 2) {
self.x = 0;
var newGround = new Ground();
newGround.x = self.width;
newGround.y = self.y;
game.addChild(newGround);
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 1.0,
width: 200,
height: 200
});
self.speed = 10;
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF // Init game with white background
});
/****
* Game Code
****/
// Initialize game variables
var desertBackground = game.addChild(LK.getAsset('desert', {
anchorX: 0,
anchorY: 0
}));
var ground = game.addChild(new Ground());
ground.x = 0;
ground.y = 2732;
var dinosaur = game.addChild(new Dinosaur());
dinosaur.x = 200;
dinosaur.y = ground.y - ground.height;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0x000000
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048;
obstacle.y = ground.y - ground.height;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function spawnBird() {
var bird = new Bird();
bird.x = 2048;
bird.y = Math.random() * (dinosaur.y - dinosaur.height - 200);
obstacles.push(bird);
game.addChild(bird);
}
// Handle game updates
game.update = function () {
dinosaur.update();
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (dinosaur.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (obstacles[i].x < -100) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
if (LK.ticks % 60 == 0) {
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].x < dinosaur.x && !obstacles[i].jumpedOver && obstacles[i] instanceof Obstacle) {
score++;
scoreTxt.setText(score);
obstacles[i].jumpedOver = true;
}
if (obstacles[i].x < -100) {
obstacles.splice(i, 1);
}
}
}
if (LK.ticks % 120 == 0) {
spawnObstacle();
}
if (LK.ticks % 180 == 0) {
spawnBird();
}
};
// Handle user input for jumping
game.down = function (x, y, obj) {
dinosaur.jump();
}; /****
* Classes
****/
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 1.0,
width: 100,
height: 100
});
self.speed = 10;
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Dinosaur class
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinoGraphics = self.attachAsset('dino', {
anchorX: 0.5,
anchorY: 1.0,
width: 200,
height: 200
});
self.isJumping = false;
self.jumpSpeed = 0;
self.gravity = 0.5;
self.groundY = ground.y - ground.height; // Ground level
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
// Check if dinosaur has jumped over an obstacle
for (var i = 0; i < obstacles.length; i++) {
if (obstacles[i].x < self.x && obstacles[i] instanceof Obstacle && !obstacles[i].jumpedOver) {
obstacles[i].jumpedOver = true;
}
}
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = -20;
LK.getSound('jump').play();
}
};
});
// Ground class
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('road', {
anchorX: 0.5,
anchorY: 1.0,
width: 4096
});
self.speed = 10;
self.update = function () {
self.x -= self.speed;
if (self.x < -self.width / 2) {
self.x = 0;
var newGround = new Ground();
newGround.x = self.width;
newGround.y = self.y;
game.addChild(newGround);
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 1.0,
width: 200,
height: 200
});
self.speed = 10;
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF // Init game with white background
});
/****
* Game Code
****/
// Initialize game variables
var desertBackground = game.addChild(LK.getAsset('desert', {
anchorX: 0,
anchorY: 0
}));
var ground = game.addChild(new Ground());
ground.x = 0;
ground.y = 2732;
var dinosaur = game.addChild(new Dinosaur());
dinosaur.x = 200;
dinosaur.y = ground.y - ground.height;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0x000000
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048;
obstacle.y = ground.y - ground.height;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function spawnBird() {
var bird = new Bird();
bird.x = 2048;
bird.y = Math.random() * (dinosaur.y - dinosaur.height - 200);
obstacles.push(bird);
game.addChild(bird);
}
// Handle game updates
game.update = function () {
dinosaur.update();
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (dinosaur.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (obstacles[i].x < -100) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
if (LK.ticks % 60 == 0) {
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].x < dinosaur.x && !obstacles[i].jumpedOver && obstacles[i] instanceof Obstacle) {
score++;
scoreTxt.setText(score);
obstacles[i].jumpedOver = true;
}
if (obstacles[i].x < -100) {
obstacles.splice(i, 1);
}
}
}
if (LK.ticks % 120 == 0) {
spawnObstacle();
}
if (LK.ticks % 180 == 0) {
spawnBird();
}
};
// Handle user input for jumping
game.down = function (x, y, obj) {
dinosaur.jump();
};