/****
* Classes
****/
// Create a class for the bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10; // Set bullet speed
self.update = function () {
self.y += self.speed;
};
});
// The assets will be automatically created and loaded by the LK engine
// Create a class for the player's character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Player's movement logic
self.x += self.vx || 0;
self.y += self.vy || 0;
};
});
// Create a class for the targets
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Target's movement logic goes here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundImage: 'tyson_beyblade' // Set background image to Tyson from Beyblade
});
/****
* Game Code
****/
// Add player movement controls
game.down = function (x, y, obj) {
player.vx = (x - player.x) * 0.1;
player.vy = (y - player.y) * 0.1;
};
game.up = function (x, y, obj) {
player.vx = 0;
player.vy = 0;
};
// Add arrow key movement controls
game.move = function (x, y, obj) {
if (obj.event && obj.event.key === 'ArrowUp') {
player.vy = -5;
} else if (obj.event.key === 'ArrowDown') {
player.vy = 5;
} else if (obj.event.key === 'ArrowLeft') {
player.vx = -5;
} else if (obj.event.key === 'ArrowRight') {
player.vx = 5;
}
};
game.up = function (x, y, obj) {
if (obj.event && ['ArrowUp', 'ArrowDown'].includes(obj.event.key)) {
player.vy = 0;
}
if (obj.event && ['ArrowLeft', 'ArrowRight'].includes(obj.event.key)) {
player.vx = 0;
}
};
// Initialize the player and add it to the game
var player = game.addChild(new Player());
player.x = 2048 / 2; // Position the player at the center of the screen
player.y = 2732 / 2;
// Initialize an array to hold the targets
var targets = [];
// Create a function to spawn a new target
function spawnTarget() {
var target = new Target();
target.x = Math.random() * 2048; // Position the target at a random x-coordinate
target.y = Math.random() * 2732; // Position the target at a random y-coordinate
targets.push(target);
game.addChild(target);
}
// Call the spawnTarget function every 2 seconds
LK.setInterval(spawnTarget, 2000);
// Initialize an array to hold the bullets
var bullets = [];
// Update the game state every frame
game.update = function () {
// Check if 20 targets have been displayed
if (targets.length >= 20) {
LK.showGameOver();
return;
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].y < -50) {
bullets[j].destroy();
bullets.splice(j, 1);
}
}
// Fire bullet every 30 ticks
if (LK.ticks % 30 == 0) {
var newBullet = new Bullet();
newBullet.x = player.x;
newBullet.y = player.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
// Check for collisions between bullets and targets
for (var i = targets.length - 1; i >= 0; i--) {
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].intersects(targets[i])) {
// If a collision is detected, remove the target and bullet from the game
targets[i].destroy();
targets.splice(i, 1);
bullets[j].destroy();
bullets.splice(j, 1);
// Increase the player's score
LK.setScore(LK.getScore() + 1);
break;
}
}
}
}; /****
* Classes
****/
// Create a class for the bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10; // Set bullet speed
self.update = function () {
self.y += self.speed;
};
});
// The assets will be automatically created and loaded by the LK engine
// Create a class for the player's character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Player's movement logic
self.x += self.vx || 0;
self.y += self.vy || 0;
};
});
// Create a class for the targets
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Target's movement logic goes here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundImage: 'tyson_beyblade' // Set background image to Tyson from Beyblade
});
/****
* Game Code
****/
// Add player movement controls
game.down = function (x, y, obj) {
player.vx = (x - player.x) * 0.1;
player.vy = (y - player.y) * 0.1;
};
game.up = function (x, y, obj) {
player.vx = 0;
player.vy = 0;
};
// Add arrow key movement controls
game.move = function (x, y, obj) {
if (obj.event && obj.event.key === 'ArrowUp') {
player.vy = -5;
} else if (obj.event.key === 'ArrowDown') {
player.vy = 5;
} else if (obj.event.key === 'ArrowLeft') {
player.vx = -5;
} else if (obj.event.key === 'ArrowRight') {
player.vx = 5;
}
};
game.up = function (x, y, obj) {
if (obj.event && ['ArrowUp', 'ArrowDown'].includes(obj.event.key)) {
player.vy = 0;
}
if (obj.event && ['ArrowLeft', 'ArrowRight'].includes(obj.event.key)) {
player.vx = 0;
}
};
// Initialize the player and add it to the game
var player = game.addChild(new Player());
player.x = 2048 / 2; // Position the player at the center of the screen
player.y = 2732 / 2;
// Initialize an array to hold the targets
var targets = [];
// Create a function to spawn a new target
function spawnTarget() {
var target = new Target();
target.x = Math.random() * 2048; // Position the target at a random x-coordinate
target.y = Math.random() * 2732; // Position the target at a random y-coordinate
targets.push(target);
game.addChild(target);
}
// Call the spawnTarget function every 2 seconds
LK.setInterval(spawnTarget, 2000);
// Initialize an array to hold the bullets
var bullets = [];
// Update the game state every frame
game.update = function () {
// Check if 20 targets have been displayed
if (targets.length >= 20) {
LK.showGameOver();
return;
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].y < -50) {
bullets[j].destroy();
bullets.splice(j, 1);
}
}
// Fire bullet every 30 ticks
if (LK.ticks % 30 == 0) {
var newBullet = new Bullet();
newBullet.x = player.x;
newBullet.y = player.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
// Check for collisions between bullets and targets
for (var i = targets.length - 1; i >= 0; i--) {
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].intersects(targets[i])) {
// If a collision is detected, remove the target and bullet from the game
targets[i].destroy();
targets.splice(i, 1);
bullets[j].destroy();
bullets.splice(j, 1);
// Increase the player's score
LK.setScore(LK.getScore() + 1);
break;
}
}
}
};