Code edit (10 edits merged)
Please save this source code
Code edit (8 edits merged)
Please save this source code
User prompt
Explosions must die on their own after 2 seconds
User prompt
When an enemy dies from a collision with the hero's fire or ship, an explosion appears in its place for 2 seconds.
Code edit (3 edits merged)
Please save this source code
User prompt
The player's score must increase by 1 point every 5 seconds of play.
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ var Explosion = Container.expand(function () { var self = Container.call(this); var explosionGraphics = self.attachAsset('explosion', { anchorX: 0.5, anchorY: 0.5 }); self.tick = function () { if (LK.ticks - self.creationTick >= 20) { // 2 seconds at 60FPS self.destroy(); } }; self.creationTick = LK.ticks; }); var Star = Container.expand(function () { var self = Container.call(this); var starGraphics = self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5 }); self.speed = Math.random() * 2 + 1; self.alpha = self.speed / 3; self.move = function () { self.y += self.speed; if (self.y > 2732 + self.height / 2) { self.y = -self.height / 2; } }; }); var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 20; self.move = function (x, y) { var dx = x - self.x; var dy = y - self.y; var angle = Math.atan2(dy, dx); self.x += Math.cos(angle) * self.speed; self.y += Math.sin(angle) * self.speed; }; self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - self.height / 2; game.addChild(bullet); return bullet; }; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.direction = Math.random() > 0.5 ? 1 : -1; self.move = function () { var multiplier = getSpeedMultiplier(score); self.y += self.speed * multiplier; if (LK.ticks % 60 == 0) { self.direction = Math.random() > 0.5 ? 1 : -1; self.speed = Math.random() * 3 + 2; } self.x += self.speed * self.direction; if (self.x < 0) { self.x = 0; } if (self.x > 2048) { self.x = 2048; } if (self.y > 2732 + self.height / 2) { self.destroy(); } }; self.shoot = function () { var bullet = new EnemyBullet(); bullet.x = self.x; bullet.y = self.y + self.height / 2; game.addChild(bullet); return bullet; }; }); var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('heroBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.move = function () { self.y += self.speed; if (self.y < -self.height / 2) { self.destroy(); } }; }); var EnemyBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('enemyBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.move = function () { var multiplier = getSpeedMultiplier(score); self.y += self.speed * multiplier; if (self.y > 2732 + self.height / 2) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ function getSpeedMultiplier(score) { return 1 + score / 100; // As an example, increase speed by 1% for every 100 points scored } var stars = []; for (var i = 0; i < 40; i++) { var star = new Star(); star.x = Math.random() * 2048; star.y = Math.random() * 2732; stars.push(star); game.addChild(star); } var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 100; var enemies = []; var heroBullets = []; var explosions = []; var enemyBullets = []; var score = 0; var scoreTxt = new Text2(score.toString(), { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var targetPos = { x: hero.x, y: hero.y }; game.on('down', function (obj) { targetPos = obj.event.getLocalPosition(game); }); game.on('move', function (obj) { targetPos = obj.event.getLocalPosition(game); }); LK.on('tick', function () { for (var i = 0; i < stars.length; i++) { stars[i].move(); } hero.move(targetPos.x, targetPos.y); // Move hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].move(); if (heroBullets[i].y < 0) { heroBullets[i].destroy(); heroBullets.splice(i, 1); } } // Move enemies bullets for (var i = enemyBullets.length - 1; i >= 0; i--) { enemyBullets[i].move(); if (enemyBullets[i].y < 0) { enemyBullets[i].destroy(); enemyBullets.splice(i, 1); } } // Move enemies and make them shoot for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].move(); var fireRate = 30; if (LK.ticks % fireRate == 0) { enemyBullets.push(enemies[j].shoot()); } if (enemies[j].y > 2732) { var explosion = new Explosion(); explosion.x = enemies[j].x; explosion.y = enemies[j].y; game.addChild(explosion); enemies[j].destroy(); enemies.splice(j, 1); } } // Check for collisions for (var k = heroBullets.length - 1; k >= 0; k--) { for (var l = enemies.length - 1; l >= 0; l--) { if (heroBullets[k].intersects(enemies[l])) { var explosion = new Explosion(); explosion.x = enemies[l].x; explosion.y = enemies[l].y; game.addChild(explosion); heroBullets[k].destroy(); heroBullets.splice(k, 1); enemies[l].destroy(); enemies.splice(l, 1); score += 10; scoreTxt.setText(score.toString()); break; } } } // Check if hero is hit by enemy bullet or enemy for (var m = enemyBullets.length - 1; m >= 0; m--) { if (hero.intersects(enemyBullets[m])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } for (var n = enemies.length - 1; n >= 0; n--) { if (hero.intersects(enemies[n])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn enemies if (LK.ticks % 120 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * (2048 - enemy.width) + enemy.width / 2; enemy.y = -enemy.height / 2; enemy.speed = Math.random() * 3 + 2; // Set random speed for each enemy enemies.push(enemy); game.addChild(enemy); } // Update explosions for (var i = explosions.length - 1; i >= 0; i--) { explosions[i].tick(); if (explosions[i].lifetime <= 0) { explosions.splice(i, 1); } } // Hero shoot if (LK.ticks % 80 == 0) { heroBullets.push(hero.shoot()); } }); var scoreIncrementTimer = LK.setInterval(function () { score += 1; scoreTxt.setText(score.toString()); }, 1000);
===================================================================
--- original.js
+++ change.js
@@ -7,9 +7,9 @@
anchorX: 0.5,
anchorY: 0.5
});
self.tick = function () {
- if (LK.ticks - self.creationTick >= 120) {
+ if (LK.ticks - self.creationTick >= 20) {
// 2 seconds at 60FPS
self.destroy();
}
};