/****
* Classes
****/
// RedEnemy class
var RedEnemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('redEnemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
self.move = function () {
// Calculate direction towards player
var dx = player.x - self.x;
var dy = player.y - self.y;
var len = Math.sqrt(dx * dx + dy * dy);
dx /= len;
dy /= len;
// Move towards player
self.x += dx * self.speed;
self.y += dy * self.speed;
// Rotate to face direction of movement
self.rotation = Math.atan2(dy, dx);
};
});
// 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;
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 18;
self.move = function () {
self.y += self.speed;
};
});
// Laser class
var Laser = Container.expand(function () {
var self = Container.call(this);
var laserGraphics = self.attachAsset('laser', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.move = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Fire lasers when the game area is clicked
// Initialize player, enemy, and laser assets for the game.
var scoreTxt = new Text2('0', {
size: 100,
fill: '#ffffff'
});
LK.gui.top.addChild(scoreTxt);
scoreTxt.x -= 100;
var score = 0;
var canShoot = true;
game.on('down', function () {
if (canShoot) {
var laser = new Laser();
laser.x = player.x;
laser.y = player.y - 30; // Start slightly above the player
lasers.push(laser);
game.addChild(laser);
canShoot = false;
LK.setTimeout(function () {
canShoot = true;
}, 200);
}
});
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2500; // Position towards the bottom
var enemies = [];
var lasers = [];
// Create enemies at intervals
var enemySpawnTimer = LK.setInterval(function () {
// Randomly decide whether to create a normal enemy or a red enemy
var isRed = Math.random() < 0.4;
var enemy = isRed ? new RedEnemy() : new Enemy();
enemy.x = Math.random() * 2048; // Random horizontal position
enemy.y = 0; // Start at the top
enemies.push(enemy);
game.addChild(enemy);
}, 250);
// Handle touch move to control player
game.on('move', function (obj) {
var pos = obj.event.getLocalPosition(game);
player.move(pos.x, pos.y);
});
// Main game loop
LK.on('tick', function () {
// Move enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].move();
// Remove enemies that go off screen or go too far to the left or right
if (enemies[i].y > 2732 || enemies[i].x < 0 || enemies[i].x > 2048) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Move lasers
for (var j = lasers.length - 1; j >= 0; j--) {
lasers[j].move();
// Remove lasers that go off screen
if (lasers[j].y < 0) {
lasers[j].destroy();
lasers.splice(j, 1);
}
}
// Check for collisions between lasers and enemies
for (var k = lasers.length - 1; k >= 0; k--) {
for (var l = enemies.length - 1; l >= 0; l--) {
if (lasers[k].intersects(enemies[l])) {
// Destroy both laser and enemy on collision
lasers[k].destroy();
lasers.splice(k, 1);
enemies[l].destroy();
enemies.splice(l, 1);
score += enemies[l] instanceof RedEnemy ? 1 : 3;
scoreTxt.setText(score);
break; // Exit the inner loop after a collision
}
}
}
// Check for collisions between player and enemies, and reset the game if a collision occurs
for (var m = enemies.length - 1; m >= 0; m--) {
if (player.intersects(enemies[m])) {
LK.showGameOver('Your score: ' + LK.getScore());
}
}
});
green cartoon laser. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
red cartoon laser. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
pink cartoon laser. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
sci-fi cartoon spaceship. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.