/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Ball class to represent each ball on the pool table var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocity = { x: 0, y: 0 }; self.update = function () { self.x += self.velocity.x; self.y += self.velocity.y; // Apply friction to slow down the ball self.velocity.x *= 0.98; self.velocity.y *= 0.98; }; }); // Cue class to represent the pool cue var Cue = Container.expand(function () { var self = Container.call(this); var cueGraphics = self.attachAsset('cue', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); self.aim = function (x, y) { var dx = x - self.x; var dy = y - self.y; self.rotation = Math.atan2(dy, dx); }; self.shoot = function (power) { // Calculate velocity based on power and direction var velocity = { x: Math.cos(self.rotation) * power, y: Math.sin(self.rotation) * power }; // Find the ball closest to the front of the cue var closestBall = null; var minDistance = Infinity; balls.forEach(function (ball) { var distance = Math.sqrt(Math.pow(ball.x - self.x, 2) + Math.pow(ball.y - self.y, 2)); if (distance < minDistance) { minDistance = distance; closestBall = ball; } }); // Apply velocity to the closest ball if (closestBall) { closestBall.velocity = { x: velocity.x, y: velocity.y }; return closestBall.velocity; } return { x: 0, y: 0 }; // Return zero velocity if no ball is found }; }); // Hole class to represent each hole on the pool table var Hole = Container.expand(function () { var self = Container.call(this); var holeGraphics = self.attachAsset('hole', { anchorX: 0.5, anchorY: 0.5 }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x228B22 // Set background color to pool table green }); /**** * Game Code ****/ // Initialize and position holes var holes = []; var holePositions = [{ x: 0, y: 0 }, { x: 1024, y: 0 }, { x: 2048, y: 0 }, { x: 0, y: 2732 }, { x: 1024, y: 2732 }, { x: 2048, y: 2732 }]; holePositions.forEach(function (position) { var hole = new Hole(); hole.x = position.x; hole.y = position.y; holes.push(hole); game.addChild(hole); }); // Initialize balls and cue var balls = []; var cue = new Cue(); game.addChild(cue); // Create and position balls for (var i = 0; i < 8; i++) { var ball = new Ball(); ball.x = 1024 + i % 5 * 50; // Position balls in a triangle formation ball.y = 1366 + Math.floor(i / 5) * 50; balls.push(ball); game.addChild(ball); } // Position cue cue.x = 1024; cue.y = 2000; // Handle aiming and shooting var isAiming = false; var power = 50; // Set initial power value for shooting var currentPlayer = 1; // 1 for player, 2 for computer var computerTurnDelay = 1000; // Delay for computer's turn // Add event listeners for aiming game.down = function (x, y, obj) { isAiming = true; cue.aim(x, y); }; game.move = function (x, y, obj) { if (isAiming) { cue.aim(x, y); } }; game.up = function (x, y, obj) { if (isAiming) { isAiming = false; cue.shoot(power); currentPlayer = 2; // Switch to computer's turn } }; function computerTurn() { if (currentPlayer === 2) { // Simple AI for computer's turn var targetBall = balls[Math.floor(Math.random() * balls.length)]; cue.aim(targetBall.x, targetBall.y); cue.shoot(50); // Fixed power for simplicity currentPlayer = 1; // Switch back to player's turn } } // Update game state game.update = function () { balls.forEach(function (ball) { ball.update(); // Check for collisions with table edges if (ball.x < 0 || ball.x > 2048) { ball.velocity.x *= -1; } if (ball.y < 0 || ball.y > 2732) { ball.velocity.y *= -1; } // Check for collisions with holes holes.forEach(function (hole) { if (ball.intersects(hole)) { // Ball falls into the hole ball.destroy(); balls.splice(balls.indexOf(ball), 1); // Update score LK.setScore(LK.getScore() + 1); } }); }); // Check if all balls have been pocketed if (balls.length === 0) { // Display game end poster LK.showGameOver(); return; // Exit update loop } // Check if all balls have stopped moving to switch turns if (balls.every(function (ball) { return ball.velocity.x === 0 && ball.velocity.y === 0; })) { if (currentPlayer === 2) { setTimeout(computerTurn, computerTurnDelay); // Delay for computer's turn } } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Ball class to represent each ball on the pool table
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = {
x: 0,
y: 0
};
self.update = function () {
self.x += self.velocity.x;
self.y += self.velocity.y;
// Apply friction to slow down the ball
self.velocity.x *= 0.98;
self.velocity.y *= 0.98;
};
});
// Cue class to represent the pool cue
var Cue = Container.expand(function () {
var self = Container.call(this);
var cueGraphics = self.attachAsset('cue', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
self.aim = function (x, y) {
var dx = x - self.x;
var dy = y - self.y;
self.rotation = Math.atan2(dy, dx);
};
self.shoot = function (power) {
// Calculate velocity based on power and direction
var velocity = {
x: Math.cos(self.rotation) * power,
y: Math.sin(self.rotation) * power
};
// Find the ball closest to the front of the cue
var closestBall = null;
var minDistance = Infinity;
balls.forEach(function (ball) {
var distance = Math.sqrt(Math.pow(ball.x - self.x, 2) + Math.pow(ball.y - self.y, 2));
if (distance < minDistance) {
minDistance = distance;
closestBall = ball;
}
});
// Apply velocity to the closest ball
if (closestBall) {
closestBall.velocity = {
x: velocity.x,
y: velocity.y
};
return closestBall.velocity;
}
return {
x: 0,
y: 0
}; // Return zero velocity if no ball is found
};
});
// Hole class to represent each hole on the pool table
var Hole = Container.expand(function () {
var self = Container.call(this);
var holeGraphics = self.attachAsset('hole', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x228B22 // Set background color to pool table green
});
/****
* Game Code
****/
// Initialize and position holes
var holes = [];
var holePositions = [{
x: 0,
y: 0
}, {
x: 1024,
y: 0
}, {
x: 2048,
y: 0
}, {
x: 0,
y: 2732
}, {
x: 1024,
y: 2732
}, {
x: 2048,
y: 2732
}];
holePositions.forEach(function (position) {
var hole = new Hole();
hole.x = position.x;
hole.y = position.y;
holes.push(hole);
game.addChild(hole);
});
// Initialize balls and cue
var balls = [];
var cue = new Cue();
game.addChild(cue);
// Create and position balls
for (var i = 0; i < 8; i++) {
var ball = new Ball();
ball.x = 1024 + i % 5 * 50; // Position balls in a triangle formation
ball.y = 1366 + Math.floor(i / 5) * 50;
balls.push(ball);
game.addChild(ball);
}
// Position cue
cue.x = 1024;
cue.y = 2000;
// Handle aiming and shooting
var isAiming = false;
var power = 50; // Set initial power value for shooting
var currentPlayer = 1; // 1 for player, 2 for computer
var computerTurnDelay = 1000; // Delay for computer's turn
// Add event listeners for aiming
game.down = function (x, y, obj) {
isAiming = true;
cue.aim(x, y);
};
game.move = function (x, y, obj) {
if (isAiming) {
cue.aim(x, y);
}
};
game.up = function (x, y, obj) {
if (isAiming) {
isAiming = false;
cue.shoot(power);
currentPlayer = 2; // Switch to computer's turn
}
};
function computerTurn() {
if (currentPlayer === 2) {
// Simple AI for computer's turn
var targetBall = balls[Math.floor(Math.random() * balls.length)];
cue.aim(targetBall.x, targetBall.y);
cue.shoot(50); // Fixed power for simplicity
currentPlayer = 1; // Switch back to player's turn
}
}
// Update game state
game.update = function () {
balls.forEach(function (ball) {
ball.update();
// Check for collisions with table edges
if (ball.x < 0 || ball.x > 2048) {
ball.velocity.x *= -1;
}
if (ball.y < 0 || ball.y > 2732) {
ball.velocity.y *= -1;
}
// Check for collisions with holes
holes.forEach(function (hole) {
if (ball.intersects(hole)) {
// Ball falls into the hole
ball.destroy();
balls.splice(balls.indexOf(ball), 1);
// Update score
LK.setScore(LK.getScore() + 1);
}
});
});
// Check if all balls have been pocketed
if (balls.length === 0) {
// Display game end poster
LK.showGameOver();
return; // Exit update loop
}
// Check if all balls have stopped moving to switch turns
if (balls.every(function (ball) {
return ball.velocity.x === 0 && ball.velocity.y === 0;
})) {
if (currentPlayer === 2) {
setTimeout(computerTurn, computerTurnDelay); // Delay for computer's turn
}
}
};