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 self.flip = function (choice) { // Randomly choose heads or tails 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; return result === choice; }; }); // 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 <= 0) { LK.showGameOver(); return; } var win = coin.flip(choice); 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
@@ -1,7 +1,23 @@
/****
* 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);
@@ -41,28 +57,21 @@
coin.x = game.width / 2;
coin.y = game.height / 2 - 200;
// Initialize the player
var player = new Player();
-// Create the balance text
-var balanceTxt = new Text2(player.balance.toString(), {
- size: 100,
- fill: "#ffffff"
-});
-balanceTxt.anchor.set(0.5, 0);
-balanceTxt.x = game.width / 2;
-balanceTxt.y = 100;
-LK.gui.top.addChild(balanceTxt);
+// 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 text
+// Function to update the balance counter display
function updateBalanceText() {
- balanceTxt.setText(player.balance.toString());
+ balanceCounter.updateText(player.balance);
}
// Function to handle the flip result
function handleFlipResult(choice) {
if (player.balance <= 0) {