/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Basketball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.8;
self.bounce = 0.6;
self.isActive = false;
self.hasScored = false;
self.update = function () {
if (self.isActive) {
self.velocityY += self.gravity;
self.x += self.velocityX;
self.y += self.velocityY;
// Check ground collision
if (self.y > 2500) {
self.reset();
}
// Check side boundaries
if (self.x < 60 || self.x > 1988) {
self.velocityX *= -self.bounce;
self.x = Math.max(60, Math.min(1988, self.x));
}
}
};
self.shoot = function (powerX, powerY) {
self.velocityX = powerX;
self.velocityY = powerY;
self.isActive = true;
self.hasScored = false;
};
self.reset = function () {
self.x = startX;
self.y = startY;
self.velocityX = 0;
self.velocityY = 0;
self.isActive = false;
self.hasScored = false;
};
return self;
});
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
var backboardGraphics = self.attachAsset('backboard', {
anchorX: 0.5,
anchorY: 0.5
});
backboardGraphics.x = 120;
backboardGraphics.y = -50;
self.moveToRandomPosition = function () {
var newX = 300 + Math.random() * 1400;
var newY = 400 + Math.random() * 800;
tween(self, {
x: newX,
y: newY
}, {
duration: 1000,
easing: tween.easeOut
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var startX = 1024;
var startY = 2400;
var basketball = null;
var hoop = null;
var isAiming = false;
var aimStartX = 0;
var aimStartY = 0;
var consecutiveShots = 0;
var missedShots = 0;
var maxMisses = 3;
var aimLines = [];
// Create score display
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create consecutive shots display
var streakTxt = new Text2('Streak: 0', {
size: 60,
fill: 0xFFFF00
});
streakTxt.anchor.set(0, 0);
streakTxt.x = 50;
streakTxt.y = 100;
LK.gui.topLeft.addChild(streakTxt);
// Create misses display
var missesTxt = new Text2('Misses: 0/3', {
size: 60,
fill: 0xFF0000
});
missesTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(missesTxt);
// Initialize basketball
basketball = game.addChild(new Basketball());
basketball.x = startX;
basketball.y = startY;
// Initialize hoop
hoop = game.addChild(new Hoop());
hoop.x = 1024;
hoop.y = 600;
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
streakTxt.setText('Streak: ' + consecutiveShots);
missesTxt.setText('Misses: ' + missedShots + '/' + maxMisses);
}
function createAimLine(x, y) {
var line = game.addChild(LK.getAsset('aimLine', {
anchorX: 0.5,
anchorY: 0.5
}));
line.x = x;
line.y = y;
line.alpha = 0.7;
aimLines.push(line);
tween(line, {
alpha: 0
}, {
duration: 200,
onFinish: function onFinish() {
line.destroy();
}
});
}
function clearAimLines() {
for (var i = aimLines.length - 1; i >= 0; i--) {
aimLines[i].destroy();
aimLines.splice(i, 1);
}
}
function checkCollisions() {
// Check hoop collision (scoring)
if (basketball.isActive && !basketball.hasScored) {
var ballLeft = basketball.x - 60;
var ballRight = basketball.x + 60;
var ballTop = basketball.y - 60;
var ballBottom = basketball.y + 60;
var hoopLeft = hoop.x - 100;
var hoopRight = hoop.x + 100;
var hoopTop = hoop.y - 20;
var hoopBottom = hoop.y + 20;
// Check if ball is passing through hoop from above
if (ballLeft > hoopLeft && ballRight < hoopRight && ballTop < hoopBottom && ballBottom > hoopTop && basketball.velocityY > 0) {
basketball.hasScored = true;
consecutiveShots++;
var points = 10 + (consecutiveShots - 1) * 5;
LK.setScore(LK.getScore() + points);
LK.getSound('swish').play();
LK.effects.flashScreen(0x00ff00, 500);
hoop.moveToRandomPosition();
updateScore();
setTimeout(function () {
basketball.reset();
}, 1000);
}
}
// Check hoop rim collision
if (basketball.isActive) {
var hoopLeft = hoop.x - 100;
var hoopRight = hoop.x + 100;
var hoopY = hoop.y;
if (basketball.x > hoopLeft && basketball.x < hoopRight && Math.abs(basketball.y - hoopY) < 80 && basketball.velocityY > 0) {
basketball.velocityY *= -basketball.bounce;
basketball.velocityX *= 0.8;
LK.getSound('bounce').play();
}
}
// Check backboard collision
var backboardX = hoop.x + 120;
var backboardTop = hoop.y - 75;
var backboardBottom = hoop.y + 75;
if (basketball.isActive && Math.abs(basketball.x - backboardX) < 70 && basketball.y > backboardTop && basketball.y < backboardBottom) {
basketball.velocityX *= -basketball.bounce;
basketball.velocityY *= 0.9;
LK.getSound('bounce').play();
}
}
function checkMiss() {
if (basketball.isActive && basketball.y > 2300 && !basketball.hasScored) {
missedShots++;
consecutiveShots = 0;
updateScore();
if (missedShots >= maxMisses) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}
game.down = function (x, y, obj) {
if (!basketball.isActive) {
isAiming = true;
aimStartX = x;
aimStartY = y;
clearAimLines();
}
};
game.move = function (x, y, obj) {
if (isAiming && !basketball.isActive) {
var deltaX = x - aimStartX;
var deltaY = y - aimStartY;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > 20) {
var steps = Math.floor(distance / 30);
for (var i = 1; i <= steps; i++) {
var lineX = aimStartX + deltaX * i / steps;
var lineY = aimStartY + deltaY * i / steps;
createAimLine(lineX, lineY);
}
}
}
};
game.up = function (x, y, obj) {
if (isAiming && !basketball.isActive) {
var deltaX = x - aimStartX;
var deltaY = y - aimStartY;
var power = Math.min(Math.sqrt(deltaX * deltaX + deltaY * deltaY) / 20, 20);
if (power > 2) {
var powerX = deltaX / 20;
var powerY = deltaY / 20;
basketball.shoot(powerX, powerY);
}
isAiming = false;
clearAimLines();
}
};
game.update = function () {
checkCollisions();
checkMiss();
// Win condition
if (LK.getScore() >= 500) {
LK.showYouWin();
}
};
updateScore(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Basketball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.8;
self.bounce = 0.6;
self.isActive = false;
self.hasScored = false;
self.update = function () {
if (self.isActive) {
self.velocityY += self.gravity;
self.x += self.velocityX;
self.y += self.velocityY;
// Check ground collision
if (self.y > 2500) {
self.reset();
}
// Check side boundaries
if (self.x < 60 || self.x > 1988) {
self.velocityX *= -self.bounce;
self.x = Math.max(60, Math.min(1988, self.x));
}
}
};
self.shoot = function (powerX, powerY) {
self.velocityX = powerX;
self.velocityY = powerY;
self.isActive = true;
self.hasScored = false;
};
self.reset = function () {
self.x = startX;
self.y = startY;
self.velocityX = 0;
self.velocityY = 0;
self.isActive = false;
self.hasScored = false;
};
return self;
});
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
var backboardGraphics = self.attachAsset('backboard', {
anchorX: 0.5,
anchorY: 0.5
});
backboardGraphics.x = 120;
backboardGraphics.y = -50;
self.moveToRandomPosition = function () {
var newX = 300 + Math.random() * 1400;
var newY = 400 + Math.random() * 800;
tween(self, {
x: newX,
y: newY
}, {
duration: 1000,
easing: tween.easeOut
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var startX = 1024;
var startY = 2400;
var basketball = null;
var hoop = null;
var isAiming = false;
var aimStartX = 0;
var aimStartY = 0;
var consecutiveShots = 0;
var missedShots = 0;
var maxMisses = 3;
var aimLines = [];
// Create score display
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create consecutive shots display
var streakTxt = new Text2('Streak: 0', {
size: 60,
fill: 0xFFFF00
});
streakTxt.anchor.set(0, 0);
streakTxt.x = 50;
streakTxt.y = 100;
LK.gui.topLeft.addChild(streakTxt);
// Create misses display
var missesTxt = new Text2('Misses: 0/3', {
size: 60,
fill: 0xFF0000
});
missesTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(missesTxt);
// Initialize basketball
basketball = game.addChild(new Basketball());
basketball.x = startX;
basketball.y = startY;
// Initialize hoop
hoop = game.addChild(new Hoop());
hoop.x = 1024;
hoop.y = 600;
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
streakTxt.setText('Streak: ' + consecutiveShots);
missesTxt.setText('Misses: ' + missedShots + '/' + maxMisses);
}
function createAimLine(x, y) {
var line = game.addChild(LK.getAsset('aimLine', {
anchorX: 0.5,
anchorY: 0.5
}));
line.x = x;
line.y = y;
line.alpha = 0.7;
aimLines.push(line);
tween(line, {
alpha: 0
}, {
duration: 200,
onFinish: function onFinish() {
line.destroy();
}
});
}
function clearAimLines() {
for (var i = aimLines.length - 1; i >= 0; i--) {
aimLines[i].destroy();
aimLines.splice(i, 1);
}
}
function checkCollisions() {
// Check hoop collision (scoring)
if (basketball.isActive && !basketball.hasScored) {
var ballLeft = basketball.x - 60;
var ballRight = basketball.x + 60;
var ballTop = basketball.y - 60;
var ballBottom = basketball.y + 60;
var hoopLeft = hoop.x - 100;
var hoopRight = hoop.x + 100;
var hoopTop = hoop.y - 20;
var hoopBottom = hoop.y + 20;
// Check if ball is passing through hoop from above
if (ballLeft > hoopLeft && ballRight < hoopRight && ballTop < hoopBottom && ballBottom > hoopTop && basketball.velocityY > 0) {
basketball.hasScored = true;
consecutiveShots++;
var points = 10 + (consecutiveShots - 1) * 5;
LK.setScore(LK.getScore() + points);
LK.getSound('swish').play();
LK.effects.flashScreen(0x00ff00, 500);
hoop.moveToRandomPosition();
updateScore();
setTimeout(function () {
basketball.reset();
}, 1000);
}
}
// Check hoop rim collision
if (basketball.isActive) {
var hoopLeft = hoop.x - 100;
var hoopRight = hoop.x + 100;
var hoopY = hoop.y;
if (basketball.x > hoopLeft && basketball.x < hoopRight && Math.abs(basketball.y - hoopY) < 80 && basketball.velocityY > 0) {
basketball.velocityY *= -basketball.bounce;
basketball.velocityX *= 0.8;
LK.getSound('bounce').play();
}
}
// Check backboard collision
var backboardX = hoop.x + 120;
var backboardTop = hoop.y - 75;
var backboardBottom = hoop.y + 75;
if (basketball.isActive && Math.abs(basketball.x - backboardX) < 70 && basketball.y > backboardTop && basketball.y < backboardBottom) {
basketball.velocityX *= -basketball.bounce;
basketball.velocityY *= 0.9;
LK.getSound('bounce').play();
}
}
function checkMiss() {
if (basketball.isActive && basketball.y > 2300 && !basketball.hasScored) {
missedShots++;
consecutiveShots = 0;
updateScore();
if (missedShots >= maxMisses) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}
game.down = function (x, y, obj) {
if (!basketball.isActive) {
isAiming = true;
aimStartX = x;
aimStartY = y;
clearAimLines();
}
};
game.move = function (x, y, obj) {
if (isAiming && !basketball.isActive) {
var deltaX = x - aimStartX;
var deltaY = y - aimStartY;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > 20) {
var steps = Math.floor(distance / 30);
for (var i = 1; i <= steps; i++) {
var lineX = aimStartX + deltaX * i / steps;
var lineY = aimStartY + deltaY * i / steps;
createAimLine(lineX, lineY);
}
}
}
};
game.up = function (x, y, obj) {
if (isAiming && !basketball.isActive) {
var deltaX = x - aimStartX;
var deltaY = y - aimStartY;
var power = Math.min(Math.sqrt(deltaX * deltaX + deltaY * deltaY) / 20, 20);
if (power > 2) {
var powerX = deltaX / 20;
var powerY = deltaY / 20;
basketball.shoot(powerX, powerY);
}
isAiming = false;
clearAimLines();
}
};
game.update = function () {
checkCollisions();
checkMiss();
// Win condition
if (LK.getScore() >= 500) {
LK.showYouWin();
}
};
updateScore();