User prompt
make that there are more ennemy pig
Code edit (1 edits merged)
Please save this source code
User prompt
make that the ennemy pigs bigger
User prompt
make that you clic on the spacebar, a bullet goes from the pig to the nearest ennemy pig
User prompt
Make the pig bigger
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'intersects')' in or related to this line: 'if (enemyPigs[i].intersects(playerPig)) {' Line Number: 136
User prompt
make that if the pig is touched by a ennemy pig he will die
User prompt
make that if the bullet touch a enemyPig he will die
User prompt
Please fix the bug: 'Uncaught TypeError: window.addEventListener is not a function' in or related to this line: 'window.addEventListener('keydown', function (event) {' Line Number: 98
User prompt
Make that if you clic spacebar the pig choots a bullet
User prompt
make the pig can move every where in the map
Initial prompt
pig wars
/**** * Classes ****/ // Create a class for the bullet var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Bullet's behavior goes here self.y -= 5; }; self.setDirection = function (target) { var dx = target.x - self.x; var dy = target.y - self.y; var angle = Math.atan2(dy, dx); self.vx = Math.cos(angle); self.vy = Math.sin(angle); }; }); // Create a class for the enemy pigs var EnemyPig = Container.expand(function () { var self = Container.call(this); var pigGraphics = self.attachAsset('enemyPig', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); self.update = function () { // Enemy pig's behavior goes here }; }); // The game engine will automatically load the assets // Create a class for the player's pig var PlayerPig = Container.expand(function () { var self = Container.call(this); var pigGraphics = self.attachAsset('playerPig', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); self.update = function () { // Player pig's behavior goes here // Move the player's pig based on the mouse/touch position if (game.mouse.isDown) { self.x = game.mouse.x; self.y = game.mouse.y; } }; self.getNearestEnemy = function () { var nearestEnemy = null; var nearestDistance = Infinity; for (var i = 0; i < enemyPigs.length; i++) { var dx = self.x - enemyPigs[i].x; var dy = self.y - enemyPigs[i].y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < nearestDistance) { nearestEnemy = enemyPigs[i]; nearestDistance = distance; } } return nearestEnemy; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Initialize the mouse state game.mouse = { isDown: false, x: 0, y: 0 }; game.move = function (x, y, obj) { // Update the mouse position game.mouse.x = x; game.mouse.y = y; }; game.up = function (x, y, obj) { // Reset the mouse down state game.mouse.isDown = false; }; game.down = function (x, y, obj) { // Set the mouse down state game.mouse.isDown = true; // Update the mouse position game.mouse.x = x; game.mouse.y = y; }; var playerPig = game.addChild(new PlayerPig()); playerPig.x = 2048 / 2; // Center the player's pig horizontally playerPig.y = 2732 - playerPig.height / 2; // Position the player's pig at the bottom of the screen // Initialize an array to hold the enemy pigs var enemyPigs = []; // Spawn two enemy pigs every 2 seconds LK.setInterval(function () { for (var i = 0; i < 2; i++) { var enemyPig = new EnemyPig(); enemyPig.x = Math.random() * 2048; // Random horizontal position enemyPig.y = -enemyPig.height / 2; // Start just off the top of the screen enemyPigs.push(enemyPig); game.addChild(enemyPig); } }, 2000); // Update the game state every frame LK.on('keydown', function (event) { if (event.code === 'right clic') { var bullet = new Bullet(); bullet.x = playerPig.x; bullet.y = playerPig.y; var nearestEnemy = playerPig.getNearestEnemy(); if (nearestEnemy) { bullet.setDirection(nearestEnemy); } game.addChild(bullet); } }); game.update = function () { // Update the bullets for (var i = game.children.length - 1; i >= 0; i--) { if (game.children[i] instanceof Bullet) { if (game.children[i].y < -game.children[i].height / 2) { game.removeChild(game.children[i]); } else { // Check for collision with enemy pigs for (var j = 0; j < enemyPigs.length; j++) { if (game.children[i].intersects(enemyPigs[j])) { // Remove the bullet and the enemy pig game.removeChild(game.children[i]); game.removeChild(enemyPigs[j]); enemyPigs.splice(j, 1); break; } } } } } // Move the enemy pigs down the screen for (var i = 0; i < enemyPigs.length; i++) { enemyPigs[i].y += 5; // If an enemy pig has reached the bottom of the screen, remove it if (enemyPigs[i].y > 2732 + enemyPigs[i].height / 2) { game.removeChild(enemyPigs[i]); enemyPigs.splice(i, 1); i--; // Adjust the index to account for the removed pig } // If an enemy pig touches the player's pig, end the game if (enemyPigs[i] && enemyPigs[i].intersects(playerPig)) { LK.showGameOver(); } } };
===================================================================
--- original.js
+++ change.js
@@ -103,19 +103,21 @@
playerPig.x = 2048 / 2; // Center the player's pig horizontally
playerPig.y = 2732 - playerPig.height / 2; // Position the player's pig at the bottom of the screen
// Initialize an array to hold the enemy pigs
var enemyPigs = [];
-// Spawn an enemy pig every 2 seconds
+// Spawn two enemy pigs every 2 seconds
LK.setInterval(function () {
- var enemyPig = new EnemyPig();
- enemyPig.x = Math.random() * 2048; // Random horizontal position
- enemyPig.y = -enemyPig.height / 2; // Start just off the top of the screen
- enemyPigs.push(enemyPig);
- game.addChild(enemyPig);
+ for (var i = 0; i < 2; i++) {
+ var enemyPig = new EnemyPig();
+ enemyPig.x = Math.random() * 2048; // Random horizontal position
+ enemyPig.y = -enemyPig.height / 2; // Start just off the top of the screen
+ enemyPigs.push(enemyPig);
+ game.addChild(enemyPig);
+ }
}, 2000);
// Update the game state every frame
LK.on('keydown', function (event) {
- if (event.code === 'w') {
+ if (event.code === 'right clic') {
var bullet = new Bullet();
bullet.x = playerPig.x;
bullet.y = playerPig.y;
var nearestEnemy = playerPig.getNearestEnemy();
A pig with a gun. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
monster pig horrorific. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a bullet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.