/****
* Classes
****/
// Card class representing an individual card (used for deck creation)
var Card = Container.expand(function (suit, value) {
var self = Container.call(this);
self.suit = suit;
self.value = value;
self.isFaceDown = false;
// Attach card asset
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
// Method to flip the card
self.flip = function () {
self.isFaceDown = !self.isFaceDown;
cardGraphics.alpha = self.isFaceDown ? 0.5 : 1;
};
});
// Deck class to manage a full 52-card deck
var Deck = Container.expand(function () {
var self = Container.call(this);
self.cards = [];
var suits = ['hearts', 'diamonds', 'clubs', 'spades'];
var values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'];
suits.forEach(function (suit) {
values.forEach(function (value) {
self.cards.push(new Card(suit, value));
});
});
// Shuffle using Fisher-Yates algorithm
self.shuffle = function () {
for (var i = self.cards.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = self.cards[i];
self.cards[i] = self.cards[j];
self.cards[j] = temp;
}
};
// Draw a card from the deck
self.draw = function () {
return self.cards.pop();
};
});
// Hand class for managing a player’s or dealer’s hand
var Hand = Container.expand(function (label) {
var self = Container.call(this);
self.cards = [];
self.label = label || '';
// Create a display text for the hand's total value
self.valueText = new Text2('0', {
size: 60,
fill: 0xFFFFFF
});
self.valueText.anchor.set(0.5, 0);
LK.gui.top.addChild(self.valueText);
// Update hand value display near the hand's position
self.updateValueDisplay = function (x, y) {
self.valueText.setText(self.getValue().toString());
self.valueText.x = x;
self.valueText.y = y - 100;
};
// Add a card to the hand at a specified position
self.addCard = function (card, posX, posY) {
var playerCard = new PlayerCard(card.suit, card.value, posX, posY);
self.cards.push(playerCard);
self.addChild(playerCard);
self.updateValueDisplay(posX, posY);
};
// Compute the hand's total value, handling Aces correctly
self.getValue = function () {
var total = 0;
var aces = 0;
self.cards.forEach(function (card) {
if (typeof card.value === 'number') {
total += card.value;
} else if (card.value === 'A') {
aces++;
total += 11;
} else {
total += 10;
}
});
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
};
});
// PlayerCard class to represent each card in a hand with value display
var PlayerCard = Container.expand(function (suit, value, x, y) {
var self = Container.call(this);
self.suit = suit;
self.value = value;
self.isFaceDown = false;
// Attach card asset
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
// Create a text object to display the card's value
self.valueText = new Text2(self.value.toString(), {
size: 30,
fill: 0xFFFFFF
});
// Position the text to the right of the card (offset from center)
self.valueText.anchor.set(0, 0.5);
self.valueText.x = 60; // (card width/2 = 50 + 10px offset)
self.valueText.y = 0;
// Only show the text when the card is face up
self.valueText.visible = !self.isFaceDown;
self.addChild(self.valueText);
// Position the card
self.x = x;
self.y = y;
// Method to flip the card
self.flip = function () {
self.isFaceDown = !self.isFaceDown;
cardGraphics.alpha = self.isFaceDown ? 0.5 : 1;
self.valueText.visible = !self.isFaceDown;
};
});
/****
* Initialize Game
****/
// Create the game instance with a black background
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Initialize deck and hands
var deck = new Deck();
deck.shuffle();
// Position the dealer's hand near the top center and the player's hand near the bottom center.
var dealerHand = new Hand("Dealer");
dealerHand.x = 500; // Adjusted so cards are centered; feel free to tweak this value.
dealerHand.y = 300; // Near the top of the 2732px height.
var playerHand = new Hand("Player");
playerHand.x = 500; // Adjusted so cards are centered.
playerHand.y = 1000; // Near the bottom of the screen.
// Deal initial cards
dealerHand.addCard(deck.draw(), dealerHand.x, dealerHand.y);
dealerHand.addCard(deck.draw(), dealerHand.x + 120, dealerHand.y);
// Keep dealer's second card face down initially
dealerHand.cards[1].flip();
playerHand.addCard(deck.draw(), playerHand.x, playerHand.y);
playerHand.addCard(deck.draw(), playerHand.x + 120, playerHand.y);
game.addChild(dealerHand);
game.addChild(playerHand);
// Game state flags
var playerTurn = true;
var gameOver = false;
/****
* Game Logic
****/
// Handle player's decision: 'hit' or 'stand'
function playerDecision(action) {
if (gameOver) {
return;
}
if (action === 'hit') {
// Display a message to the player
var hitMessage = new Text2('You have decided to hit!', {
size: 60,
fill: 0xFFFFFF
});
hitMessage.anchor.set(0.5, 0.5);
hitMessage.x = 1024; // Center of the screen
hitMessage.y = 1366; // Center of the screen
LK.gui.center.addChild(hitMessage);
// Remove the message after 2 seconds
LK.setTimeout(function () {
LK.gui.center.removeChild(hitMessage);
}, 2000);
// Position new card based on current number of cards
var newX = playerHand.x + playerHand.cards.length * 120;
playerHand.addCard(deck.draw(), newX, playerHand.y);
if (playerHand.getValue() > 21) {
gameOver = true;
LK.showGameOver();
}
} else if (action === 'stand') {
playerTurn = false;
dealerPlay();
}
}
// Dealer plays automatically once the player stands
function dealerPlay() {
// Reveal dealer's face-down card
dealerHand.cards[1].flip();
// Dealer draws until reaching at least 17
while (dealerHand.getValue() < 17) {
var newX = dealerHand.x + dealerHand.cards.length * 120;
dealerHand.addCard(deck.draw(), newX, dealerHand.y);
}
// Determine outcome
var playerTotal = playerHand.getValue();
var dealerTotal = dealerHand.getValue();
if (dealerTotal > 21 || playerTotal > dealerTotal) {
LK.showYouWin();
} else if (playerTotal < dealerTotal) {
LK.showGameOver();
} else {
LK.showPush(); // Handle tie/push situation
}
}
// Create buttons for player actions 'hit' and 'stand'
// Placing the buttons near the bottom within the 2048×2732 bounds.
var hitButton = new Text2('Hit', {
size: 80,
fill: 0xFFFFFF
});
hitButton.anchor.set(0.5, 0.5);
hitButton.x = 512; // Left side of the screen.
hitButton.y = 2600; // Near the bottom.
hitButton.interactive = true;
hitButton.buttonMode = true;
hitButton.on('pointerdown', function (event) {
playerDecision('hit');
event.stopPropagation(); // Ensure event doesn't propagate further
});
game.addChild(hitButton);
var standButton = new Text2('Stand', {
size: 80,
fill: 0xFFFFFF
});
standButton.anchor.set(0.5, 0.5);
standButton.x = 1536; // Right side of the screen.
standButton.y = 2600; // Near the bottom.
standButton.interactive = true;
standButton.buttonMode = true;
standButton.on('pointerdown', function (event) {
playerDecision('stand');
event.stopPropagation(); // Ensure event doesn't propagate further
});
game.addChild(standButton);
// Game update loop (can be used for additional logic if needed)
game.update = function () {
// Additional per-frame game updates can be placed here
}; /****
* Classes
****/
// Card class representing an individual card (used for deck creation)
var Card = Container.expand(function (suit, value) {
var self = Container.call(this);
self.suit = suit;
self.value = value;
self.isFaceDown = false;
// Attach card asset
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
// Method to flip the card
self.flip = function () {
self.isFaceDown = !self.isFaceDown;
cardGraphics.alpha = self.isFaceDown ? 0.5 : 1;
};
});
// Deck class to manage a full 52-card deck
var Deck = Container.expand(function () {
var self = Container.call(this);
self.cards = [];
var suits = ['hearts', 'diamonds', 'clubs', 'spades'];
var values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'];
suits.forEach(function (suit) {
values.forEach(function (value) {
self.cards.push(new Card(suit, value));
});
});
// Shuffle using Fisher-Yates algorithm
self.shuffle = function () {
for (var i = self.cards.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = self.cards[i];
self.cards[i] = self.cards[j];
self.cards[j] = temp;
}
};
// Draw a card from the deck
self.draw = function () {
return self.cards.pop();
};
});
// Hand class for managing a player’s or dealer’s hand
var Hand = Container.expand(function (label) {
var self = Container.call(this);
self.cards = [];
self.label = label || '';
// Create a display text for the hand's total value
self.valueText = new Text2('0', {
size: 60,
fill: 0xFFFFFF
});
self.valueText.anchor.set(0.5, 0);
LK.gui.top.addChild(self.valueText);
// Update hand value display near the hand's position
self.updateValueDisplay = function (x, y) {
self.valueText.setText(self.getValue().toString());
self.valueText.x = x;
self.valueText.y = y - 100;
};
// Add a card to the hand at a specified position
self.addCard = function (card, posX, posY) {
var playerCard = new PlayerCard(card.suit, card.value, posX, posY);
self.cards.push(playerCard);
self.addChild(playerCard);
self.updateValueDisplay(posX, posY);
};
// Compute the hand's total value, handling Aces correctly
self.getValue = function () {
var total = 0;
var aces = 0;
self.cards.forEach(function (card) {
if (typeof card.value === 'number') {
total += card.value;
} else if (card.value === 'A') {
aces++;
total += 11;
} else {
total += 10;
}
});
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
};
});
// PlayerCard class to represent each card in a hand with value display
var PlayerCard = Container.expand(function (suit, value, x, y) {
var self = Container.call(this);
self.suit = suit;
self.value = value;
self.isFaceDown = false;
// Attach card asset
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
// Create a text object to display the card's value
self.valueText = new Text2(self.value.toString(), {
size: 30,
fill: 0xFFFFFF
});
// Position the text to the right of the card (offset from center)
self.valueText.anchor.set(0, 0.5);
self.valueText.x = 60; // (card width/2 = 50 + 10px offset)
self.valueText.y = 0;
// Only show the text when the card is face up
self.valueText.visible = !self.isFaceDown;
self.addChild(self.valueText);
// Position the card
self.x = x;
self.y = y;
// Method to flip the card
self.flip = function () {
self.isFaceDown = !self.isFaceDown;
cardGraphics.alpha = self.isFaceDown ? 0.5 : 1;
self.valueText.visible = !self.isFaceDown;
};
});
/****
* Initialize Game
****/
// Create the game instance with a black background
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Initialize deck and hands
var deck = new Deck();
deck.shuffle();
// Position the dealer's hand near the top center and the player's hand near the bottom center.
var dealerHand = new Hand("Dealer");
dealerHand.x = 500; // Adjusted so cards are centered; feel free to tweak this value.
dealerHand.y = 300; // Near the top of the 2732px height.
var playerHand = new Hand("Player");
playerHand.x = 500; // Adjusted so cards are centered.
playerHand.y = 1000; // Near the bottom of the screen.
// Deal initial cards
dealerHand.addCard(deck.draw(), dealerHand.x, dealerHand.y);
dealerHand.addCard(deck.draw(), dealerHand.x + 120, dealerHand.y);
// Keep dealer's second card face down initially
dealerHand.cards[1].flip();
playerHand.addCard(deck.draw(), playerHand.x, playerHand.y);
playerHand.addCard(deck.draw(), playerHand.x + 120, playerHand.y);
game.addChild(dealerHand);
game.addChild(playerHand);
// Game state flags
var playerTurn = true;
var gameOver = false;
/****
* Game Logic
****/
// Handle player's decision: 'hit' or 'stand'
function playerDecision(action) {
if (gameOver) {
return;
}
if (action === 'hit') {
// Display a message to the player
var hitMessage = new Text2('You have decided to hit!', {
size: 60,
fill: 0xFFFFFF
});
hitMessage.anchor.set(0.5, 0.5);
hitMessage.x = 1024; // Center of the screen
hitMessage.y = 1366; // Center of the screen
LK.gui.center.addChild(hitMessage);
// Remove the message after 2 seconds
LK.setTimeout(function () {
LK.gui.center.removeChild(hitMessage);
}, 2000);
// Position new card based on current number of cards
var newX = playerHand.x + playerHand.cards.length * 120;
playerHand.addCard(deck.draw(), newX, playerHand.y);
if (playerHand.getValue() > 21) {
gameOver = true;
LK.showGameOver();
}
} else if (action === 'stand') {
playerTurn = false;
dealerPlay();
}
}
// Dealer plays automatically once the player stands
function dealerPlay() {
// Reveal dealer's face-down card
dealerHand.cards[1].flip();
// Dealer draws until reaching at least 17
while (dealerHand.getValue() < 17) {
var newX = dealerHand.x + dealerHand.cards.length * 120;
dealerHand.addCard(deck.draw(), newX, dealerHand.y);
}
// Determine outcome
var playerTotal = playerHand.getValue();
var dealerTotal = dealerHand.getValue();
if (dealerTotal > 21 || playerTotal > dealerTotal) {
LK.showYouWin();
} else if (playerTotal < dealerTotal) {
LK.showGameOver();
} else {
LK.showPush(); // Handle tie/push situation
}
}
// Create buttons for player actions 'hit' and 'stand'
// Placing the buttons near the bottom within the 2048×2732 bounds.
var hitButton = new Text2('Hit', {
size: 80,
fill: 0xFFFFFF
});
hitButton.anchor.set(0.5, 0.5);
hitButton.x = 512; // Left side of the screen.
hitButton.y = 2600; // Near the bottom.
hitButton.interactive = true;
hitButton.buttonMode = true;
hitButton.on('pointerdown', function (event) {
playerDecision('hit');
event.stopPropagation(); // Ensure event doesn't propagate further
});
game.addChild(hitButton);
var standButton = new Text2('Stand', {
size: 80,
fill: 0xFFFFFF
});
standButton.anchor.set(0.5, 0.5);
standButton.x = 1536; // Right side of the screen.
standButton.y = 2600; // Near the bottom.
standButton.interactive = true;
standButton.buttonMode = true;
standButton.on('pointerdown', function (event) {
playerDecision('stand');
event.stopPropagation(); // Ensure event doesn't propagate further
});
game.addChild(standButton);
// Game update loop (can be used for additional logic if needed)
game.update = function () {
// Additional per-frame game updates can be placed here
};