/**** * Classes ****/ // Define a class for bullets var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 50; self.update = function () { self.x += self.speed; if (self.x > 2048) { self.destroy(); } }; }); // Define a class for enemies 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.x -= self.speed; if (Math.random() < 0.1) { // 10% chance to jump self.y -= 100; // Jump up by 100 pixels } if (self.y < 2732 / 2) { // If enemy is above the center line self.y += 10; // Move back down faster due to increased gravity } if (self.x < -50) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Define a class for the player character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); var weaponGraphics = self.attachAsset('weapon', { anchorX: 0.5, anchorY: 0.5 }); weaponGraphics.x = 50; weaponGraphics.y = -15; self.speed = 5; self.update = function () {}; self.shoot = function () { var bullet = game.addChild(new Bullet()); bullets.push(bullet); bullet.x = self.x + weaponGraphics.x + 75; // Adjust the bullet's starting x position to be at the end of the weapon bullet.y = self.y + weaponGraphics.y; // Keep the bullet's starting y position the same as the weapon's bullet.speed = 20; // Set bullet speed }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ var bullets = []; var background = game.addChild(LK.getAsset('background', { anchorX: 0, anchorY: 0 })); background.x = 0; background.y = 0; // Initialize player var player = game.addChild(new Player()); player.x = 0 + player.width / 2; // Position player at the left edge player.y = 2732 / 2; // Keep player vertically centered // Initialize enemies var enemies = []; var enemySpawnInterval = 100; var enemySpawnCounter = 0; // Create a new Text2 object to display the score var scoreText = new Text2('0', { size: 100, fill: 0xFFFFFF }); // Add the score text to the game GUI at the top center of the screen LK.gui.top.addChild(scoreText); scoreText.x = 2048 / 2; scoreText.y = 0; // Handle game updates game.update = function () { player.update(); // Spawn enemies enemySpawnCounter++; if (enemySpawnCounter >= enemySpawnInterval) { var enemy = new Enemy(); enemy.x = 2048; enemy.y = 2732 / 2; enemies.push(enemy); game.addChild(enemy); // Randomize the spawn interval for the next enemy enemySpawnInterval = Math.floor(Math.random() * 150) + 50; enemySpawnCounter = 0; } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (player.intersects(enemies[j])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } else if (player.x > enemies[j].x && !enemies[j].passed) { enemies[j].passed = true; LK.setScore(LK.getScore() + 1); scoreText.setText(LK.getScore()); } // Check for bullet collision with enemy for (var k = bullets.length - 1; k >= 0; k--) { if (bullets[k].intersects(enemies[j])) { enemies[j].destroy(); bullets[k].destroy(); bullets.splice(k, 1); enemies.splice(j, 1); // Remove the enemy from the array break; } } } }; // Handle player jump game.down = function (x, y, obj) { player.shoot(); };
/****
* Classes
****/
// Define a class for bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 50;
self.update = function () {
self.x += self.speed;
if (self.x > 2048) {
self.destroy();
}
};
});
// Define a class for enemies
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.x -= self.speed;
if (Math.random() < 0.1) {
// 10% chance to jump
self.y -= 100; // Jump up by 100 pixels
}
if (self.y < 2732 / 2) {
// If enemy is above the center line
self.y += 10; // Move back down faster due to increased gravity
}
if (self.x < -50) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Define a class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
var weaponGraphics = self.attachAsset('weapon', {
anchorX: 0.5,
anchorY: 0.5
});
weaponGraphics.x = 50;
weaponGraphics.y = -15;
self.speed = 5;
self.update = function () {};
self.shoot = function () {
var bullet = game.addChild(new Bullet());
bullets.push(bullet);
bullet.x = self.x + weaponGraphics.x + 75; // Adjust the bullet's starting x position to be at the end of the weapon
bullet.y = self.y + weaponGraphics.y; // Keep the bullet's starting y position the same as the weapon's
bullet.speed = 20; // Set bullet speed
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var bullets = [];
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0
}));
background.x = 0;
background.y = 0;
// Initialize player
var player = game.addChild(new Player());
player.x = 0 + player.width / 2; // Position player at the left edge
player.y = 2732 / 2; // Keep player vertically centered
// Initialize enemies
var enemies = [];
var enemySpawnInterval = 100;
var enemySpawnCounter = 0;
// Create a new Text2 object to display the score
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
// Add the score text to the game GUI at the top center of the screen
LK.gui.top.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 0;
// Handle game updates
game.update = function () {
player.update();
// Spawn enemies
enemySpawnCounter++;
if (enemySpawnCounter >= enemySpawnInterval) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 / 2;
enemies.push(enemy);
game.addChild(enemy);
// Randomize the spawn interval for the next enemy
enemySpawnInterval = Math.floor(Math.random() * 150) + 50;
enemySpawnCounter = 0;
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (player.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else if (player.x > enemies[j].x && !enemies[j].passed) {
enemies[j].passed = true;
LK.setScore(LK.getScore() + 1);
scoreText.setText(LK.getScore());
}
// Check for bullet collision with enemy
for (var k = bullets.length - 1; k >= 0; k--) {
if (bullets[k].intersects(enemies[j])) {
enemies[j].destroy();
bullets[k].destroy();
bullets.splice(k, 1);
enemies.splice(j, 1); // Remove the enemy from the array
break;
}
}
}
};
// Handle player jump
game.down = function (x, y, obj) {
player.shoot();
};