/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -40;
self.update = function () {
self.x += self.speed;
};
});
var Cactus = Container.expand(function () {
var self = Container.call(this);
var cactusGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -40;
self.update = function () {
self.x += self.speed;
};
});
var Dino = Container.expand(function () {
var self = Container.call(this);
var dinoGraphics = self.attachAsset('dino', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.gravity = 1;
self.jumpStrength = -20;
self.isJumping = false;
self.update = function () {
if (self.isJumping) {
self.speed += self.gravity;
self.y += self.speed;
if (self.y > 2732 - dinoGraphics.height) {
self.y = 2732 - dinoGraphics.height;
self.speed = 0;
self.isJumping = false;
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.speed = self.jumpStrength;
}
};
});
/****
* Initialize Game
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Dino class representing the player character
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var dino = game.addChild(new Dino());
dino.x = 200;
dino.y = 2732 - dino.height;
var obstacles = [];
game.update = function () {
if (LK.ticks % 120 == 0) {
var obstacle;
if (Math.random() < 0.5) {
obstacle = new Cactus();
obstacle.y = 2732 - obstacle.height;
} else {
obstacle = new Bird();
obstacle.y = 2732 - obstacle.height - 200;
}
obstacle.x = 2048;
obstacles.push(obstacle);
game.addChild(obstacle);
}
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].x < -obstacles[i].width) {
obstacles[i].destroy();
obstacles.splice(i, 1);
} else if (dino.intersects(obstacles[i])) {
LK.showGameOver();
}
}
if (LK.ticks % 7 == 0) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
}
};
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
dino.jump();
};