/****
* 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
});
var shadow = self.attachAsset('shadow', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.5;
self.bounceCount = 0;
self.isFlying = false;
self.depth = 0;
self.update = function () {
if (self.isFlying) {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityY += self.gravity;
// Update depth-based scaling
self.depth += self.velocityX * 0.001;
var scale = Math.max(0.3, 1 - self.depth * 0.5);
ballGraphics.scaleX = scale;
ballGraphics.scaleY = scale;
// Update shadow
shadow.x = 0;
shadow.y = 200 + self.depth * 100;
shadow.alpha = Math.max(0.2, 0.8 - self.depth);
shadow.scaleX = scale * 0.8;
shadow.scaleY = scale * 0.4;
// Bounce on ground
if (self.y > 2500) {
self.y = 2500;
self.velocityY *= -0.6;
self.velocityX *= 0.9;
self.bounceCount++;
LK.getSound('bounce').play();
if (self.bounceCount > 3 || Math.abs(self.velocityY) < 2) {
self.isFlying = false;
}
}
}
};
return self;
});
var Hoop = Container.expand(function () {
var self = Container.call(this);
var backboard = self.attachAsset('backboard', {
anchorX: 0.5,
anchorY: 0.5
});
var hoopRim = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
var net = self.attachAsset('net', {
anchorX: 0.5,
anchorY: 0.5
});
// Position elements
backboard.y = -40;
hoopRim.y = 0;
net.y = 20;
net.alpha = 0.7;
self.distance = 1;
self.scoreMultiplier = 1;
self.setDistance = function (distance) {
self.distance = distance;
self.scoreMultiplier = Math.floor(distance * 2);
var scale = Math.max(0.4, 1 / distance);
self.scaleX = scale;
self.scaleY = scale;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Court background
var court = game.addChild(LK.getAsset('court', {
anchorX: 0,
anchorY: 1,
x: 0,
y: 2732
}));
court.alpha = 0.8;
// Game variables
var basketball = null;
var hoops = [];
var trajectoryDots = [];
var dragStart = null;
var isDragging = false;
var shotsRemaining = 10;
var timeLeft = 60;
var consecutiveHits = 0;
var scoreMultiplier = 1;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var shotsTxt = new Text2('Shots: 10', {
size: 50,
fill: 0xFFFFFF
});
shotsTxt.anchor.set(0, 0);
shotsTxt.x = 50;
shotsTxt.y = 50;
LK.gui.topLeft.addChild(shotsTxt);
var timeTxt = new Text2('Time: 60', {
size: 50,
fill: 0xFFFFFF
});
timeTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(timeTxt);
var multiplierTxt = new Text2('x1', {
size: 80,
fill: 0xFFD700
});
multiplierTxt.anchor.set(0.5, 0.5);
multiplierTxt.x = 1024;
multiplierTxt.y = 300;
game.addChild(multiplierTxt);
// Initialize basketball
function createBasketball() {
if (basketball) {
basketball.destroy();
}
basketball = game.addChild(new Basketball());
basketball.x = 1024;
basketball.y = 2400;
}
// Initialize hoops
function createHoops() {
// Clear existing hoops
for (var i = hoops.length - 1; i >= 0; i--) {
hoops[i].destroy();
}
hoops = [];
// Create hoops at different distances
var positions = [{
x: 1024,
y: 800,
distance: 1
}, {
x: 800,
y: 600,
distance: 1.5
}, {
x: 1248,
y: 500,
distance: 2
}, {
x: 1024,
y: 400,
distance: 2.5
}];
for (var i = 0; i < positions.length; i++) {
var hoop = game.addChild(new Hoop());
hoop.x = positions[i].x;
hoop.y = positions[i].y;
hoop.setDistance(positions[i].distance);
hoops.push(hoop);
}
}
// Create trajectory preview
function updateTrajectory(startX, startY, endX, endY) {
// Clear existing dots
for (var i = trajectoryDots.length - 1; i >= 0; i--) {
trajectoryDots[i].destroy();
}
trajectoryDots = [];
if (!isDragging) return;
var dx = endX - startX;
var dy = endY - startY;
var power = Math.min(Math.sqrt(dx * dx + dy * dy) / 10, 15);
var velX = dx * 0.1;
var velY = dy * 0.1;
var x = startX;
var y = startY;
for (var i = 0; i < 20; i++) {
if (y > 2600) break;
var dot = game.addChild(LK.getAsset('trajectoryDot', {
anchorX: 0.5,
anchorY: 0.5,
x: x,
y: y
}));
dot.alpha = 0.7 - i * 0.03;
trajectoryDots.push(dot);
x += velX;
y += velY;
velY += 0.5;
}
}
// Check if basketball scored
function checkScoring() {
if (!basketball || !basketball.isFlying) return;
for (var i = 0; i < hoops.length; i++) {
var hoop = hoops[i];
var dx = basketball.x - hoop.x;
var dy = basketball.y - hoop.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Check if ball is near hoop and moving downward
if (distance < 80 * hoop.scaleX && basketball.velocityY > 0 && dy < 20 && dy > -20) {
// Score!
var points = 10 * hoop.scoreMultiplier * scoreMultiplier;
LK.setScore(LK.getScore() + points);
consecutiveHits++;
if (consecutiveHits >= 3) {
scoreMultiplier = Math.min(scoreMultiplier + 1, 5);
multiplierTxt.setText('x' + scoreMultiplier);
LK.effects.flashObject(multiplierTxt, 0xFFD700, 500);
}
LK.getSound('swoosh').play();
LK.setTimeout(function () {
LK.getSound('cheer').play();
}, 200);
// Flash effect
LK.effects.flashObject(hoop, 0x00FF00, 800);
scoreTxt.setText('Score: ' + LK.getScore());
return;
}
// Check for rim hit
if (distance < 100 * hoop.scaleX && Math.abs(dy) < 30) {
LK.getSound('clank').play();
consecutiveHits = 0;
scoreMultiplier = 1;
multiplierTxt.setText('x1');
}
}
}
// Timer
var gameTimer = LK.setInterval(function () {
timeLeft--;
timeTxt.setText('Time: ' + timeLeft);
if (timeLeft <= 0) {
LK.showGameOver();
}
}, 1000);
// Initialize game elements
createBasketball();
createHoops();
scoreTxt.setText('Score: ' + LK.getScore());
// Play background music
LK.playMusic('courtAmbience');
// Game controls
game.down = function (x, y, obj) {
if (!basketball || basketball.isFlying) return;
var dx = x - basketball.x;
var dy = y - basketball.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
isDragging = true;
dragStart = {
x: x,
y: y
};
}
};
game.move = function (x, y, obj) {
if (isDragging && dragStart) {
updateTrajectory(basketball.x, basketball.y, x, y);
}
};
game.up = function (x, y, obj) {
if (!isDragging || !dragStart || !basketball) return;
var dx = x - basketball.x;
var dy = y - basketball.y;
basketball.velocityX = dx * 0.08;
basketball.velocityY = dy * 0.08;
basketball.isFlying = true;
basketball.bounceCount = 0;
basketball.depth = 0;
shotsRemaining--;
shotsTxt.setText('Shots: ' + shotsRemaining);
isDragging = false;
dragStart = null;
// Clear trajectory
for (var i = trajectoryDots.length - 1; i >= 0; i--) {
trajectoryDots[i].destroy();
}
trajectoryDots = [];
if (shotsRemaining <= 0) {
LK.setTimeout(function () {
LK.showGameOver();
}, 3000);
}
// Create new basketball after delay
LK.setTimeout(function () {
if (shotsRemaining > 0) {
createBasketball();
}
}, 2000);
};
game.update = function () {
checkScoring();
// Move hoops occasionally for dynamic gameplay
if (LK.ticks % 600 === 0 && hoops.length > 0) {
var randomHoop = hoops[Math.floor(Math.random() * hoops.length)];
var newX = 500 + Math.random() * 1048;
var newY = 400 + Math.random() * 400;
tween(randomHoop, {
x: newX,
y: newY
}, {
duration: 2000,
easing: tween.easeInOut
});
}
}; /****
* 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
});
var shadow = self.attachAsset('shadow', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.5;
self.bounceCount = 0;
self.isFlying = false;
self.depth = 0;
self.update = function () {
if (self.isFlying) {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityY += self.gravity;
// Update depth-based scaling
self.depth += self.velocityX * 0.001;
var scale = Math.max(0.3, 1 - self.depth * 0.5);
ballGraphics.scaleX = scale;
ballGraphics.scaleY = scale;
// Update shadow
shadow.x = 0;
shadow.y = 200 + self.depth * 100;
shadow.alpha = Math.max(0.2, 0.8 - self.depth);
shadow.scaleX = scale * 0.8;
shadow.scaleY = scale * 0.4;
// Bounce on ground
if (self.y > 2500) {
self.y = 2500;
self.velocityY *= -0.6;
self.velocityX *= 0.9;
self.bounceCount++;
LK.getSound('bounce').play();
if (self.bounceCount > 3 || Math.abs(self.velocityY) < 2) {
self.isFlying = false;
}
}
}
};
return self;
});
var Hoop = Container.expand(function () {
var self = Container.call(this);
var backboard = self.attachAsset('backboard', {
anchorX: 0.5,
anchorY: 0.5
});
var hoopRim = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
var net = self.attachAsset('net', {
anchorX: 0.5,
anchorY: 0.5
});
// Position elements
backboard.y = -40;
hoopRim.y = 0;
net.y = 20;
net.alpha = 0.7;
self.distance = 1;
self.scoreMultiplier = 1;
self.setDistance = function (distance) {
self.distance = distance;
self.scoreMultiplier = Math.floor(distance * 2);
var scale = Math.max(0.4, 1 / distance);
self.scaleX = scale;
self.scaleY = scale;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Court background
var court = game.addChild(LK.getAsset('court', {
anchorX: 0,
anchorY: 1,
x: 0,
y: 2732
}));
court.alpha = 0.8;
// Game variables
var basketball = null;
var hoops = [];
var trajectoryDots = [];
var dragStart = null;
var isDragging = false;
var shotsRemaining = 10;
var timeLeft = 60;
var consecutiveHits = 0;
var scoreMultiplier = 1;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var shotsTxt = new Text2('Shots: 10', {
size: 50,
fill: 0xFFFFFF
});
shotsTxt.anchor.set(0, 0);
shotsTxt.x = 50;
shotsTxt.y = 50;
LK.gui.topLeft.addChild(shotsTxt);
var timeTxt = new Text2('Time: 60', {
size: 50,
fill: 0xFFFFFF
});
timeTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(timeTxt);
var multiplierTxt = new Text2('x1', {
size: 80,
fill: 0xFFD700
});
multiplierTxt.anchor.set(0.5, 0.5);
multiplierTxt.x = 1024;
multiplierTxt.y = 300;
game.addChild(multiplierTxt);
// Initialize basketball
function createBasketball() {
if (basketball) {
basketball.destroy();
}
basketball = game.addChild(new Basketball());
basketball.x = 1024;
basketball.y = 2400;
}
// Initialize hoops
function createHoops() {
// Clear existing hoops
for (var i = hoops.length - 1; i >= 0; i--) {
hoops[i].destroy();
}
hoops = [];
// Create hoops at different distances
var positions = [{
x: 1024,
y: 800,
distance: 1
}, {
x: 800,
y: 600,
distance: 1.5
}, {
x: 1248,
y: 500,
distance: 2
}, {
x: 1024,
y: 400,
distance: 2.5
}];
for (var i = 0; i < positions.length; i++) {
var hoop = game.addChild(new Hoop());
hoop.x = positions[i].x;
hoop.y = positions[i].y;
hoop.setDistance(positions[i].distance);
hoops.push(hoop);
}
}
// Create trajectory preview
function updateTrajectory(startX, startY, endX, endY) {
// Clear existing dots
for (var i = trajectoryDots.length - 1; i >= 0; i--) {
trajectoryDots[i].destroy();
}
trajectoryDots = [];
if (!isDragging) return;
var dx = endX - startX;
var dy = endY - startY;
var power = Math.min(Math.sqrt(dx * dx + dy * dy) / 10, 15);
var velX = dx * 0.1;
var velY = dy * 0.1;
var x = startX;
var y = startY;
for (var i = 0; i < 20; i++) {
if (y > 2600) break;
var dot = game.addChild(LK.getAsset('trajectoryDot', {
anchorX: 0.5,
anchorY: 0.5,
x: x,
y: y
}));
dot.alpha = 0.7 - i * 0.03;
trajectoryDots.push(dot);
x += velX;
y += velY;
velY += 0.5;
}
}
// Check if basketball scored
function checkScoring() {
if (!basketball || !basketball.isFlying) return;
for (var i = 0; i < hoops.length; i++) {
var hoop = hoops[i];
var dx = basketball.x - hoop.x;
var dy = basketball.y - hoop.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Check if ball is near hoop and moving downward
if (distance < 80 * hoop.scaleX && basketball.velocityY > 0 && dy < 20 && dy > -20) {
// Score!
var points = 10 * hoop.scoreMultiplier * scoreMultiplier;
LK.setScore(LK.getScore() + points);
consecutiveHits++;
if (consecutiveHits >= 3) {
scoreMultiplier = Math.min(scoreMultiplier + 1, 5);
multiplierTxt.setText('x' + scoreMultiplier);
LK.effects.flashObject(multiplierTxt, 0xFFD700, 500);
}
LK.getSound('swoosh').play();
LK.setTimeout(function () {
LK.getSound('cheer').play();
}, 200);
// Flash effect
LK.effects.flashObject(hoop, 0x00FF00, 800);
scoreTxt.setText('Score: ' + LK.getScore());
return;
}
// Check for rim hit
if (distance < 100 * hoop.scaleX && Math.abs(dy) < 30) {
LK.getSound('clank').play();
consecutiveHits = 0;
scoreMultiplier = 1;
multiplierTxt.setText('x1');
}
}
}
// Timer
var gameTimer = LK.setInterval(function () {
timeLeft--;
timeTxt.setText('Time: ' + timeLeft);
if (timeLeft <= 0) {
LK.showGameOver();
}
}, 1000);
// Initialize game elements
createBasketball();
createHoops();
scoreTxt.setText('Score: ' + LK.getScore());
// Play background music
LK.playMusic('courtAmbience');
// Game controls
game.down = function (x, y, obj) {
if (!basketball || basketball.isFlying) return;
var dx = x - basketball.x;
var dy = y - basketball.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
isDragging = true;
dragStart = {
x: x,
y: y
};
}
};
game.move = function (x, y, obj) {
if (isDragging && dragStart) {
updateTrajectory(basketball.x, basketball.y, x, y);
}
};
game.up = function (x, y, obj) {
if (!isDragging || !dragStart || !basketball) return;
var dx = x - basketball.x;
var dy = y - basketball.y;
basketball.velocityX = dx * 0.08;
basketball.velocityY = dy * 0.08;
basketball.isFlying = true;
basketball.bounceCount = 0;
basketball.depth = 0;
shotsRemaining--;
shotsTxt.setText('Shots: ' + shotsRemaining);
isDragging = false;
dragStart = null;
// Clear trajectory
for (var i = trajectoryDots.length - 1; i >= 0; i--) {
trajectoryDots[i].destroy();
}
trajectoryDots = [];
if (shotsRemaining <= 0) {
LK.setTimeout(function () {
LK.showGameOver();
}, 3000);
}
// Create new basketball after delay
LK.setTimeout(function () {
if (shotsRemaining > 0) {
createBasketball();
}
}, 2000);
};
game.update = function () {
checkScoring();
// Move hoops occasionally for dynamic gameplay
if (LK.ticks % 600 === 0 && hoops.length > 0) {
var randomHoop = hoops[Math.floor(Math.random() * hoops.length)];
var newX = 500 + Math.random() * 1048;
var newY = 400 + Math.random() * 400;
tween(randomHoop, {
x: newX,
y: newY
}, {
duration: 2000,
easing: tween.easeInOut
});
}
};