/**** * Classes ****/ // Define the 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.y += self.speed; if (self.y < -self.height) { self.destroy(); } }; }); // Define the 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 = 3; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -self.height; } }; }); //<Assets used in the game will automatically appear here> // Define the Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Player update logic }; self.move = function (x, y) { self.x = x; self.y = y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 200; // Initialize enemies var enemies = []; for (var i = 0; i < 5; i++) { var enemy = game.addChild(new Enemy()); enemy.x = Math.random() * 2048; enemy.y = Math.random() * -2732; enemies.push(enemy); } // Initialize bullets var bullets = []; // Handle player movement game.move = function (x, y, obj) { player.move(x, y); }; // Handle shooting game.down = function (x, y, obj) { var bullet = game.addChild(new Bullet()); bullet.x = player.x; bullet.y = player.y; bullets.push(bullet); }; // Update game state game.update = function () { // Update player player.update(); // Update enemies for (var i = 0; i < enemies.length; i++) { enemies[i].update(); } // Update bullets for (var j = 0; j < bullets.length; j++) { bullets[j].update(); } // Check for collisions for (var k = 0; k < bullets.length; k++) { for (var l = 0; l < enemies.length; l++) { if (bullets[k].intersects(enemies[l])) { bullets[k].destroy(); enemies[l].destroy(); bullets.splice(k, 1); enemies.splice(l, 1); // Spawn a new enemy at a random position at the top var newEnemy = game.addChild(new Enemy()); newEnemy.x = Math.random() * 2048; newEnemy.y = Math.random() * -500; // spawn slightly above the screen enemies.push(newEnemy); break; } } } };
===================================================================
--- original.js
+++ change.js
@@ -1,8 +1,7 @@
/****
* Classes
****/
-// Gun uses bullet asset for now, but you can add a new gun asset if available
// Define the Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
@@ -32,40 +31,22 @@
}
};
});
//<Assets used in the game will automatically appear here>
-// Define the Gun class
-var Gun = Container.expand(function () {
- var self = Container.call(this);
- // Use a simple box as gun for now, you can replace with an image asset if available
- var gunGraphics = self.attachAsset('bullet', {
- anchorX: 0.5,
- anchorY: 0.8,
- scaleX: 0.5,
- scaleY: 1.5
- });
- // Offset gun so it appears above the player
- self.x = 0;
- self.y = -60;
- return self;
-});
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
- // Attach gun to player
- self.gun = self.addChild(new Gun());
self.update = function () {
// Player update logic
};
self.move = function (x, y) {
self.x = x;
self.y = y;
- // Gun is a child, so it moves with player automatically, but you could update its position here if needed
};
});
/****
@@ -122,8 +103,13 @@
bullets[k].destroy();
enemies[l].destroy();
bullets.splice(k, 1);
enemies.splice(l, 1);
+ // Spawn a new enemy at a random position at the top
+ var newEnemy = game.addChild(new Enemy());
+ newEnemy.x = Math.random() * 2048;
+ newEnemy.y = Math.random() * -500; // spawn slightly above the screen
+ enemies.push(newEnemy);
break;
}
}
}
bulet. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
police. In-Game asset. 2d. High contrast. No shadows
criminal. In-Game asset. 2d. High contrast. No shadows
Road. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat