/**** * Classes ****/ // Background class for the game var Background = Container.expand(function () { var self = Container.call(this); var backgroundGraphics = self.createAsset('background_image', 'Background image for the game', 0, 0); // Set the background to cover the entire game area backgroundGraphics.width = game.width; backgroundGraphics.height = game.height; }); // Marshmallow character class var Character = Container.expand(function () { var self = Container.call(this); var characterGraphics = self.createAsset('marshmallow_clipart', 'Fluffy marshmallow character clipart', 0.5, 1); self.velocity = { x: 0, y: 0 }; self.isJumping = false; self.jump = function () { if (!self.isJumping) { self.velocity.y = -15; self.isJumping = true; } }; self.update = function () { self.y += self.velocity.y; self.velocity.y += 0.5; // gravity effect if (self.y > game.height - characterGraphics.height) { self.y = game.height - characterGraphics.height; self.isJumping = false; } }; }); // Trap class for obstacles var Trap = Container.expand(function () { var self = Container.call(this); var trapGraphics = self.createAsset('trap_clipart', 'Tasty trap obstacle clipart', 0.5, 1); self.speed = 5; self.move = function () { self.x -= self.speed; }; self.isOffScreen = function () { return self.x < -trapGraphics.width; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xFFFFFF // Init game with white background }); /**** * Game Code ****/ var background = game.addChild(new Background()); var character = game.addChild(new Character()); character.x = game.width / 4; character.y = game.height - 100; var traps = []; var scoreTxt = new Text2('0', { size: 150, fill: "#000000" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); game.on('down', function (obj) { character.jump(); }); var spawnTrap = function spawnTrap() { var trap = new Trap(); trap.x = game.width; trap.y = game.height - 50; traps.push(trap); game.addChild(trap); }; var updateScore = function updateScore() { LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); }; var gameOver = function gameOver() { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); }; LK.on('tick', function () { character.update(); for (var i = traps.length - 1; i >= 0; i--) { traps[i].move(); if (traps[i].isOffScreen()) { traps[i].destroy(); traps.splice(i, 1); } else if (character.intersects(traps[i])) { gameOver(); } } if (LK.ticks % 120 == 0) { spawnTrap(); } if (LK.ticks % 60 == 0) { updateScore(); } });
/****
* Classes
****/
// Background class for the game
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.createAsset('background_image', 'Background image for the game', 0, 0);
// Set the background to cover the entire game area
backgroundGraphics.width = game.width;
backgroundGraphics.height = game.height;
});
// Marshmallow character class
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.createAsset('marshmallow_clipart', 'Fluffy marshmallow character clipart', 0.5, 1);
self.velocity = {
x: 0,
y: 0
};
self.isJumping = false;
self.jump = function () {
if (!self.isJumping) {
self.velocity.y = -15;
self.isJumping = true;
}
};
self.update = function () {
self.y += self.velocity.y;
self.velocity.y += 0.5; // gravity effect
if (self.y > game.height - characterGraphics.height) {
self.y = game.height - characterGraphics.height;
self.isJumping = false;
}
};
});
// Trap class for obstacles
var Trap = Container.expand(function () {
var self = Container.call(this);
var trapGraphics = self.createAsset('trap_clipart', 'Tasty trap obstacle clipart', 0.5, 1);
self.speed = 5;
self.move = function () {
self.x -= self.speed;
};
self.isOffScreen = function () {
return self.x < -trapGraphics.width;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF // Init game with white background
});
/****
* Game Code
****/
var background = game.addChild(new Background());
var character = game.addChild(new Character());
character.x = game.width / 4;
character.y = game.height - 100;
var traps = [];
var scoreTxt = new Text2('0', {
size: 150,
fill: "#000000"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.on('down', function (obj) {
character.jump();
});
var spawnTrap = function spawnTrap() {
var trap = new Trap();
trap.x = game.width;
trap.y = game.height - 50;
traps.push(trap);
game.addChild(trap);
};
var updateScore = function updateScore() {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
};
var gameOver = function gameOver() {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
};
LK.on('tick', function () {
character.update();
for (var i = traps.length - 1; i >= 0; i--) {
traps[i].move();
if (traps[i].isOffScreen()) {
traps[i].destroy();
traps.splice(i, 1);
} else if (character.intersects(traps[i])) {
gameOver();
}
}
if (LK.ticks % 120 == 0) {
spawnTrap();
}
if (LK.ticks % 60 == 0) {
updateScore();
}
});