/****
* Classes
****/
// Counter class to display and update the player's balance
var Counter = Container.expand(function (initialValue, posX, posY) {
var self = Container.call(this);
var text = new Text2(initialValue.toString(), {
size: 100,
fill: "#ffffff"
});
text.anchor.set(0.5, 0);
text.x = posX;
text.y = posY;
self.addChild(text);
// Method to update the displayed text
self.updateText = function (newValue) {
text.setText(newValue.toString());
};
});
// Coin class to represent the coin being flipped
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.createAsset('coin_heads', 'Coin with heads side up', 0.5, 0.5);
self.side = 'heads'; // Initial side of the coin
// Function to flip the coin with animation
self.flip = function (choice, callback) {
var flipCount = 40; // Number of flips before showing the result
var flipInterval = 25; // Interval in ms between flips
var currentFlip = 0;
var flipAnimation = function flipAnimation() {
if (currentFlip < flipCount) {
coinGraphics.texture = LK.getAsset(currentFlip % 2 === 0 ? 'coin_heads' : 'coin_tails').texture;
coinGraphics.rotation += Math.PI / 20; // Add rotation to simulate rolling
currentFlip++;
LK.setTimeout(flipAnimation, flipInterval);
} else {
coinGraphics.rotation = 0; // Reset rotation after animation
// Randomly choose heads or tails for the final result
var result = Math.random() < 0.5 ? 'heads' : 'tails';
// Update the coin graphics based on the result
coinGraphics.texture = LK.getAsset('coin_' + result).texture;
self.side = result;
if (typeof callback === 'function') {
callback(result === choice);
}
}
};
// Start the flip animation
flipAnimation();
};
});
// Player class to represent the player's state
var Player = Container.expand(function () {
var self = Container.call(this);
self.balance = 1000; // Player starts with $1000
// Function to update the player's balance
self.updateBalance = function (amount) {
self.balance += amount;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize the coin
var coin = game.addChild(new Coin());
coin.x = game.width / 2;
coin.y = game.height / 2 - 200;
// Initialize the player
var player = new Player();
// Create the balance counter using the Counter class
var balanceCounter = game.addChild(new Counter(player.balance, game.width / 2, 100));
// Create the Heads button
var headsButton = game.addChild(LK.getAsset('button_heads', 'Heads Button', 0.5, 0.5));
headsButton.x = game.width / 4;
headsButton.y = game.height - 300;
// Create the Tails button
var tailsButton = game.addChild(LK.getAsset('button_tails', 'Tails Button', 0.5, 0.5));
tailsButton.x = game.width / 4 * 3;
tailsButton.y = game.height - 300;
// Function to update the balance counter display
function updateBalanceText() {
balanceCounter.updateText(player.balance);
}
// Function to handle the flip result
function handleFlipResult(choice) {
coin.flip(choice, function (win) {
if (win) {
player.updateBalance(200); // Win $200
} else {
player.updateBalance(-200); // Lose $200
}
updateBalanceText();
if (player.balance <= 100 || player.balance >= 2000) {
var explosion = game.addChild(LK.getAsset('explosion', 'Explosion filling the screen', 0.5, 0.5));
explosion.width = game.width;
explosion.height = game.height;
explosion.x = game.width / 2;
explosion.y = game.height / 2;
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
});
}
// Event listener for the Heads button
headsButton.on('down', function () {
handleFlipResult('heads');
});
// Event listener for the Tails button
tailsButton.on('down', function () {
handleFlipResult('tails');
});
// No need for a tick function as the game does not have continuous animation or movement