User prompt
add stars to the background
User prompt
re add color swapping'
User prompt
add a larger delay
User prompt
reset the player bullets
User prompt
they aren't destructible
User prompt
i meant the enemy bullets
User prompt
make the bullets destructible and homing
User prompt
make the enemies and bullets hurt the player
User prompt
add a delay for shooting
User prompt
fix the swapping ability
User prompt
make red only able to be hit by red bullets
User prompt
fix it
User prompt
move shoot button to left click
User prompt
change the change color button to rmb
User prompt
make blue enemies only able to be hit by blue bullets
User prompt
make the enemies only able to be damaged by bullets of their color
User prompt
now make the bullet shoot button left click
User prompt
could you swap the swap button to right click
User prompt
Please fix the bug: 'Uncaught ReferenceError: BlueBullet is not defined' in or related to this line: 'bullet = new BlueBullet();' Line Number: 79
User prompt
add the ability to swap to red bullets
User prompt
add blue enemies that are the same as the red ones
Initial prompt
Space Invaders
/****
* Classes
****/
// BlueBullet class
var BlueBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('blueBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
// BlueEnemy class
var BlueEnemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('blueEnemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + enemyGraphics.height) {
self.destroy();
enemies.splice(enemies.indexOf(self), 1);
}
};
});
// DestructibleBullet class
var DestructibleBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
// Add homing functionality
if (enemies.length > 0) {
var closestEnemy = enemies[0];
var closestDistance = Math.hypot(closestEnemy.x - self.x, closestEnemy.y - self.y);
for (var i = 1; i < enemies.length; i++) {
var distance = Math.hypot(enemies[i].x - self.x, enemies[i].y - self.y);
if (distance < closestDistance) {
closestEnemy = enemies[i];
closestDistance = distance;
}
}
var dx = closestEnemy.x - self.x;
var dy = closestEnemy.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
}
if (self.y < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
// 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 = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + enemyGraphics.height) {
self.destroy();
enemies.splice(enemies.indexOf(self), 1);
}
};
});
// EnemyBullet class
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.update = function () {
self.y += self.speed;
if (self.y > 2732 + bulletGraphics.height) {
self.destroy();
enemyBullets.splice(enemyBullets.indexOf(self), 1);
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Hero update logic
};
self.bulletColor = 'blue';
self.shoot = function () {
var bullet = new DestructibleBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
self.swapColor = function () {
if (self.bulletColor === 'blue') {
self.bulletColor = 'red';
} else {
self.bulletColor = 'blue';
}
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
// RedBullet class
var RedBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('redBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemies = [];
var enemyBullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Spawn enemies
function spawnEnemy() {
var enemy;
if (Math.random() > 0.5) {
enemy = new Enemy();
} else {
enemy = new BlueEnemy();
}
enemy.x = Math.random() * 2048;
enemy.y = -enemy.height;
game.addChild(enemy);
enemies.push(enemy);
}
// Handle game updates
game.update = function () {
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].update();
}
// Check for collisions
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].destroy();
heroBullets[i].destroy();
enemies.splice(j, 1);
heroBullets.splice(i, 1);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Check for collisions between hero and enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
if (hero.intersects(enemyBullets[i])) {
hero.destroy();
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
LK.showGameOver();
break;
}
}
// Fire enemy bullets
if (LK.ticks % 60 == 0) {
for (var i = 0; i < enemies.length; i++) {
var bullet = new EnemyBullet();
bullet.x = enemies[i].x;
bullet.y = enemies[i].y + enemies[i].height / 2;
game.addChild(bullet);
enemyBullets.push(bullet);
}
}
// Spawn new enemies
if (LK.ticks % 120 == 0) {
spawnEnemy();
}
// Decrease shootDelay
if (shootDelay > 0) {
shootDelay--;
}
};
// Handle touch events
game.down = function (x, y, obj) {
hero.swapColor();
};
var shootDelay = 0;
game.move = function (x, y, obj) {
hero.x = x;
if (shootDelay <= 0) {
hero.shoot();
shootDelay = 10; // Delay for 10 frames
}
};
game.up = function (x, y, obj) {
// No action needed on touch up
}; ===================================================================
--- original.js
+++ change.js
@@ -32,8 +32,41 @@
enemies.splice(enemies.indexOf(self), 1);
}
};
});
+// DestructibleBullet class
+var DestructibleBullet = Container.expand(function () {
+ var self = Container.call(this);
+ var bulletGraphics = self.attachAsset('heroBullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -15;
+ self.update = function () {
+ self.y += self.speed;
+ // Add homing functionality
+ if (enemies.length > 0) {
+ var closestEnemy = enemies[0];
+ var closestDistance = Math.hypot(closestEnemy.x - self.x, closestEnemy.y - self.y);
+ for (var i = 1; i < enemies.length; i++) {
+ var distance = Math.hypot(enemies[i].x - self.x, enemies[i].y - self.y);
+ if (distance < closestDistance) {
+ closestEnemy = enemies[i];
+ closestDistance = distance;
+ }
+ }
+ var dx = closestEnemy.x - self.x;
+ var dy = closestEnemy.y - self.y;
+ var angle = Math.atan2(dy, dx);
+ self.x += Math.cos(angle) * self.speed;
+ self.y += Math.sin(angle) * self.speed;
+ }
+ if (self.y < -bulletGraphics.height) {
+ self.destroy();
+ heroBullets.splice(heroBullets.indexOf(self), 1);
+ }
+ };
+});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
@@ -78,14 +111,9 @@
// Hero update logic
};
self.bulletColor = 'blue';
self.shoot = function () {
- var bullet;
- if (self.bulletColor === 'blue') {
- bullet = new BlueBullet();
- } else {
- bullet = new RedBullet();
- }
+ var bullet = new DestructibleBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
@@ -188,25 +216,15 @@
// Check for collisions
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
- if (enemies[j] instanceof BlueEnemy && heroBullets[i] instanceof BlueBullet) {
- enemies[j].destroy();
- heroBullets[i].destroy();
- enemies.splice(j, 1);
- heroBullets.splice(i, 1);
- score++;
- scoreTxt.setText(score);
- break;
- } else if (!(enemies[j] instanceof BlueEnemy) && heroBullets[i] instanceof RedBullet) {
- enemies[j].destroy();
- heroBullets[i].destroy();
- enemies.splice(j, 1);
- heroBullets.splice(i, 1);
- score++;
- scoreTxt.setText(score);
- break;
- }
+ enemies[j].destroy();
+ heroBullets[i].destroy();
+ enemies.splice(j, 1);
+ heroBullets.splice(i, 1);
+ score++;
+ scoreTxt.setText(score);
+ break;
}
}
}
// Check for collisions between hero and enemy bullets
blue tentacle alien. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
red tentacle alien. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
purple laser. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
RED LASER. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
blue LASER. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
spaceship, facing up. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.