User prompt
When updating score textg also call LK.setScore
User prompt
Make ship shoot twice as fast
Code edit (1 edits merged)
Please save this source code
User prompt
Make ship shoot twice as fast
User prompt
Add powerups to the game that makes the ship shoot two bullets side by side for 5 seconds.
User prompt
Make ship shoot twice as fast
User prompt
Make ship shoot twice as fast
User prompt
Fix Bug: 'ReferenceError: Can't find variable: HeroBullet' in this line: 'var bullet = new HeroBullet();' Line Number: 84
===================================================================
--- original.js
+++ change.js
@@ -1,13 +1,7 @@
-var PowerUp = Container.expand(function () {
- var self = Container.call(this);
- var powerUpGraphics = LK.getAsset('powerUp', 'Power Up Graphics', 0.5, 0.5);
- self.addChild(powerUpGraphics);
- self.speed = 2;
- self.move = function () {
- self.y += self.speed;
- };
-});
+/****
+* Classes
+****/
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = LK.getAsset('heroBullet', 'Hero Bullet Graphics', 0.5, 0.5);
self.addChild(bulletGraphics);
@@ -41,10 +35,14 @@
if (self.y < self.targetY) {
self.y += self.speed;
}
self.x += Math.sin(LK.ticks / 30) * 5;
- if (self.x < 0) self.x = 0;
- if (self.x > 2048 - enemyGraphics.width) self.x = 2048 - enemyGraphics.width;
+ if (self.x < 0) {
+ self.x = 0;
+ }
+ if (self.x > 2048 - enemyGraphics.width) {
+ self.x = 2048 - enemyGraphics.width;
+ }
};
});
var StarField = Container.expand(function () {
var self = Container.call(this);
@@ -61,189 +59,165 @@
self.alpha = self.speed / 3;
}
};
});
-var Game = Container.expand(function () {
- var self = Container.call(this);
- var starField = [];
- for (var i = 0; i < 100; i++) {
- var star = new StarField();
- star.x = Math.random() * 2048;
- star.y = Math.random() * 2732;
- starField.push(star);
- self.addChild(star);
+
+/****
+* Initialize Game
+****/
+var game = new LK.Game({
+ backgroundColor: 0x000000
+});
+
+/****
+* Game Code
+****/
+var starField = [];
+for (var i = 0; i < 100; i++) {
+ var star = new StarField();
+ star.x = Math.random() * 2048;
+ star.y = Math.random() * 2732;
+ starField.push(star);
+ game.addChild(star);
+}
+var hero = game.addChild(new Hero());
+hero.x = 1024;
+hero.y = 2400;
+var enemies = [];
+var heroBullets = [];
+var enemyBullets = [];
+var score = 0;
+var currentWave = 0;
+var allEnemiesKilled = true;
+var scoreText = new Text2('0', {
+ size: 100,
+ font: "'GillSans-Bold',Impact,'Arial Black',Tahoma",
+ fill: "#ffffff"
+});
+scoreText.anchor.set(0.5, 0);
+LK.gui.topCenter.addChild(scoreText);
+function spawnWave() {
+ for (var i = 0; i < currentWave + 3; i++) {
+ var enemy = new Enemy();
+ enemy.x = Math.random() * 2048;
+ enemy.y = -100 - i * 100;
+ enemies.push(enemy);
+ game.addChild(enemy);
}
- var hero = self.addChild(new Hero());
- hero.x = 1024;
- hero.y = 2400;
- var enemies = [];
- var heroBullets = [];
- var enemyBullets = [];
- var powerUps = [];
- var powerUpTimer = 0;
- var score = 0;
- var currentWave = 0;
- var allEnemiesKilled = true;
- var scoreText = new Text2('0', {
- size: 100,
- font: "'GillSans-Bold',Impact,'Arial Black',Tahoma",
- fill: "#ffffff"
+ allEnemiesKilled = false;
+}
+function fireHeroBullet() {
+ var bullet = new HeroBullet();
+ bullet.x = hero.x;
+ bullet.y = hero.y - 50;
+ heroBullets.push(bullet);
+ game.addChild(bullet);
+}
+function fireEnemyBullet(enemy) {
+ var bullet = new EnemyBullet();
+ bullet.x = enemy.x;
+ bullet.y = enemy.y + 50;
+ var angle = Math.atan2(hero.y - bullet.y, hero.x - bullet.x);
+ bullet.speedX = bullet.speed * Math.cos(angle);
+ bullet.speedY = bullet.speed * Math.sin(angle);
+ enemyBullets.push(bullet);
+ game.addChild(bullet);
+}
+function updateGameObjects() {
+ enemies.forEach(function (enemy, index) {
+ enemy.move();
+ if (enemy.y > 2732) {
+ enemy.destroy();
+ enemies.splice(index, 1);
+ }
});
- scoreText.anchor.set(0.5, 0);
- LK.gui.topCenter.addChild(scoreText);
- function spawnWave() {
- for (var i = 0; i < currentWave + 3; i++) {
- var enemy = new Enemy();
- enemy.x = Math.random() * 2048;
- enemy.y = -100 - i * 100;
- enemies.push(enemy);
- self.addChild(enemy);
+ heroBullets.forEach(function (bullet, index) {
+ bullet.move();
+ if (bullet.y < 0) {
+ bullet.destroy();
+ heroBullets.splice(index, 1);
}
- allEnemiesKilled = false;
- }
- function fireHeroBullet() {
- if (powerUpTimer > 0) {
- var bullet1 = new HeroBullet();
- bullet1.x = hero.x - 20;
- bullet1.y = hero.y - 50;
- heroBullets.push(bullet1);
- self.addChild(bullet1);
- var bullet2 = new HeroBullet();
- bullet2.x = hero.x + 20;
- bullet2.y = hero.y - 50;
- heroBullets.push(bullet2);
- self.addChild(bullet2);
- } else {
- var bullet = new HeroBullet();
- bullet.x = hero.x;
- bullet.y = hero.y - 50;
- heroBullets.push(bullet);
- self.addChild(bullet);
+ });
+ enemyBullets.forEach(function (bullet, index) {
+ bullet.move();
+ if (bullet.y > 2732) {
+ bullet.destroy();
+ enemyBullets.splice(index, 1);
}
- }
- function fireEnemyBullet(enemy) {
- var bullet = new EnemyBullet();
- bullet.x = enemy.x;
- bullet.y = enemy.y + 50;
- var angle = Math.atan2(hero.y - bullet.y, hero.x - bullet.x);
- bullet.speedX = bullet.speed * Math.cos(angle);
- bullet.speedY = bullet.speed * Math.sin(angle);
- enemyBullets.push(bullet);
- self.addChild(bullet);
- }
- function updateGameObjects() {
- enemies.forEach(function (enemy, index) {
- enemy.move();
- if (enemy.y > 2732) {
+ });
+}
+function checkCollisions() {
+ // Check collisions between hero bullets and enemies
+ heroBullets.forEach(function (heroBullet, hIndex) {
+ enemies.forEach(function (enemy, eIndex) {
+ if (heroBullet.intersects(enemy)) {
+ heroBullet.destroy();
+ heroBullets.splice(hIndex, 1);
enemy.destroy();
- enemies.splice(index, 1);
- }
- });
- heroBullets.forEach(function (bullet, index) {
- bullet.move();
- if (bullet.y < 0) {
- bullet.destroy();
- heroBullets.splice(index, 1);
- }
- });
- enemyBullets.forEach(function (bullet, index) {
- bullet.move();
- if (bullet.y > 2732) {
- bullet.destroy();
- enemyBullets.splice(index, 1);
- }
- });
- }
- function checkCollisions() {
- heroBullets.forEach(function (heroBullet, hIndex) {
- enemies.forEach(function (enemy, eIndex) {
- if (heroBullet.intersects(enemy)) {
- heroBullet.destroy();
- heroBullets.splice(hIndex, 1);
- enemy.destroy();
- enemies.splice(eIndex, 1);
- score += 100;
- scoreText.setText(score);
- if (enemies.length === 0) {
- allEnemiesKilled = true;
- }
+ enemies.splice(eIndex, 1);
+ score += 100;
+ scoreText.setText(score);
+ if (enemies.length === 0) {
+ allEnemiesKilled = true;
}
- });
- });
- enemyBullets.forEach(function (enemyBullet, index) {
- if (enemyBullet.intersects(hero)) {
- enemyBullet.destroy();
- enemyBullets.splice(index, 1);
- LK.showGameOver();
}
});
- }
- var targetPosition = {
- x: hero.x,
- y: hero.y
- };
- stage.on('down', function (obj) {
- targetPosition = obj.event.getLocalPosition(self);
});
- stage.on('move', function (obj) {
- targetPosition = obj.event.getLocalPosition(self);
- });
- function moveHeroTowardsTarget() {
- var dx = targetPosition.x - hero.x;
- var dy = targetPosition.y - hero.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- var maxSpeed = 15;
- if (distance > maxSpeed) {
- var angle = Math.atan2(dy, dx);
- hero.x += maxSpeed * Math.cos(angle);
- hero.y += maxSpeed * Math.sin(angle);
- } else {
- hero.x = targetPosition.x;
- hero.y = targetPosition.y;
+
+ // Check collisions between enemy bullets and hero
+ enemyBullets.forEach(function (enemyBullet, index) {
+ if (enemyBullet.intersects(hero)) {
+ enemyBullet.destroy();
+ enemyBullets.splice(index, 1);
+ LK.showGameOver();
+ // Handle hero hit by enemy bullet
}
+ });
+}
+var targetPosition = {
+ x: hero.x,
+ y: hero.y
+};
+game.on('down', function (obj) {
+ targetPosition = obj.event.getLocalPosition(game);
+});
+game.on('move', function (obj) {
+ targetPosition = obj.event.getLocalPosition(game);
+});
+function moveHeroTowardsTarget() {
+ var dx = targetPosition.x - hero.x;
+ var dy = targetPosition.y - hero.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ var maxSpeed = 15;
+ if (distance > maxSpeed) {
+ var angle = Math.atan2(dy, dx);
+ hero.x += maxSpeed * Math.cos(angle);
+ hero.y += maxSpeed * Math.sin(angle);
+ } else {
+ hero.x = targetPosition.x;
+ hero.y = targetPosition.y;
}
- function handleTick() {
- moveHeroTowardsTarget();
- updateGameObjects();
- checkCollisions();
- starField.forEach(function (star) {
- star.move();
- });
- if (allEnemiesKilled) {
- currentWave++;
- spawnWave();
+}
+function handleTick() {
+ moveHeroTowardsTarget();
+ updateGameObjects();
+ checkCollisions();
+ starField.forEach(function (star) {
+ star.move();
+ });
+ if (allEnemiesKilled) {
+ currentWave++;
+ spawnWave();
+ }
+ if (LK.ticks % 15 === 0) {
+ fireHeroBullet();
+ }
+ enemies.forEach(function (enemy, index) {
+ if (LK.ticks % 90 === 0) {
+ fireEnemyBullet(enemy);
}
- if (LK.ticks % 600 === 0) {
- var powerUp = new PowerUp();
- powerUp.x = Math.random() * 2048;
- powerUp.y = -100;
- powerUps.push(powerUp);
- self.addChild(powerUp);
+ if (LK.ticks % 180 === 0 && index % 2 === 0) {
+ fireEnemyBullet(enemy);
}
- if (powerUpTimer > 0) {
- powerUpTimer--;
- }
- if (LK.ticks % 30 === 0) {
- fireHeroBullet();
- }
- powerUps.forEach(function (powerUp, index) {
- powerUp.move();
- if (powerUp.y > 2732) {
- powerUp.destroy();
- powerUps.splice(index, 1);
- } else if (powerUp.intersects(hero)) {
- powerUp.destroy();
- powerUps.splice(index, 1);
- powerUpTimer = 300;
- }
- });
- enemies.forEach(function (enemy, index) {
- if (LK.ticks % 90 === 0) {
- fireEnemyBullet(enemy);
- }
- if (LK.ticks % 180 === 0 && index % 2 === 0) {
- fireEnemyBullet(enemy);
- }
- });
- }
- LK.on('tick', handleTick);
-});
+ });
+}
+LK.on('tick', handleTick);
\ No newline at end of file
Single space torpedo flying upwards Game Texture. In-Game asset. 2d. Pixelart. White background. Blank background. Low detail. High contrast.
Single enemy slime bullet flying downwards. Game Texture. In-Game asset. 2d. Pixelart. White background. Blank background. Low detail. High contrast.
single alien enemy spaceship facing down, looking like space alien adopted to living in space. Game Texture. In-Game asset. 2d. Pixelart. White background. Blank background. Low detail. High contrast.
Hero spaceship facing upwards, with a single cannon in the center. Game Texture. In-Game asset. 2d. Pixelart. White background. Blank background. Low detail. High contrast.