/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.8;
self.bounce = 0;
self.friction = 0.995;
self.hasLanded = false;
self.update = function () {
if (self.hasLanded) return;
// Apply gravity
self.velocityY += self.gravity;
// Update position
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= self.friction;
// Check for peg collisions
for (var i = 0; i < pegs.length; i++) {
var peg = pegs[i];
var dx = self.x - peg.x;
var dy = self.y - peg.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 30) {
// Ball radius + peg radius
// Bounce off peg with more bounce
var angle = Math.atan2(dy, dx);
self.velocityX = Math.cos(angle) * 6;
self.velocityY = Math.sin(angle) * 5;
// Add some randomness
self.velocityX += (Math.random() - 0.5) * 4;
// No bounce effect
LK.getSound('peg_hit').play();
break;
}
}
// Check if ball reached slots
if (self.y > 2400 && !self.hasLanded) {
self.hasLanded = true;
self.checkSlotLanding();
}
// Remove ball if it goes off screen
if (self.y > 2800) {
self.destroy();
var index = balls.indexOf(self);
if (index > -1) {
balls.splice(index, 1);
}
}
};
self.checkSlotLanding = function () {
var slotIndex = -1;
var ballX = self.x;
// Determine which slot the ball landed in (8 slots with 225px spacing)
if (ballX >= 128 && ballX <= 353) {
slotIndex = 0; // x10 slot
} else if (ballX >= 353 && ballX <= 578) {
slotIndex = 1; // x5 slot
} else if (ballX >= 578 && ballX <= 803) {
slotIndex = 2; // x2 slot
} else if (ballX >= 803 && ballX <= 1028) {
slotIndex = 3; // x0.2 slot
} else if (ballX >= 1028 && ballX <= 1253) {
slotIndex = 4; // x0.2 slot
} else if (ballX >= 1253 && ballX <= 1478) {
slotIndex = 5; // x2 slot
} else if (ballX >= 1478 && ballX <= 1703) {
slotIndex = 6; // x5 slot
} else if (ballX >= 1703 && ballX <= 1928) {
slotIndex = 7; // x10 slot
}
if (slotIndex >= 0) {
var multiplier = slotMultipliers[slotIndex];
var winnings = Math.floor(10 * multiplier);
bankroll += winnings;
updateBankrollDisplay();
showWinnings(winnings, self.x, self.y);
LK.getSound('slot_land').play();
}
};
return self;
});
var Peg = Container.expand(function () {
var self = Container.call(this);
var pegGraphics = self.attachAsset('peg', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0a0a0a
});
/****
* Game Code
****/
var bankroll = 100;
var ballCost = 10;
var balls = [];
var pegs = [];
var slotMultipliers = [5, 2, 1, 0.5, 0.5, 1, 2, 5]; // Reduced outer multipliers, higher center probability
var canDropBall = true;
// Create board background
var board = game.addChild(LK.getAsset('board', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
}));
// Create pegs in triangular pattern
function createPegs() {
var startY = 400;
var rows = 12;
var pegSpacing = 120;
for (var row = 0; row < rows; row++) {
var pegsInRow = row + 3;
var startX = 1024 - (pegsInRow - 1) * pegSpacing / 2;
for (var col = 0; col < pegsInRow; col++) {
var peg = new Peg();
var baseX = startX + col * pegSpacing;
// Create funnel effect - push outer pegs slightly inward toward center
var centerX = 1024;
var distanceFromCenter = Math.abs(baseX - centerX);
var funnelOffset = distanceFromCenter * 0.15; // Adjust strength of funnel
if (baseX < centerX) {
peg.x = baseX + funnelOffset; // Move left pegs right
} else {
peg.x = baseX - funnelOffset; // Move right pegs left
}
peg.y = startY + row * 150;
pegs.push(peg);
game.addChild(peg);
}
}
}
createPegs();
// Create slots at bottom
var slots = [];
var slotLabels = [];
var slotColors = [0xff0000, 0xffa500, 0xffcc00, 0xffff00, 0xffff00, 0xffcc00, 0xffa500, 0xff0000];
for (var i = 0; i < 8; i++) {
var slot = game.addChild(LK.getAsset('slot', {
anchorX: 0.5,
anchorY: 0.5,
x: 240 + i * 225,
y: 2450
}));
slot.tint = slotColors[i];
slots.push(slot);
// Create slot labels
var multiplierText = 'x' + slotMultipliers[i];
var label = new Text2(multiplierText, {
size: 60,
fill: 0xFFFFFF
});
label.anchor.set(0.5, 0.5);
label.x = 240 + i * 225;
label.y = 2450;
game.addChild(label);
slotLabels.push(label);
}
// Create bankroll display
var bankrollText = new Text2('$' + bankroll, {
size: 100,
fill: 0x00FF00
});
bankrollText.anchor.set(0.5, 0);
bankrollText.x = 0; // Move slightly to the right
LK.gui.top.addChild(bankrollText);
// Create instructions
var instructionText = new Text2('Tap to drop $10 ball', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 0; // Move slightly to the right
instructionText.y = 120;
LK.gui.top.addChild(instructionText);
function updateBankrollDisplay() {
bankrollText.setText('$' + bankroll);
if (bankroll < 0) {
bankrollText.tint = 0xff0000;
instructionText.setText('Game Over - Bankrupt!');
canDropBall = false;
// Show game over immediately when bankrupt
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
} else if (bankroll < ballCost) {
bankrollText.tint = 0xff0000;
instructionText.setText('Game Over - Not enough money!');
canDropBall = false;
// Show game over after a delay
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
}
function showWinnings(amount, x, y) {
var winText = new Text2((amount >= 10 ? '+$' : '') + (amount - 10), {
size: 80,
fill: amount >= 10 ? "#00ff00" : "#ff0000"
});
winText.anchor.set(0.5, 0.5);
winText.x = x;
winText.y = y;
game.addChild(winText);
// Animate winnings text
tween(winText, {
y: y - 100,
alpha: 0
}, {
duration: 2000,
onFinish: function onFinish() {
winText.destroy();
}
});
}
// Game input handling
game.down = function (x, y, obj) {
if (!canDropBall || bankroll < ballCost) return;
// Deduct cost immediately
bankroll -= ballCost;
updateBankrollDisplay();
// Create and drop ball
var ball = new Ball();
ball.x = 1024; // Start from center of screen
ball.y = 200;
ball.velocityX = (Math.random() - 0.5) * 2; // Small random horizontal velocity
ball.velocityY = 1;
balls.push(ball);
game.addChild(ball);
LK.getSound('drop').play();
// Prevent rapid dropping
canDropBall = false;
LK.setTimeout(function () {
canDropBall = true;
}, 300);
};
game.update = function () {
// Update all balls
for (var i = balls.length - 1; i >= 0; i--) {
var ball = balls[i];
if (ball.destroyed) {
balls.splice(i, 1);
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.8;
self.bounce = 0;
self.friction = 0.995;
self.hasLanded = false;
self.update = function () {
if (self.hasLanded) return;
// Apply gravity
self.velocityY += self.gravity;
// Update position
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= self.friction;
// Check for peg collisions
for (var i = 0; i < pegs.length; i++) {
var peg = pegs[i];
var dx = self.x - peg.x;
var dy = self.y - peg.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 30) {
// Ball radius + peg radius
// Bounce off peg with more bounce
var angle = Math.atan2(dy, dx);
self.velocityX = Math.cos(angle) * 6;
self.velocityY = Math.sin(angle) * 5;
// Add some randomness
self.velocityX += (Math.random() - 0.5) * 4;
// No bounce effect
LK.getSound('peg_hit').play();
break;
}
}
// Check if ball reached slots
if (self.y > 2400 && !self.hasLanded) {
self.hasLanded = true;
self.checkSlotLanding();
}
// Remove ball if it goes off screen
if (self.y > 2800) {
self.destroy();
var index = balls.indexOf(self);
if (index > -1) {
balls.splice(index, 1);
}
}
};
self.checkSlotLanding = function () {
var slotIndex = -1;
var ballX = self.x;
// Determine which slot the ball landed in (8 slots with 225px spacing)
if (ballX >= 128 && ballX <= 353) {
slotIndex = 0; // x10 slot
} else if (ballX >= 353 && ballX <= 578) {
slotIndex = 1; // x5 slot
} else if (ballX >= 578 && ballX <= 803) {
slotIndex = 2; // x2 slot
} else if (ballX >= 803 && ballX <= 1028) {
slotIndex = 3; // x0.2 slot
} else if (ballX >= 1028 && ballX <= 1253) {
slotIndex = 4; // x0.2 slot
} else if (ballX >= 1253 && ballX <= 1478) {
slotIndex = 5; // x2 slot
} else if (ballX >= 1478 && ballX <= 1703) {
slotIndex = 6; // x5 slot
} else if (ballX >= 1703 && ballX <= 1928) {
slotIndex = 7; // x10 slot
}
if (slotIndex >= 0) {
var multiplier = slotMultipliers[slotIndex];
var winnings = Math.floor(10 * multiplier);
bankroll += winnings;
updateBankrollDisplay();
showWinnings(winnings, self.x, self.y);
LK.getSound('slot_land').play();
}
};
return self;
});
var Peg = Container.expand(function () {
var self = Container.call(this);
var pegGraphics = self.attachAsset('peg', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0a0a0a
});
/****
* Game Code
****/
var bankroll = 100;
var ballCost = 10;
var balls = [];
var pegs = [];
var slotMultipliers = [5, 2, 1, 0.5, 0.5, 1, 2, 5]; // Reduced outer multipliers, higher center probability
var canDropBall = true;
// Create board background
var board = game.addChild(LK.getAsset('board', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
}));
// Create pegs in triangular pattern
function createPegs() {
var startY = 400;
var rows = 12;
var pegSpacing = 120;
for (var row = 0; row < rows; row++) {
var pegsInRow = row + 3;
var startX = 1024 - (pegsInRow - 1) * pegSpacing / 2;
for (var col = 0; col < pegsInRow; col++) {
var peg = new Peg();
var baseX = startX + col * pegSpacing;
// Create funnel effect - push outer pegs slightly inward toward center
var centerX = 1024;
var distanceFromCenter = Math.abs(baseX - centerX);
var funnelOffset = distanceFromCenter * 0.15; // Adjust strength of funnel
if (baseX < centerX) {
peg.x = baseX + funnelOffset; // Move left pegs right
} else {
peg.x = baseX - funnelOffset; // Move right pegs left
}
peg.y = startY + row * 150;
pegs.push(peg);
game.addChild(peg);
}
}
}
createPegs();
// Create slots at bottom
var slots = [];
var slotLabels = [];
var slotColors = [0xff0000, 0xffa500, 0xffcc00, 0xffff00, 0xffff00, 0xffcc00, 0xffa500, 0xff0000];
for (var i = 0; i < 8; i++) {
var slot = game.addChild(LK.getAsset('slot', {
anchorX: 0.5,
anchorY: 0.5,
x: 240 + i * 225,
y: 2450
}));
slot.tint = slotColors[i];
slots.push(slot);
// Create slot labels
var multiplierText = 'x' + slotMultipliers[i];
var label = new Text2(multiplierText, {
size: 60,
fill: 0xFFFFFF
});
label.anchor.set(0.5, 0.5);
label.x = 240 + i * 225;
label.y = 2450;
game.addChild(label);
slotLabels.push(label);
}
// Create bankroll display
var bankrollText = new Text2('$' + bankroll, {
size: 100,
fill: 0x00FF00
});
bankrollText.anchor.set(0.5, 0);
bankrollText.x = 0; // Move slightly to the right
LK.gui.top.addChild(bankrollText);
// Create instructions
var instructionText = new Text2('Tap to drop $10 ball', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 0; // Move slightly to the right
instructionText.y = 120;
LK.gui.top.addChild(instructionText);
function updateBankrollDisplay() {
bankrollText.setText('$' + bankroll);
if (bankroll < 0) {
bankrollText.tint = 0xff0000;
instructionText.setText('Game Over - Bankrupt!');
canDropBall = false;
// Show game over immediately when bankrupt
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
} else if (bankroll < ballCost) {
bankrollText.tint = 0xff0000;
instructionText.setText('Game Over - Not enough money!');
canDropBall = false;
// Show game over after a delay
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
}
function showWinnings(amount, x, y) {
var winText = new Text2((amount >= 10 ? '+$' : '') + (amount - 10), {
size: 80,
fill: amount >= 10 ? "#00ff00" : "#ff0000"
});
winText.anchor.set(0.5, 0.5);
winText.x = x;
winText.y = y;
game.addChild(winText);
// Animate winnings text
tween(winText, {
y: y - 100,
alpha: 0
}, {
duration: 2000,
onFinish: function onFinish() {
winText.destroy();
}
});
}
// Game input handling
game.down = function (x, y, obj) {
if (!canDropBall || bankroll < ballCost) return;
// Deduct cost immediately
bankroll -= ballCost;
updateBankrollDisplay();
// Create and drop ball
var ball = new Ball();
ball.x = 1024; // Start from center of screen
ball.y = 200;
ball.velocityX = (Math.random() - 0.5) * 2; // Small random horizontal velocity
ball.velocityY = 1;
balls.push(ball);
game.addChild(ball);
LK.getSound('drop').play();
// Prevent rapid dropping
canDropBall = false;
LK.setTimeout(function () {
canDropBall = true;
}, 300);
};
game.update = function () {
// Update all balls
for (var i = balls.length - 1; i >= 0; i--) {
var ball = balls[i];
if (ball.destroyed) {
balls.splice(i, 1);
}
}
};