/****
* Classes
****/
// Assets will be automatically generated based on usage in the code.
// Ball class for the basketball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.move = function () {
self.x += self.speedX;
self.y += self.speedY;
};
self.reset = function () {
self.x = game.width / 2;
self.y = game.height - 200;
self.speedX = 0;
self.speedY = 0;
var hoopX, hoopY;
hoopX = Math.random() * game.width;
hoopY = Math.random() * game.height;
hoop.setPosition(hoopX, hoopY);
};
});
// Hoop class for the basketball hoop
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
});
// Obstacle class for the game ending obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.radius = Math.random() * 50 + 50; // radius of damage, random between 50 and 100
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: Math.floor(Math.random() * 16777215) // Randomize the color of the background each game
});
/****
* Game Code
****/
var background = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: game.width / 2,
y: game.height / 2,
width: game.width,
height: game.height
}));
var ball = game.addChild(new Ball());
var playerMoved = false;
var hoop = game.addChild(new Hoop());
var obstacles = [];
for (var i = 0; i < 10; i++) {
var obstacle = game.addChild(new Obstacle());
do {
obstacle.x = Math.random() * game.width;
obstacle.y = Math.random() * game.height;
} while (Math.abs(obstacle.x - ball.x) < 200 || Math.abs(obstacle.y - ball.y) < 200);
obstacles.push(obstacle);
}
var scoreTxt = new Text2('0', {
size: 150,
fill: "#FFFF00"
});
LK.gui.top.addChild(scoreTxt);
// Initialize stopwatch
var stopwatch = new Text2('0:00', {
size: 100,
fill: "#FFFFFF"
});
LK.gui.topLeft.addChild(stopwatch);
// Initialize pause button
var pauseButton = new Text2('Pause', {
size: 100,
fill: "#FFFFFF"
});
LK.gui.topRight.addChild(pauseButton);
// Add event listener to pause button
pauseButton.on('down', function () {
if (game.paused) {
game.resume();
pauseButton.setText('Pause');
} else {
game.pause();
pauseButton.setText('Resume');
}
});
// Initialize stopwatch but do not start it
var stopwatchSeconds = 0;
var stopwatchInterval = null;
// Set initial positions
ball.reset();
hoop.setPosition(game.width / 2, 100);
// Score variable
var score = 0;
// Touch event to launch the ball
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
var dx = pos.x - ball.x;
var dy = pos.y - ball.y;
var distance = Math.sqrt(dx * dx + dy * dy);
ball.speedX = dx / distance * 10;
ball.speedY = dy / distance * 10;
playerMoved = true;
// Start the stopwatch when the player moves for the first time
if (stopwatchInterval === null) {
stopwatchInterval = LK.setInterval(function () {
stopwatchSeconds++;
var minutes = Math.floor(stopwatchSeconds / 60);
var seconds = stopwatchSeconds % 60;
stopwatch.setText(minutes + ':' + (seconds < 10 ? '0' : '') + seconds);
}, 1000);
}
});
// Game tick event
LK.on('tick', function () {
ball.move();
// Check for collision with hoop
if (ball.intersects(hoop)) {
// If the hoop is on the top half of the screen, the goal is worth 3 points
// If the hoop is on the bottom half of the screen, the goal is worth 2 points
if (hoop.y < game.height / 2) {
score += 3;
} else {
score += 2;
}
scoreTxt.setText(score.toString());
// Create a small flash of text that says "+3" or "+2" every time there is a score
var scoreFlash = new Text2(hoop.y < game.height / 2 ? '+3' : '+2', {
size: 100,
fill: "#ffffff"
});
scoreFlash.x = hoop.x;
scoreFlash.y = hoop.y;
game.addChild(scoreFlash);
var fadeInterval = LK.setInterval(function () {
scoreFlash.alpha -= 0.02; // decrease the alpha value to create a fade out effect
if (scoreFlash.alpha <= 0) {
game.removeChild(scoreFlash);
LK.clearInterval(fadeInterval); // clear the interval once the text has fully faded
}
}, 10); // run the fade out effect every 10ms
ball.reset();
}
// Check for collision with obstacles
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var dx = ball.x - obstacle.x;
var dy = ball.y - obstacle.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (playerMoved && distance < obstacle.radius) {
LK.showGameOver();
break;
}
// Move the obstacle around the screen asynchronously
// Alternate between clockwise and counterclockwise movement for each obstacle
if (i % 2 == 0) {
obstacle.x += Math.cos((LK.ticks + i * 6) / 60) * 5;
obstacle.y += Math.sin((LK.ticks + i * 6) / 60) * 5;
} else {
obstacle.x -= Math.cos((LK.ticks + i * 6) / 60) * 5;
obstacle.y -= Math.sin((LK.ticks + i * 6) / 60) * 5;
}
// Restrict obstacle movement within screen limits
if (obstacle.x < 0) {
obstacle.x = 0;
if (ball.y < -50 || ball.y > game.height + 50 || ball.x < -50 || ball.x > game.width + 50) {
ball.reset();
}
// Wrap obstacle around screen
}
if (obstacle.x > game.width) {
obstacle.x = game.width;
}
if (obstacle.y < 0) {
obstacle.y = 0;
}
if (obstacle.y > game.height * 0.9) {
obstacle.y = game.height * 0.9;
}
if (ball.y < -50 || ball.y > game.height + 50 || ball.x < -50 || ball.x > game.width + 50) {
LK.showGameOver();
}
}
});