/****
* 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();
}
}
};
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.