/****
* Classes
****/
// Cursor class
var Cursor = Container.expand(function () {
var self = Container.call(this);
var cursorGraphics = self.attachAsset('hand', {
anchorX: 0.5,
anchorY: 0.5
});
});
//<Assets used in the game will automatically appear here>
// Dart class
var Dart = Container.expand(function () {
var self = Container.call(this);
var dartGraphics = self.attachAsset('dart', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.gravity = 0.5;
self.update = function () {
self.speedY += self.gravity;
self.x += self.speedX;
self.y += self.speedY;
// Rotate dart in the direction of movement
self.rotation = Math.atan2(self.speedY, self.speedX) + Math.PI / 2;
};
});
// Target class
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
// Keep the target within the screen bounds
if (self.x < 0) {
self.x = 0;
}
if (self.x > 2048) {
self.x = 2048;
}
if (self.y < 0) {
self.y = 0;
}
if (self.y > 2732 / 2) {
self.y = 2732 / 2;
}
};
});
/****
* 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
});
game.addChild(background);
// Initialize score
var score = 0;
// Initialize player lives
var lives = 3;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Display player lives
var livesTxt = new Text2('Lives: ' + lives, {
size: 150,
fill: "#ff0000"
});
livesTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(livesTxt);
livesTxt.y = scoreTxt.height;
// Initialize target
var target = game.addChild(new Target());
target.x = 2048 / 2;
target.y = 2732 / 4;
// Initialize cursor
var cursor = game.addChild(new Cursor());
// Initialize darts array
var darts = [];
// Mouse or touch down on the game
game.down = function (x, y, obj) {
var newDart = new Dart();
newDart.x = x;
newDart.y = y;
newDart.startX = x;
newDart.startY = y;
newDart.lastX = x;
newDart.lastY = y;
darts.push(newDart);
game.addChild(newDart);
game.currentDart = newDart;
};
// Mouse or touch move on the game
game.move = function (x, y, obj) {
if (y > 2732 * 2 / 3) {
// Check if the y-coordinate is in the lower third of the screen
if (game.currentDart) {
var dx = x - game.currentDart.startX;
var dy = y - game.currentDart.startY;
var distance = Math.sqrt(dx * dx + dy * dy);
var time = 1; // Assuming 1 frame for simplicity, adjust as needed
var speed = distance / time * 0.05; // Reduce speed by a factor of 20
var angle = Math.atan2(dy, dx);
game.currentDart.speedX = Math.cos(angle) * speed;
game.currentDart.speedY = Math.sin(angle) * speed;
game.currentDart.startX = x;
game.currentDart.startY = y;
}
// Update cursor position
cursor.x = x;
cursor.y = y;
}
};
// Mouse or touch up on the game
game.up = function (x, y, obj) {
if (game.currentDart) {
var dx = x - game.currentDart.lastX;
var dy = y - game.currentDart.lastY;
var distance = Math.sqrt(dx * dx + dy * dy);
var time = 1; // Assuming 1 frame for simplicity, adjust as needed
var speed = distance / time * 0.05; // Reduce speed by a factor of 20
var angle = Math.atan2(dy, dx);
game.currentDart.speedX = Math.cos(angle) * speed;
game.currentDart.speedY = Math.sin(angle) * speed;
game.currentDart = null;
}
};
// Update game every tick
game.update = function () {
for (var i = darts.length - 1; i >= 0; i--) {
if (darts[i].intersects(target)) {
// Update score
score += 1;
scoreTxt.setText(score);
darts[i].destroy();
darts.splice(i, 1);
// Move target to a random position
target.x = Math.random() * 2048;
target.y = Math.random() * 2732 / 2; // Limit to upper half of the screen
// If score is 10 or more, start moving the target
if (score >= 10) {
target.speedX = (Math.random() - 0.5) * 10;
target.speedY = (Math.random() - 0.5) * 10;
}
} else if (darts[i].x > 2048 || darts[i].y > 2732 || darts[i].y < 0 || darts[i].x < 0) {
darts[i].destroy();
darts.splice(i, 1);
// Decrease player lives on miss
lives--;
// Update lives display
livesTxt.setText('Lives: ' + lives);
// Trigger game over when player lives reach 0
if (lives <= 0) {
LK.showGameOver();
}
} else {
darts[i].update();
}
}
};