User prompt
make the coinflip animation smoother
User prompt
make it so when you either hit 2000$ or 100$, the game ends and a picture of an explosion filling the entire screen appears
User prompt
make it so you win or lose 200$ instead of 100$
User prompt
show the results of the coinflip when the animation is done
User prompt
make a rolling animation for the coin when clicking the 'button_heads' or 'button_tails'
User prompt
make a flipping animation for coin, when you click either of the buttons
User prompt
make it so if you hit 2000$ or 100$, the game ends
User prompt
make a counter for the money
User prompt
Fix Bug: 'TypeError: coinGraphics.setTexture is not a function' in this line: 'coinGraphics.setTexture(LK.getAsset('coin_' + result, 'Coin with ' + result + ' side up', 0.5, 0.5));' Line Number: 14
Initial prompt
Coinflip
/****
* 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 = 20; // Number of flips before showing the result
var flipInterval = 50; // 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 / 10; // 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) {
if (player.balance <= 100 || player.balance >= 2000) {
LK.showGameOver();
return;
}
coin.flip(choice, function (win) {
if (win) {
player.updateBalance(100); // Win $100
} else {
player.updateBalance(-100); // Lose $100
}
updateBalanceText();
});
}
// 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 ===================================================================
--- original.js
+++ change.js
@@ -94,15 +94,16 @@
if (player.balance <= 100 || player.balance >= 2000) {
LK.showGameOver();
return;
}
- var win = coin.flip(choice);
- if (win) {
- player.updateBalance(100); // Win $100
- } else {
- player.updateBalance(-100); // Lose $100
- }
- updateBalanceText();
+ coin.flip(choice, function (win) {
+ if (win) {
+ player.updateBalance(100); // Win $100
+ } else {
+ player.updateBalance(-100); // Lose $100
+ }
+ updateBalanceText();
+ });
}
// Event listener for the Heads button
headsButton.on('down', function () {
handleFlipResult('heads');