/**** * Classes ****/ // BackgroundContainer class var BackgroundContainer = Container.expand(function () { var self = Container.call(this); var backgroundGraphics = self.attachAsset('background', { anchorX: 0.5, anchorY: 0.5, scaleX: 2048 / 1000, scaleY: 2732 / 1000 }); }); // Assets will be automatically created based on usage in the code. // Ball class var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.speedY = 0; self.gravity = 1; self.bounce = -0.7; self.update = function () { if (!isStuck) { self.speedY += self.gravity; self.y += self.speedY; if (self.y > 2732 - self.height / 2) { self.speedY *= self.bounce; self.y = 2732 - self.height / 2; } self.rotation += 0.1; // Add rotation to the ball } else { self.x = cursorX; } }; }); // ForegroundContainer class var ForegroundContainer = Container.expand(function () { var self = Container.call(this); }); // Hoop class var Hoop = Container.expand(function () { var self = Container.call(this); var hoopGraphics = self.attachAsset('hoop', { anchorX: 0.5, anchorY: 0.5, alpha: 1 }); }); // MidgroundContainer class var MidgroundContainer = Container.expand(function () { var self = Container.call(this); }); // Panel class var Panel = Container.expand(function () { var self = Container.call(this); var panelGraphics = self.attachAsset('panel', { anchorX: 0.5, anchorY: 0.5 }); self.direction = 1; // 1 for moving right, -1 for moving left self.move = function () { self.x += panelSpeed * self.direction; if (self.x > 2048 - self.width / 2) { self.direction = -1; } else if (self.x < self.width / 2) { self.direction = 1; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to simulate sky }); /**** * Game Code ****/ game.on('up', function (obj) { isStuck = false; }); var ball; var hoop; var score = 0; var isStuck = true; var cursorX = 1024; // Initialize cursorX to the center of the screen var scoreTxt; var panel; var panelSpeed = 10; // Initialize panel's speed var backgroundContainer = game.addChild(new BackgroundContainer()); backgroundContainer.x = 1024; // Center horizontally backgroundContainer.y = 1366; // Center vertically var midgroundContainer = game.addChild(new MidgroundContainer()); var foregroundContainer = game.addChild(new ForegroundContainer()); function initGame() { ball = foregroundContainer.addChild(new Ball()); ball.x = 1024; // Center horizontally ball.y = 350; // Starting height panel = midgroundContainer.addChild(new Panel()); panel.x = 1024; // Center horizontally panel.y = 700; // Position from bottom hoop = panel.addChild(new Hoop()); hoop.x = 0; // Center horizontally hoop.y = 130; // Position from bottom var visibleHoop = foregroundContainer.attachAsset('visibleHoop', { anchorX: 0.5, anchorY: 0.5, x: panel.x, y: panel.y + hoop.height + 250 // Position 100 pixels below the hoop }); scoreTxt = panel.addChild(new Text2(score.toString(), { size: 170, fill: "#ffffff", stroke: "#000000", strokeThickness: 15 })); scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text. scoreTxt.x = 0; // Position score text in the middle of the panel scoreTxt.y = -150; // Move score text higher game.on('move', function (obj) { var event = obj.event; var pos = event.getLocalPosition(game); cursorX = pos.x; }); game.on('down', function (obj) { if (isStuck) { var event = obj.event; var pos = event.getLocalPosition(game); ball.x = pos.x; } }); LK.on('tick', function () { if (isStuck) { ball.x = cursorX; } ball.update(); panel.move(); visibleHoop.x = panel.x; visibleHoop.y = panel.y + hoop.height + 290; checkScore(); if (ball.y >= 2732 - ball.height / 2 && !isStuck && !ball.scoring) { LK.showGameOver(); } }); } function checkScore() { if (ball.intersects(hoop) && !ball.scoring) { score += 1; scoreTxt.setText(score.toString()); ball.scoring = true; // Mark the ball as having scored ball.x = hoop.x + hoop.parent.x; // Move the ball's horizontal position to match the center of the hoop } // Handle ball reset if it has scored if (ball.y >= 2732 - ball.height / 2 && ball.scoring) { // Reset ball position for another drop ball.x = 1024; ball.y = 350; ball.speedY = 0; ball.rotation = 0; // Reset ball's rotation isStuck = true; ball.scoring = false; // Reset scoring status for the next drop var panelLowerLimit = 2400; // Define the lowest point the panel can move to if (panel.y < panelLowerLimit) { // Check if the panel is above the bottom of the screen panel.y += 50; // Move the panel 50 pixels lower } panelSpeed *= 1.05; // Increase the panel's speed by 20% } } initGame();
/****
* Classes
****/
// BackgroundContainer class
var BackgroundContainer = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2048 / 1000,
scaleY: 2732 / 1000
});
});
// Assets will be automatically created based on usage in the code.
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 1;
self.bounce = -0.7;
self.update = function () {
if (!isStuck) {
self.speedY += self.gravity;
self.y += self.speedY;
if (self.y > 2732 - self.height / 2) {
self.speedY *= self.bounce;
self.y = 2732 - self.height / 2;
}
self.rotation += 0.1; // Add rotation to the ball
} else {
self.x = cursorX;
}
};
});
// ForegroundContainer class
var ForegroundContainer = Container.expand(function () {
var self = Container.call(this);
});
// Hoop class
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 1
});
});
// MidgroundContainer class
var MidgroundContainer = Container.expand(function () {
var self = Container.call(this);
});
// Panel class
var Panel = Container.expand(function () {
var self = Container.call(this);
var panelGraphics = self.attachAsset('panel', {
anchorX: 0.5,
anchorY: 0.5
});
self.direction = 1; // 1 for moving right, -1 for moving left
self.move = function () {
self.x += panelSpeed * self.direction;
if (self.x > 2048 - self.width / 2) {
self.direction = -1;
} else if (self.x < self.width / 2) {
self.direction = 1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate sky
});
/****
* Game Code
****/
game.on('up', function (obj) {
isStuck = false;
});
var ball;
var hoop;
var score = 0;
var isStuck = true;
var cursorX = 1024; // Initialize cursorX to the center of the screen
var scoreTxt;
var panel;
var panelSpeed = 10; // Initialize panel's speed
var backgroundContainer = game.addChild(new BackgroundContainer());
backgroundContainer.x = 1024; // Center horizontally
backgroundContainer.y = 1366; // Center vertically
var midgroundContainer = game.addChild(new MidgroundContainer());
var foregroundContainer = game.addChild(new ForegroundContainer());
function initGame() {
ball = foregroundContainer.addChild(new Ball());
ball.x = 1024; // Center horizontally
ball.y = 350; // Starting height
panel = midgroundContainer.addChild(new Panel());
panel.x = 1024; // Center horizontally
panel.y = 700; // Position from bottom
hoop = panel.addChild(new Hoop());
hoop.x = 0; // Center horizontally
hoop.y = 130; // Position from bottom
var visibleHoop = foregroundContainer.attachAsset('visibleHoop', {
anchorX: 0.5,
anchorY: 0.5,
x: panel.x,
y: panel.y + hoop.height + 250 // Position 100 pixels below the hoop
});
scoreTxt = panel.addChild(new Text2(score.toString(), {
size: 170,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 15
}));
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
scoreTxt.x = 0; // Position score text in the middle of the panel
scoreTxt.y = -150; // Move score text higher
game.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
cursorX = pos.x;
});
game.on('down', function (obj) {
if (isStuck) {
var event = obj.event;
var pos = event.getLocalPosition(game);
ball.x = pos.x;
}
});
LK.on('tick', function () {
if (isStuck) {
ball.x = cursorX;
}
ball.update();
panel.move();
visibleHoop.x = panel.x;
visibleHoop.y = panel.y + hoop.height + 290;
checkScore();
if (ball.y >= 2732 - ball.height / 2 && !isStuck && !ball.scoring) {
LK.showGameOver();
}
});
}
function checkScore() {
if (ball.intersects(hoop) && !ball.scoring) {
score += 1;
scoreTxt.setText(score.toString());
ball.scoring = true; // Mark the ball as having scored
ball.x = hoop.x + hoop.parent.x; // Move the ball's horizontal position to match the center of the hoop
}
// Handle ball reset if it has scored
if (ball.y >= 2732 - ball.height / 2 && ball.scoring) {
// Reset ball position for another drop
ball.x = 1024;
ball.y = 350;
ball.speedY = 0;
ball.rotation = 0; // Reset ball's rotation
isStuck = true;
ball.scoring = false; // Reset scoring status for the next drop
var panelLowerLimit = 2400; // Define the lowest point the panel can move to
if (panel.y < panelLowerLimit) {
// Check if the panel is above the bottom of the screen
panel.y += 50; // Move the panel 50 pixels lower
}
panelSpeed *= 1.05; // Increase the panel's speed by 20%
}
}
initGame();