/**** * Classes ****/ // Bullet class representing the bullets fired by the player 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; }; }); // Life class representing the player's lives var Life = Container.expand(function () { var self = Container.call(this); var lifeGraphics = self.attachAsset('Life', { anchorX: 0.5, anchorY: 0.5 }); }); // Object class representing the object that stays below the screen and moves to where the click is var Object = Container.expand(function () { var self = Container.call(this); var objectGraphics = self.attachAsset('object', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Add any object-specific update logic here }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Target class representing the targets to be hit var Target = Container.expand(function () { var self = Container.call(this); var targetGraphics = self.attachAsset('target', { anchorX: 0.5, anchorY: 0.5 }); // Initialize random speed and direction for the target self.speedX = (Math.random() - 0.5) * 10; self.speedY = (Math.random() - 0.5) * 10; self.update = function () { // Add any target-specific update logic here self.x += self.speedX; self.y += self.speedY; // Check if target is off-screen, then change direction if (self.x < 0 || self.x > 2048) { self.speedX *= -1; } if (self.y < 0 || self.y > 2732) { self.speedY *= -1; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var background = game.attachAsset('background', { anchorX: 0, anchorY: 0 }); // Initialize variables var targets = []; var bullets = []; var score = 0; var object = new Object(); object.y = 2732; game.addChild(object); // Initialize the player's lives var lives = []; for (var i = 0; i < 3; i++) { var life = new Life(); life.x = 100 + i * 100; life.y = 100; lives.push(life); game.addChild(life); } // Create score display var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF, font: "'Comic Sans MS', cursive, sans-serif" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to spawn a new target function spawnTarget() { var newTarget = new Target(); newTarget.x = Math.random() * 2048; newTarget.y = Math.random() * 1000 + 500; targets.push(newTarget); game.addChild(newTarget); } // Function to handle shooting function shoot(x, y) { var newBullet = new Bullet(); newBullet.x = x; newBullet.y = y; bullets.push(newBullet); game.addChild(newBullet); } // Handle game updates game.update = function () { // End the game when all lives are lost if (lives.length === 0) { LK.showGameOver(); return; } // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; bullet.update(); // Check if bullet is off-screen if (bullet.y < -50) { bullet.destroy(); bullets.splice(i, 1); // Destroy a life var life = lives.pop(); life.destroy(); // Play the LoseLife sound LK.getSound('LoseLife').play(); continue; } // Check for collisions with targets for (var j = targets.length - 1; j >= 0; j--) { var target = targets[j]; if (bullet.intersects(target)) { // Increase score score += 10; scoreTxt.setText('Score: ' + score); // Destroy bullet and target bullet.destroy(); bullets.splice(i, 1); target.destroy(); targets.splice(j, 1); // Play the DestroyTarget sound LK.getSound('DestroyTarget').play(); // Spawn a new target spawnTarget(); // Increase the speed of targets for each target destroyed for (var t = 0; t < targets.length; t++) { targets[t].speedX *= 1.1; targets[t].speedY *= 1.1; } break; } } } }; // Handle touch or mouse down events game.down = function (x, y, obj) { object.x = x; if (bullets.length === 0) { shoot(object.x, object.y); } }; // Start the game spawnTarget();
/****
* Classes
****/
// Bullet class representing the bullets fired by the player
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;
};
});
// Life class representing the player's lives
var Life = Container.expand(function () {
var self = Container.call(this);
var lifeGraphics = self.attachAsset('Life', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Object class representing the object that stays below the screen and moves to where the click is
var Object = Container.expand(function () {
var self = Container.call(this);
var objectGraphics = self.attachAsset('object', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Add any object-specific update logic here
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Target class representing the targets to be hit
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
// Initialize random speed and direction for the target
self.speedX = (Math.random() - 0.5) * 10;
self.speedY = (Math.random() - 0.5) * 10;
self.update = function () {
// Add any target-specific update logic here
self.x += self.speedX;
self.y += self.speedY;
// Check if target is off-screen, then change direction
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 0 || self.y > 2732) {
self.speedY *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0,
anchorY: 0
});
// Initialize variables
var targets = [];
var bullets = [];
var score = 0;
var object = new Object();
object.y = 2732;
game.addChild(object);
// Initialize the player's lives
var lives = [];
for (var i = 0; i < 3; i++) {
var life = new Life();
life.x = 100 + i * 100;
life.y = 100;
lives.push(life);
game.addChild(life);
}
// Create score display
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF,
font: "'Comic Sans MS', cursive, sans-serif"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn a new target
function spawnTarget() {
var newTarget = new Target();
newTarget.x = Math.random() * 2048;
newTarget.y = Math.random() * 1000 + 500;
targets.push(newTarget);
game.addChild(newTarget);
}
// Function to handle shooting
function shoot(x, y) {
var newBullet = new Bullet();
newBullet.x = x;
newBullet.y = y;
bullets.push(newBullet);
game.addChild(newBullet);
}
// Handle game updates
game.update = function () {
// End the game when all lives are lost
if (lives.length === 0) {
LK.showGameOver();
return;
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
bullet.update();
// Check if bullet is off-screen
if (bullet.y < -50) {
bullet.destroy();
bullets.splice(i, 1);
// Destroy a life
var life = lives.pop();
life.destroy();
// Play the LoseLife sound
LK.getSound('LoseLife').play();
continue;
}
// Check for collisions with targets
for (var j = targets.length - 1; j >= 0; j--) {
var target = targets[j];
if (bullet.intersects(target)) {
// Increase score
score += 10;
scoreTxt.setText('Score: ' + score);
// Destroy bullet and target
bullet.destroy();
bullets.splice(i, 1);
target.destroy();
targets.splice(j, 1);
// Play the DestroyTarget sound
LK.getSound('DestroyTarget').play();
// Spawn a new target
spawnTarget();
// Increase the speed of targets for each target destroyed
for (var t = 0; t < targets.length; t++) {
targets[t].speedX *= 1.1;
targets[t].speedY *= 1.1;
}
break;
}
}
}
};
// Handle touch or mouse down events
game.down = function (x, y, obj) {
object.x = x;
if (bullets.length === 0) {
shoot(object.x, object.y);
}
};
// Start the game
spawnTarget();
Red and white target. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Dart shooting practice room. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Red heart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows