/**** * Classes ****/ // Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.move = function () { self.y += self.speed; }; }); // Meteor class var Meteor = Container.expand(function () { var self = Container.call(this); var meteorGraphics = self.attachAsset('meteor', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.move = function () { self.y += self.speed; }; }); // Assets are automatically created based on usage in the code. // Spaceship class var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.moveLeft = function () { self.x -= self.speed; }; self.moveRight = function () { self.x += self.speed; }; self.shoot = function () { var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y - 50; // Position bullet at the top of the spaceship game.addChild(bullet); bullets.push(bullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ var spaceship; var bullets = []; var meteors = []; var score = 0; var scoreTxt; function setup() { spaceship = game.addChild(new Spaceship()); spaceship.x = 1024; // Center horizontally spaceship.y = 2500; // Position near the bottom scoreTxt = new Text2(score.toString(), { size: 150, fill: "#ffffff" }); LK.gui.top.addChild(scoreTxt); game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); if (pos.x < 1024) { spaceship.moveLeft(); } else { spaceship.moveRight(); } }); game.on('up', function (obj) { spaceship.shoot(); }); LK.on('tick', function () { bullets.forEach(function (bullet, index) { bullet.move(); if (bullet.y < 0) { bullet.destroy(); bullets.splice(index, 1); } }); meteors.forEach(function (meteor, index) { meteor.move(); if (meteor.y > 2732) { meteor.destroy(); meteors.splice(index, 1); } bullets.forEach(function (bullet, bulletIndex) { if (bullet.intersects(meteor)) { meteor.destroy(); bullet.destroy(); meteors.splice(index, 1); bullets.splice(bulletIndex, 1); score += 10; scoreTxt.setText(score.toString()); } }); }); if (LK.ticks % 120 == 0) { // Spawn a meteor every 2 seconds var meteor = new Meteor(); meteor.x = Math.random() * 2048; meteor.y = 0; game.addChild(meteor); meteors.push(meteor); } }); } setup();
/****
* Classes
****/
// Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.move = function () {
self.y += self.speed;
};
});
// Meteor class
var Meteor = Container.expand(function () {
var self = Container.call(this);
var meteorGraphics = self.attachAsset('meteor', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.move = function () {
self.y += self.speed;
};
});
// Assets are automatically created based on usage in the code.
// Spaceship class
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.moveLeft = function () {
self.x -= self.speed;
};
self.moveRight = function () {
self.x += self.speed;
};
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y - 50; // Position bullet at the top of the spaceship
game.addChild(bullet);
bullets.push(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var spaceship;
var bullets = [];
var meteors = [];
var score = 0;
var scoreTxt;
function setup() {
spaceship = game.addChild(new Spaceship());
spaceship.x = 1024; // Center horizontally
spaceship.y = 2500; // Position near the bottom
scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
if (pos.x < 1024) {
spaceship.moveLeft();
} else {
spaceship.moveRight();
}
});
game.on('up', function (obj) {
spaceship.shoot();
});
LK.on('tick', function () {
bullets.forEach(function (bullet, index) {
bullet.move();
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(index, 1);
}
});
meteors.forEach(function (meteor, index) {
meteor.move();
if (meteor.y > 2732) {
meteor.destroy();
meteors.splice(index, 1);
}
bullets.forEach(function (bullet, bulletIndex) {
if (bullet.intersects(meteor)) {
meteor.destroy();
bullet.destroy();
meteors.splice(index, 1);
bullets.splice(bulletIndex, 1);
score += 10;
scoreTxt.setText(score.toString());
}
});
});
if (LK.ticks % 120 == 0) {
// Spawn a meteor every 2 seconds
var meteor = new Meteor();
meteor.x = Math.random() * 2048;
meteor.y = 0;
game.addChild(meteor);
meteors.push(meteor);
}
});
}
setup();