User prompt
Add player health bar. When an enemy colides with the player, take 20% of the health bar. Game over at 0 health
User prompt
Make enemies move towards the player. If they touch a bullet, remove both the bullet and the enemy
User prompt
Make enemies appear at random points along the edge
User prompt
still not fixed.
User prompt
Its not fixed, they mostly come from the corners
User prompt
Enemies should only spawn at the edge of the screen, they should die after being shot once.
User prompt
Make enemies appear and approach the player. Remove them if they get hit or touch the player
Initial prompt
defense
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// 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.update = function () {
self.x += self.speed * Math.cos(self.rotation);
self.y += self.speed * Math.sin(self.rotation);
};
});
// 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 = 2;
self.update = function () {
var dx = player.x - self.x;
var dy = player.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += self.speed * Math.cos(angle);
self.y += self.speed * Math.sin(angle);
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.rotation = 0;
self.bullets = [];
self.health = 100; // Initialize player health
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.rotation = self.rotation;
self.bullets.push(bullet);
game.addChild(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
// Initialize health bar
var healthBar = LK.getAsset('healthBar', {
anchorX: 0.0,
anchorY: 0.0,
x: 0,
y: 0
});
game.addChild(healthBar);
// Handle mouse move to rotate player
game.move = function (x, y, obj) {
var localPos = game.toLocal(obj.global);
var dx = localPos.x - player.x;
var dy = localPos.y - player.y;
player.rotation = Math.atan2(dy, dx);
};
// Handle mouse down to shoot
game.down = function (x, y, obj) {
player.shoot();
};
// Update game state
// Initialize enemies array
var enemies = [];
game.update = function () {
// Create enemies at random points along the edge
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
var side = Math.floor(Math.random() * 4);
switch (side) {
case 0:
// top
enemy.x = Math.random() * 2048;
enemy.y = 0;
break;
case 1:
// right
enemy.x = 2048;
enemy.y = Math.random() * 2732;
break;
case 2:
// bottom
enemy.x = Math.random() * 2048;
enemy.y = 2732;
break;
case 3:
// left
enemy.x = 0;
enemy.y = Math.random() * 2732;
break;
}
enemies.push(enemy);
game.addChild(enemy);
}
for (var i = player.bullets.length - 1; i >= 0; i--) {
player.bullets[i].update();
if (player.bullets[i].x < 0 || player.bullets[i].x > 2048 || player.bullets[i].y < 0 || player.bullets[i].y > 2732) {
player.bullets[i].destroy();
player.bullets.splice(i, 1);
} else {
for (var j = enemies.length - 1; j >= 0; j--) {
if (player.bullets[i].intersects(enemies[j])) {
player.bullets[i].destroy();
player.bullets.splice(i, 1);
enemies[j].destroy();
enemies.splice(j, 1);
break;
} else if (player.intersects(enemies[j])) {
player.health -= 20; // Decrease player's health
healthBar.width = player.health / 100 * 2048; // Update health bar
if (player.health <= 0) {
LK.showGameOver();
}
}
}
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -39,8 +39,9 @@
anchorY: 0.5
});
self.rotation = 0;
self.bullets = [];
+ self.health = 100; // Initialize player health
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
@@ -64,8 +65,16 @@
var player = new Player();
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
+// Initialize health bar
+var healthBar = LK.getAsset('healthBar', {
+ anchorX: 0.0,
+ anchorY: 0.0,
+ x: 0,
+ y: 0
+});
+game.addChild(healthBar);
// Handle mouse move to rotate player
game.move = function (x, y, obj) {
var localPos = game.toLocal(obj.global);
var dx = localPos.x - player.x;
@@ -121,8 +130,14 @@
player.bullets.splice(i, 1);
enemies[j].destroy();
enemies.splice(j, 1);
break;
+ } else if (player.intersects(enemies[j])) {
+ player.health -= 20; // Decrease player's health
+ healthBar.width = player.health / 100 * 2048; // Update health bar
+ if (player.health <= 0) {
+ LK.showGameOver();
+ }
}
}
}
}
Fireball with angry face. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. 8 bit
Grinning creepy ball shaped clown face. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. 8 bit
Burning ball shaped creepy grinning devil. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. 8 bit
Creepy Jester clown with a fat circular belly, grinning and carying a trident. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. 8 bit
Dark night sky. Dim stars. DMT psychedelic. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. 8 bit