/**** * Classes ****/ // Apple class var Apple = Container.expand(function () { var self = Container.call(this); var appleGraphics = self.attachAsset('apple', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.move = function () { self.y += self.speed; }; }); // 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; }; }); // Assets are automatically created based on usage in the code. // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.move = function (x, y) { self.x = x; self.y = y; }; self.shoot = function () { var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y - 50; // Shoot from above the player game.addChild(bullet); bullets.push(bullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ var player = game.addChild(new Player()); player.x = 1024; // Center horizontally player.y = 2500; // Position towards the bottom var bullets = []; var apples = []; // Touch event to move player and shoot game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); player.move(pos.x, pos.y); player.shoot(); }); // Game tick event LK.on('tick', function () { // Spawn apples if (LK.ticks % 60 == 0) { // Every 60 ticks var apple = new Apple(); apple.x = Math.random() * 2048; // Random horizontal position apple.y = 0; // Spawn at the top of the screen game.addChild(apple); apples.push(apple); } // Move bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].move(); if (bullets[i].y < 0) { bullets[i].destroy(); bullets.splice(i, 1); } } // Move apples for (var i = apples.length - 1; i >= 0; i--) { apples[i].move(); if (apples[i].y > 2732) { // If apple has reached the bottom of the screen apples[i].destroy(); apples.splice(i, 1); } // Check for collision with bullets for (var j = bullets.length - 1; j >= 0; j--) { if (apples[i].intersects(bullets[j])) { // Destroy both the apple and the bullet apples[i].destroy(); apples.splice(i, 1); bullets[j].destroy(); bullets.splice(j, 1); // Update score LK.setScore(LK.getScore() + 1); break; } } // Check for collision with player if (apples[i].intersects(player)) { // Game over LK.showGameOver(); break; } } });
/****
* Classes
****/
// Apple class
var Apple = Container.expand(function () {
var self = Container.call(this);
var appleGraphics = self.attachAsset('apple', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.move = function () {
self.y += self.speed;
};
});
// 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;
};
});
// Assets are automatically created based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = y;
};
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y - 50; // Shoot from above the player
game.addChild(bullet);
bullets.push(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2500; // Position towards the bottom
var bullets = [];
var apples = [];
// Touch event to move player and shoot
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
player.move(pos.x, pos.y);
player.shoot();
});
// Game tick event
LK.on('tick', function () {
// Spawn apples
if (LK.ticks % 60 == 0) {
// Every 60 ticks
var apple = new Apple();
apple.x = Math.random() * 2048; // Random horizontal position
apple.y = 0; // Spawn at the top of the screen
game.addChild(apple);
apples.push(apple);
}
// Move bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].move();
if (bullets[i].y < 0) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Move apples
for (var i = apples.length - 1; i >= 0; i--) {
apples[i].move();
if (apples[i].y > 2732) {
// If apple has reached the bottom of the screen
apples[i].destroy();
apples.splice(i, 1);
}
// Check for collision with bullets
for (var j = bullets.length - 1; j >= 0; j--) {
if (apples[i].intersects(bullets[j])) {
// Destroy both the apple and the bullet
apples[i].destroy();
apples.splice(i, 1);
bullets[j].destroy();
bullets.splice(j, 1);
// Update score
LK.setScore(LK.getScore() + 1);
break;
}
}
// Check for collision with player
if (apples[i].intersects(player)) {
// Game over
LK.showGameOver();
break;
}
}
});