/**** * Classes ****/ // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 1.0 }); self.speed = -5; self.move = function () { self.x += self.speed; }; }); // Assets will be automatically created based on usage in the code. // Pony class var Pony = Container.expand(function () { var self = Container.call(this); var ponyGraphics = self.attachAsset('pony', { anchorX: 0.5, anchorY: 0.5 }); self.jumpSpeed = -15; self.gravity = 0.5; self.velocity = 0; self.isJumping = false; self.jump = function () { if (!self.isJumping) { self.velocity = self.jumpSpeed; self.isJumping = true; } }; self.update = function () { self.y += self.velocity; self.velocity += self.gravity; if (self.y > game.floorLevel) { self.y = game.floorLevel; self.isJumping = false; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background }); /**** * Game Code ****/ // Game variables game.floorLevel = 2732 - 200; // Floor level where the pony runs game.obstacles = []; game.score = 0; game.pony = game.addChild(new Pony()); game.pony.x = 400; game.pony.y = game.floorLevel; // Score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); LK.gui.top.addChild(scoreTxt); // Game tick event LK.on('tick', function () { game.pony.update(); // Move and check obstacles for (var i = game.obstacles.length - 1; i >= 0; i--) { game.obstacles[i].move(); if (game.obstacles[i].x < -100) { // Off-screen game.obstacles[i].destroy(); game.obstacles.splice(i, 1); } else if (game.pony.intersects(game.obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Increase score over time if (LK.ticks % 60 == 0) { // Every second game.score += 1; scoreTxt.setText(game.score.toString()); } // Add obstacles if (LK.ticks % 300 == 0) { // Every 5 seconds var newObstacle = new Obstacle(); newObstacle.x = 2048; newObstacle.y = game.floorLevel; game.obstacles.push(newObstacle); game.addChild(newObstacle); } }); // Touch event to make the pony jump game.on('down', function (obj) { game.pony.jump(); });
/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = -5;
self.move = function () {
self.x += self.speed;
};
});
// Assets will be automatically created based on usage in the code.
// Pony class
var Pony = Container.expand(function () {
var self = Container.call(this);
var ponyGraphics = self.attachAsset('pony', {
anchorX: 0.5,
anchorY: 0.5
});
self.jumpSpeed = -15;
self.gravity = 0.5;
self.velocity = 0;
self.isJumping = false;
self.jump = function () {
if (!self.isJumping) {
self.velocity = self.jumpSpeed;
self.isJumping = true;
}
};
self.update = function () {
self.y += self.velocity;
self.velocity += self.gravity;
if (self.y > game.floorLevel) {
self.y = game.floorLevel;
self.isJumping = false;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background
});
/****
* Game Code
****/
// Game variables
game.floorLevel = 2732 - 200; // Floor level where the pony runs
game.obstacles = [];
game.score = 0;
game.pony = game.addChild(new Pony());
game.pony.x = 400;
game.pony.y = game.floorLevel;
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Game tick event
LK.on('tick', function () {
game.pony.update();
// Move and check obstacles
for (var i = game.obstacles.length - 1; i >= 0; i--) {
game.obstacles[i].move();
if (game.obstacles[i].x < -100) {
// Off-screen
game.obstacles[i].destroy();
game.obstacles.splice(i, 1);
} else if (game.pony.intersects(game.obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Increase score over time
if (LK.ticks % 60 == 0) {
// Every second
game.score += 1;
scoreTxt.setText(game.score.toString());
}
// Add obstacles
if (LK.ticks % 300 == 0) {
// Every 5 seconds
var newObstacle = new Obstacle();
newObstacle.x = 2048;
newObstacle.y = game.floorLevel;
game.obstacles.push(newObstacle);
game.addChild(newObstacle);
}
});
// Touch event to make the pony jump
game.on('down', function (obj) {
game.pony.jump();
});