/****
* Classes
****/
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 < 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a class for the main character
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Character update logic
};
});
// Define a class for obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Obstacle update logic
};
});
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.x += self.speed;
if (self.x > 2048 - 100 || self.x < 0) {
self.speed *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var character = game.addChild(new Character());
character.x = 1024; // Center horizontally
character.y = 2600; // Near the bottom
var bullets = [];
var target = game.addChild(new Target());
target.x = Math.random() * 2048;
target.y = 200;
var score = 0;
// Function to handle character movement
function moveCharacter(x, y, obj) {
character.x = x;
character.y = y;
}
// Function to create obstacles
function createObstacle() {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048; // Random horizontal position
obstacle.y = 0; // Start at the top
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Game update function
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
bullet.update();
if (bullet.intersects(target)) {
bullet.destroy();
bullets.splice(i, 1);
target.x = Math.random() * 2048;
score++;
target.speed += 0.1; // Increase difficulty
}
}
// Fire bullet
if (LK.ticks % 30 == 0) {
var newBullet = new Bullet();
newBullet.x = character.x;
newBullet.y = character.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
};
// Handle touch/mouse events
game.down = function (x, y, obj) {
moveCharacter(x, y, obj);
};
game.move = function (x, y, obj) {
moveCharacter(x, y, obj);
};
game.up = function (x, y, obj) {
// Stop character movement
};
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update score display
function updateScore() {
scoreTxt.setText("Score: " + score);
}
// Set interval to update score display
LK.setInterval(updateScore, 1000); /****
* Classes
****/
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 < 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a class for the main character
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Character update logic
};
});
// Define a class for obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Obstacle update logic
};
});
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.x += self.speed;
if (self.x > 2048 - 100 || self.x < 0) {
self.speed *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var character = game.addChild(new Character());
character.x = 1024; // Center horizontally
character.y = 2600; // Near the bottom
var bullets = [];
var target = game.addChild(new Target());
target.x = Math.random() * 2048;
target.y = 200;
var score = 0;
// Function to handle character movement
function moveCharacter(x, y, obj) {
character.x = x;
character.y = y;
}
// Function to create obstacles
function createObstacle() {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048; // Random horizontal position
obstacle.y = 0; // Start at the top
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Game update function
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
bullet.update();
if (bullet.intersects(target)) {
bullet.destroy();
bullets.splice(i, 1);
target.x = Math.random() * 2048;
score++;
target.speed += 0.1; // Increase difficulty
}
}
// Fire bullet
if (LK.ticks % 30 == 0) {
var newBullet = new Bullet();
newBullet.x = character.x;
newBullet.y = character.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
};
// Handle touch/mouse events
game.down = function (x, y, obj) {
moveCharacter(x, y, obj);
};
game.move = function (x, y, obj) {
moveCharacter(x, y, obj);
};
game.up = function (x, y, obj) {
// Stop character movement
};
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update score display
function updateScore() {
scoreTxt.setText("Score: " + score);
}
// Set interval to update score display
LK.setInterval(updateScore, 1000);