/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Button = Container.expand(function (text, color) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset(color === 0x228B22 ? 'hitButton' : color === 0xDC143C ? 'standButton' : 'newGameButton', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(text, {
size: 36,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.enabled = true;
self.setEnabled = function (enabled) {
self.enabled = enabled;
buttonGraphics.alpha = enabled ? 1.0 : 0.5;
buttonText.alpha = enabled ? 1.0 : 0.5;
};
self.down = function (x, y, obj) {
if (!self.enabled) return;
if (text === 'HIT') {
playerHit();
} else if (text === 'STAND') {
playerStand();
} else if (text === 'Next Round') {
startNewGame();
} else if (text === '$200') {
selectBet(200);
} else if (text === '$500') {
selectBet(500);
} else if (text === '$1000') {
selectBet(1000);
}
};
return self;
});
var Card = Container.expand(function (suit, value) {
var self = Container.call(this);
self.suit = suit;
self.value = value;
self.faceUp = false;
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
var cardBack = self.attachAsset('cardBack', {
anchorX: 0.5,
anchorY: 0.5
});
// Create card text
var cardText = new Text2('', {
size: 40,
fill: 0x000000
});
cardText.anchor.set(0.5, 0.5);
self.addChild(cardText);
self.flip = function () {
self.faceUp = true;
cardBack.visible = false;
cardGraphics.visible = true;
cardText.visible = true;
var displayValue = self.value;
if (displayValue === 1) displayValue = 'A';else if (displayValue === 11) displayValue = 'J';else if (displayValue === 12) displayValue = 'Q';else if (displayValue === 13) displayValue = 'K';
var suitSymbol = '';
if (self.suit === 'hearts') suitSymbol = '♥';else if (self.suit === 'diamonds') suitSymbol = '♦';else if (self.suit === 'clubs') suitSymbol = '♣';else if (self.suit === 'spades') suitSymbol = '♠';
cardText.setText(displayValue + '\n' + suitSymbol);
if (self.suit === 'hearts' || self.suit === 'diamonds') {
cardText.fill = 0xff0000;
}
LK.getSound('cardFlip').play();
};
self.getValue = function () {
if (self.value >= 10) return 10;
return self.value;
};
// Initialize card face down
cardBack.visible = true;
cardGraphics.visible = false;
cardText.visible = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0F5132
});
/****
* Game Code
****/
// Add background image
var backgroundImage = game.addChild(LK.getAsset('background', {
anchorX: 0.0,
anchorY: 0.0
}));
backgroundImage.x = 0;
backgroundImage.y = 0;
var suits = ['hearts', 'diamonds', 'clubs', 'spades'];
var deck = [];
var playerCards = [];
var dealerCards = [];
var npc1Cards = [];
var npc2Cards = [];
var gameState = 'betting'; // 'betting', 'playing', 'dealerTurn', 'gameOver'
var playerScore = 0;
var dealerScore = 0;
var npc1Score = 0;
var npc2Score = 0;
var wins = 0;
var losses = 0;
var playerMoney = 10000;
var npc1Money = 10000;
var npc2Money = 10000;
var dealerMoney = 30000;
var currentBet = 0;
var playerBet = 0;
var npc1Bet = 0;
var npc2Bet = 0;
// UI Elements
var playerScoreText = new Text2('Player: 0', {
size: 48,
fill: 0xFFFFFF
});
playerScoreText.anchor.set(0.5, 0);
playerScoreText.x = 1024;
playerScoreText.y = 1800;
game.addChild(playerScoreText);
var dealerScoreText = new Text2('Dealer: ?', {
size: 48,
fill: 0xFFFFFF
});
dealerScoreText.anchor.set(0.5, 0);
dealerScoreText.x = 1024;
dealerScoreText.y = 500;
game.addChild(dealerScoreText);
var gameResultText = new Text2('', {
size: 60,
fill: 0xFFFF00
});
gameResultText.anchor.set(0.5, 0.5);
gameResultText.x = 1024;
gameResultText.y = 1366;
game.addChild(gameResultText);
var bettingText = new Text2('Choose Bet:', {
size: 48,
fill: 0xFFFFFF
});
bettingText.anchor.set(0.5, 0.5);
bettingText.x = 1024;
bettingText.y = 1200;
game.addChild(bettingText);
var currentBetText = new Text2('', {
size: 36,
fill: 0x00FF00
});
currentBetText.anchor.set(0.5, 0.5);
currentBetText.x = 1024;
currentBetText.y = 1300;
game.addChild(currentBetText);
var winsText = new Text2('Wins: 0', {
size: 36,
fill: 0xFFFFFF
});
winsText.anchor.set(0, 0);
winsText.x = 150;
winsText.y = 150;
game.addChild(winsText);
var lossesText = new Text2('Losses: 0', {
size: 36,
fill: 0xFFFFFF
});
lossesText.anchor.set(0, 0);
lossesText.x = 150;
lossesText.y = 200;
game.addChild(lossesText);
var npc1ScoreText = new Text2('Celil: 0', {
size: 36,
fill: 0xFFFFFF
});
npc1ScoreText.anchor.set(0.5, 0);
npc1ScoreText.x = 300;
npc1ScoreText.y = 1000;
game.addChild(npc1ScoreText);
var npc2ScoreText = new Text2('Sampuan: 0', {
size: 36,
fill: 0xFFFFFF
});
npc2ScoreText.anchor.set(0.5, 0);
npc2ScoreText.x = 1700;
npc2ScoreText.y = 1000;
game.addChild(npc2ScoreText);
var playerMoneyText = new Text2('$10000', {
size: 32,
fill: 0x00FF00
});
playerMoneyText.anchor.set(0.5, 0);
playerMoneyText.x = 1024;
playerMoneyText.y = 2300;
game.addChild(playerMoneyText);
var npc1MoneyText = new Text2('$10000', {
size: 32,
fill: 0x00FF00
});
npc1MoneyText.anchor.set(0.5, 0);
npc1MoneyText.x = 300;
npc1MoneyText.y = 1400;
game.addChild(npc1MoneyText);
var npc2MoneyText = new Text2('$10000', {
size: 32,
fill: 0x00FF00
});
npc2MoneyText.anchor.set(0.5, 0);
npc2MoneyText.x = 1700;
npc2MoneyText.y = 1400;
game.addChild(npc2MoneyText);
var dealerMoneyText = new Text2('$30000', {
size: 32,
fill: 0x00FF00
});
dealerMoneyText.anchor.set(0.5, 0);
dealerMoneyText.x = 1024;
dealerMoneyText.y = 400;
game.addChild(dealerMoneyText);
// Card deck in top right corner
var deckVisual = game.addChild(LK.getAsset('deck', {
anchorX: 1.0,
anchorY: 0.0
}));
deckVisual.x = 1948; // 100px from right edge (2048 - 100)
deckVisual.y = 100; // 100px from top
// Deck count text
var deckCountText = new Text2('52', {
size: 24,
fill: 0xFFFFFF
});
deckCountText.anchor.set(0.5, 0.5);
deckCountText.x = 1948 - 90; // Center on deck (adjusted for new width)
deckCountText.y = 225; // Center on deck (adjusted for new height)
game.addChild(deckCountText);
// Buttons
var hitButton = game.addChild(new Button('HIT', 0x228B22));
hitButton.x = 800;
hitButton.y = 2200;
var standButton = game.addChild(new Button('STAND', 0xDC143C));
standButton.x = 1200;
standButton.y = 2200;
var newGameButton = game.addChild(new Button('Next Round', 0x4169E1));
newGameButton.x = 1024;
newGameButton.y = 2400;
// Betting buttons
var bet200Button = game.addChild(new Button('$200', 0x8B4513));
bet200Button.x = 600;
bet200Button.y = 1400;
var bet500Button = game.addChild(new Button('$500', 0x8B4513));
bet500Button.x = 1024;
bet500Button.y = 1400;
var bet1000Button = game.addChild(new Button('$1000', 0x8B4513));
bet1000Button.x = 1448;
bet1000Button.y = 1400;
function selectBet(amount) {
if (gameState !== 'betting') return;
if (playerMoney < amount) return;
playerBet = amount;
currentBet = amount;
currentBetText.setText('Bahis: $' + amount);
// NPC betting logic
var betOptions = [200, 500, 1000];
npc1Bet = betOptions[Math.floor(Math.random() * betOptions.length)];
npc2Bet = betOptions[Math.floor(Math.random() * betOptions.length)];
// Ensure NPCs have enough money
if (npc1Money < npc1Bet) npc1Bet = 200;
if (npc2Money < npc2Bet) npc2Bet = 200;
// Hide betting UI and start game
setBettingUIVisible(false);
startGameRound();
}
function setBettingUIVisible(visible) {
bettingText.visible = visible;
currentBetText.visible = visible;
bet200Button.visible = visible;
bet500Button.visible = visible;
bet1000Button.visible = visible;
bet200Button.setEnabled(visible);
bet500Button.setEnabled(visible);
bet1000Button.setEnabled(visible);
}
function createDeck() {
deck = [];
for (var suitIndex = 0; suitIndex < suits.length; suitIndex++) {
var suit = suits[suitIndex];
for (var value = 1; value <= 13; value++) {
deck.push({
suit: suit,
value: value
});
}
}
// Shuffle deck
for (var i = deck.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
// Update deck count display
deckCountText.setText(deck.length.toString());
}
function dealCard(target) {
if (deck.length === 0) createDeck();
var cardData = deck.pop();
var card = new Card(cardData.suit, cardData.value);
game.addChild(card);
// Update deck count display
deckCountText.setText(deck.length.toString());
if (target === 'player') {
playerCards.push(card);
card.x = 400 + (playerCards.length - 1) * 200;
card.y = 2000;
card.flip();
} else if (target === 'npc1') {
npc1Cards.push(card);
card.x = 200 + (npc1Cards.length - 1) * 120;
card.y = 1200;
card.flip();
} else if (target === 'npc2') {
npc2Cards.push(card);
card.x = 1600 + (npc2Cards.length - 1) * 120;
card.y = 1200;
card.flip();
} else {
dealerCards.push(card);
card.x = 400 + (dealerCards.length - 1) * 200;
card.y = 800;
// Only flip first dealer card
if (dealerCards.length === 1) {
card.flip();
}
}
return card;
}
function calculateScore(cards, showAll) {
var score = 0;
var aces = 0;
for (var i = 0; i < cards.length; i++) {
var card = cards[i];
if (!showAll && i > 0 && cards === dealerCards && !card.faceUp) {
continue; // Skip face-down dealer cards
}
var value = card.getValue();
if (card.value === 1) {
aces++;
score += 11;
} else {
score += value;
}
}
// Adjust for aces
while (score > 21 && aces > 0) {
score -= 10;
aces--;
}
return score;
}
function updateScores() {
playerScore = calculateScore(playerCards, true);
dealerScore = calculateScore(dealerCards, gameState === 'dealerTurn' || gameState === 'gameOver');
npc1Score = calculateScore(npc1Cards, true);
npc2Score = calculateScore(npc2Cards, true);
playerScoreText.setText('Player: ' + playerScore);
npc1ScoreText.setText('Celil: ' + npc1Score);
npc2ScoreText.setText('Sampuan: ' + npc2Score);
if (gameState === 'dealerTurn' || gameState === 'gameOver') {
dealerScoreText.setText('Dealer: ' + dealerScore);
} else {
dealerScoreText.setText('Dealer: ?');
}
playerMoneyText.setText('$' + playerMoney);
npc1MoneyText.setText('$' + npc1Money);
npc2MoneyText.setText('$' + npc2Money);
dealerMoneyText.setText('$' + dealerMoney);
}
function npcDecision(npcCards, npcTarget) {
var score = calculateScore(npcCards, true);
// Basic AI: hit if score is 16 or less, stand if 17 or more
if (score < 17) {
dealCard(npcTarget);
updateScores();
} else {
// NPC stands
return 'stand';
}
return 'hit';
}
function processNPCTurns() {
// NPC 1 turn
if (npc1Score < 21) {
npcDecision(npc1Cards, 'npc1');
}
// NPC 2 turn
if (npc2Score < 21) {
npcDecision(npc2Cards, 'npc2');
}
}
function playerHit() {
if (gameState !== 'playing') return;
dealCard('player');
updateScores();
// Process NPC turns after player action
processNPCTurns();
updateScores();
if (playerScore > 21) {
// Player busts
gameState = 'gameOver';
gameResultText.setText('BUST! You lose!');
losses++;
lossesText.setText('Losses: ' + losses);
// Player loses bet amount when busting
playerMoney -= playerBet;
dealerMoney += playerBet;
hitButton.setEnabled(false);
standButton.setEnabled(false);
LK.getSound('lose').play();
// Reveal dealer's hidden card
if (dealerCards.length > 1 && !dealerCards[1].faceUp) {
dealerCards[1].flip();
}
updateScores();
}
}
function playerStand() {
if (gameState !== 'playing') return;
gameState = 'dealerTurn';
hitButton.setEnabled(false);
standButton.setEnabled(false);
// Process final NPC turns
processNPCTurns();
updateScores();
// Reveal dealer's hidden card
if (dealerCards.length > 1 && !dealerCards[1].faceUp) {
dealerCards[1].flip();
}
dealerPlay();
}
function dealerPlay() {
updateScores();
if (dealerScore < 17) {
// Dealer hits
LK.setTimeout(function () {
dealCard(false);
dealerCards[dealerCards.length - 1].flip();
dealerPlay();
}, 1000);
} else {
// Dealer stands
endGame();
}
}
function endGame() {
gameState = 'gameOver';
updateScores();
var playerBlackjack = playerCards.length === 2 && playerScore === 21;
var dealerBlackjack = dealerCards.length === 2 && dealerScore === 21;
// Determine winners and update money - if we lose, subtract bet amount, if we win, add bet amount
if (playerScore > 21) {
gameResultText.setText('BUST! You lose!');
losses++;
playerMoney -= playerBet; // Lose bet amount when busting
dealerMoney += playerBet;
LK.getSound('lose').play();
} else if (dealerScore > 21) {
gameResultText.setText('Dealer busts! You win!');
wins++;
playerMoney += playerBet; // Win bet amount when dealer busts
dealerMoney -= playerBet;
LK.getSound('win').play();
} else if (playerBlackjack && !dealerBlackjack) {
gameResultText.setText('BLACKJACK! You win!');
wins++;
var blackjackWin = Math.floor(playerBet * 1.5);
playerMoney += blackjackWin; // Win 1.5x bet amount for blackjack
dealerMoney -= blackjackWin;
LK.getSound('win').play();
} else if (dealerBlackjack && !playerBlackjack) {
gameResultText.setText('Dealer blackjack! You lose!');
losses++;
playerMoney -= playerBet; // Lose bet amount when dealer has blackjack
dealerMoney += playerBet;
LK.getSound('lose').play();
} else if (playerScore > dealerScore) {
gameResultText.setText('You win!');
wins++;
playerMoney += playerBet; // Win bet amount when score is higher
dealerMoney -= playerBet;
LK.getSound('win').play();
} else if (dealerScore > playerScore) {
gameResultText.setText('You lose!');
losses++;
playerMoney -= playerBet; // Lose bet amount when score is lower
dealerMoney += playerBet;
LK.getSound('lose').play();
} else {
gameResultText.setText('Push! It\'s a tie!');
// No money change on tie
}
// Update NPC money based on their performance - if they lose, subtract bet, if they win, add bet
if (npc1Score > 21) {
npc1Money -= npc1Bet; // NPC loses bet when busting
dealerMoney += npc1Bet;
} else if (dealerScore > 21 || npc1Score > dealerScore) {
npc1Money += npc1Bet; // NPC wins bet amount
dealerMoney -= npc1Bet;
} else if (npc1Score < dealerScore) {
npc1Money -= npc1Bet; // NPC loses bet when score is lower
dealerMoney += npc1Bet;
}
if (npc2Score > 21) {
npc2Money -= npc2Bet; // NPC loses bet when busting
dealerMoney += npc2Bet;
} else if (dealerScore > 21 || npc2Score > dealerScore) {
npc2Money += npc2Bet; // NPC wins bet amount
dealerMoney -= npc2Bet;
} else if (npc2Score < dealerScore) {
npc2Money -= npc2Bet; // NPC loses bet when score is lower
dealerMoney += npc2Bet;
}
winsText.setText('Wins: ' + wins);
lossesText.setText('Losses: ' + losses);
// Update money displays after all calculations
updateScores();
// Check for game ending conditions
if (playerMoney < 200) {
// Player loses - insufficient money to continue
gameResultText.setText('GAME OVER! Not enough money to continue!');
LK.getSound('lose').play();
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
return;
} else if (dealerMoney <= 0) {
// Player wins - dealer out of money
gameResultText.setText('YOU WIN THE GAME! Dealer ran out of money!');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
return;
} else if (npc1Money <= 0 || npc2Money <= 0) {
// NPCs out of money - reset their money and continue
if (npc1Money <= 0) npc1Money = 10000;
if (npc2Money <= 0) npc2Money = 10000;
}
// Auto start next round after 3 seconds
LK.setTimeout(function () {
startNewGame();
}, 3000);
}
function startNewGame() {
// Clear previous cards
for (var i = 0; i < playerCards.length; i++) {
playerCards[i].destroy();
}
for (var i = 0; i < dealerCards.length; i++) {
dealerCards[i].destroy();
}
for (var i = 0; i < npc1Cards.length; i++) {
npc1Cards[i].destroy();
}
for (var i = 0; i < npc2Cards.length; i++) {
npc2Cards[i].destroy();
}
playerCards = [];
dealerCards = [];
npc1Cards = [];
npc2Cards = [];
gameState = 'betting';
gameResultText.setText('');
currentBetText.setText('');
hitButton.setEnabled(false);
standButton.setEnabled(false);
setBettingUIVisible(true);
}
function startGameRound() {
gameState = 'playing';
hitButton.setEnabled(true);
standButton.setEnabled(true);
// Deal initial cards
dealCard('player'); // Player card 1
dealCard('dealer'); // Dealer card 1
dealCard('player'); // Player card 2
dealCard('dealer'); // Dealer card 2 (face down)
dealCard('npc1'); // NPC 1 card 1
dealCard('npc1'); // NPC 1 card 2
dealCard('npc2'); // NPC 2 card 1
dealCard('npc2'); // NPC 2 card 2
updateScores();
// Check for blackjack
if (playerScore === 21) {
playerStand();
}
}
// Initialize game
createDeck();
startNewGame();
game.update = function () {
// Game loop - most logic is event-driven
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Button = Container.expand(function (text, color) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset(color === 0x228B22 ? 'hitButton' : color === 0xDC143C ? 'standButton' : 'newGameButton', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(text, {
size: 36,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.enabled = true;
self.setEnabled = function (enabled) {
self.enabled = enabled;
buttonGraphics.alpha = enabled ? 1.0 : 0.5;
buttonText.alpha = enabled ? 1.0 : 0.5;
};
self.down = function (x, y, obj) {
if (!self.enabled) return;
if (text === 'HIT') {
playerHit();
} else if (text === 'STAND') {
playerStand();
} else if (text === 'Next Round') {
startNewGame();
} else if (text === '$200') {
selectBet(200);
} else if (text === '$500') {
selectBet(500);
} else if (text === '$1000') {
selectBet(1000);
}
};
return self;
});
var Card = Container.expand(function (suit, value) {
var self = Container.call(this);
self.suit = suit;
self.value = value;
self.faceUp = false;
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
var cardBack = self.attachAsset('cardBack', {
anchorX: 0.5,
anchorY: 0.5
});
// Create card text
var cardText = new Text2('', {
size: 40,
fill: 0x000000
});
cardText.anchor.set(0.5, 0.5);
self.addChild(cardText);
self.flip = function () {
self.faceUp = true;
cardBack.visible = false;
cardGraphics.visible = true;
cardText.visible = true;
var displayValue = self.value;
if (displayValue === 1) displayValue = 'A';else if (displayValue === 11) displayValue = 'J';else if (displayValue === 12) displayValue = 'Q';else if (displayValue === 13) displayValue = 'K';
var suitSymbol = '';
if (self.suit === 'hearts') suitSymbol = '♥';else if (self.suit === 'diamonds') suitSymbol = '♦';else if (self.suit === 'clubs') suitSymbol = '♣';else if (self.suit === 'spades') suitSymbol = '♠';
cardText.setText(displayValue + '\n' + suitSymbol);
if (self.suit === 'hearts' || self.suit === 'diamonds') {
cardText.fill = 0xff0000;
}
LK.getSound('cardFlip').play();
};
self.getValue = function () {
if (self.value >= 10) return 10;
return self.value;
};
// Initialize card face down
cardBack.visible = true;
cardGraphics.visible = false;
cardText.visible = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0F5132
});
/****
* Game Code
****/
// Add background image
var backgroundImage = game.addChild(LK.getAsset('background', {
anchorX: 0.0,
anchorY: 0.0
}));
backgroundImage.x = 0;
backgroundImage.y = 0;
var suits = ['hearts', 'diamonds', 'clubs', 'spades'];
var deck = [];
var playerCards = [];
var dealerCards = [];
var npc1Cards = [];
var npc2Cards = [];
var gameState = 'betting'; // 'betting', 'playing', 'dealerTurn', 'gameOver'
var playerScore = 0;
var dealerScore = 0;
var npc1Score = 0;
var npc2Score = 0;
var wins = 0;
var losses = 0;
var playerMoney = 10000;
var npc1Money = 10000;
var npc2Money = 10000;
var dealerMoney = 30000;
var currentBet = 0;
var playerBet = 0;
var npc1Bet = 0;
var npc2Bet = 0;
// UI Elements
var playerScoreText = new Text2('Player: 0', {
size: 48,
fill: 0xFFFFFF
});
playerScoreText.anchor.set(0.5, 0);
playerScoreText.x = 1024;
playerScoreText.y = 1800;
game.addChild(playerScoreText);
var dealerScoreText = new Text2('Dealer: ?', {
size: 48,
fill: 0xFFFFFF
});
dealerScoreText.anchor.set(0.5, 0);
dealerScoreText.x = 1024;
dealerScoreText.y = 500;
game.addChild(dealerScoreText);
var gameResultText = new Text2('', {
size: 60,
fill: 0xFFFF00
});
gameResultText.anchor.set(0.5, 0.5);
gameResultText.x = 1024;
gameResultText.y = 1366;
game.addChild(gameResultText);
var bettingText = new Text2('Choose Bet:', {
size: 48,
fill: 0xFFFFFF
});
bettingText.anchor.set(0.5, 0.5);
bettingText.x = 1024;
bettingText.y = 1200;
game.addChild(bettingText);
var currentBetText = new Text2('', {
size: 36,
fill: 0x00FF00
});
currentBetText.anchor.set(0.5, 0.5);
currentBetText.x = 1024;
currentBetText.y = 1300;
game.addChild(currentBetText);
var winsText = new Text2('Wins: 0', {
size: 36,
fill: 0xFFFFFF
});
winsText.anchor.set(0, 0);
winsText.x = 150;
winsText.y = 150;
game.addChild(winsText);
var lossesText = new Text2('Losses: 0', {
size: 36,
fill: 0xFFFFFF
});
lossesText.anchor.set(0, 0);
lossesText.x = 150;
lossesText.y = 200;
game.addChild(lossesText);
var npc1ScoreText = new Text2('Celil: 0', {
size: 36,
fill: 0xFFFFFF
});
npc1ScoreText.anchor.set(0.5, 0);
npc1ScoreText.x = 300;
npc1ScoreText.y = 1000;
game.addChild(npc1ScoreText);
var npc2ScoreText = new Text2('Sampuan: 0', {
size: 36,
fill: 0xFFFFFF
});
npc2ScoreText.anchor.set(0.5, 0);
npc2ScoreText.x = 1700;
npc2ScoreText.y = 1000;
game.addChild(npc2ScoreText);
var playerMoneyText = new Text2('$10000', {
size: 32,
fill: 0x00FF00
});
playerMoneyText.anchor.set(0.5, 0);
playerMoneyText.x = 1024;
playerMoneyText.y = 2300;
game.addChild(playerMoneyText);
var npc1MoneyText = new Text2('$10000', {
size: 32,
fill: 0x00FF00
});
npc1MoneyText.anchor.set(0.5, 0);
npc1MoneyText.x = 300;
npc1MoneyText.y = 1400;
game.addChild(npc1MoneyText);
var npc2MoneyText = new Text2('$10000', {
size: 32,
fill: 0x00FF00
});
npc2MoneyText.anchor.set(0.5, 0);
npc2MoneyText.x = 1700;
npc2MoneyText.y = 1400;
game.addChild(npc2MoneyText);
var dealerMoneyText = new Text2('$30000', {
size: 32,
fill: 0x00FF00
});
dealerMoneyText.anchor.set(0.5, 0);
dealerMoneyText.x = 1024;
dealerMoneyText.y = 400;
game.addChild(dealerMoneyText);
// Card deck in top right corner
var deckVisual = game.addChild(LK.getAsset('deck', {
anchorX: 1.0,
anchorY: 0.0
}));
deckVisual.x = 1948; // 100px from right edge (2048 - 100)
deckVisual.y = 100; // 100px from top
// Deck count text
var deckCountText = new Text2('52', {
size: 24,
fill: 0xFFFFFF
});
deckCountText.anchor.set(0.5, 0.5);
deckCountText.x = 1948 - 90; // Center on deck (adjusted for new width)
deckCountText.y = 225; // Center on deck (adjusted for new height)
game.addChild(deckCountText);
// Buttons
var hitButton = game.addChild(new Button('HIT', 0x228B22));
hitButton.x = 800;
hitButton.y = 2200;
var standButton = game.addChild(new Button('STAND', 0xDC143C));
standButton.x = 1200;
standButton.y = 2200;
var newGameButton = game.addChild(new Button('Next Round', 0x4169E1));
newGameButton.x = 1024;
newGameButton.y = 2400;
// Betting buttons
var bet200Button = game.addChild(new Button('$200', 0x8B4513));
bet200Button.x = 600;
bet200Button.y = 1400;
var bet500Button = game.addChild(new Button('$500', 0x8B4513));
bet500Button.x = 1024;
bet500Button.y = 1400;
var bet1000Button = game.addChild(new Button('$1000', 0x8B4513));
bet1000Button.x = 1448;
bet1000Button.y = 1400;
function selectBet(amount) {
if (gameState !== 'betting') return;
if (playerMoney < amount) return;
playerBet = amount;
currentBet = amount;
currentBetText.setText('Bahis: $' + amount);
// NPC betting logic
var betOptions = [200, 500, 1000];
npc1Bet = betOptions[Math.floor(Math.random() * betOptions.length)];
npc2Bet = betOptions[Math.floor(Math.random() * betOptions.length)];
// Ensure NPCs have enough money
if (npc1Money < npc1Bet) npc1Bet = 200;
if (npc2Money < npc2Bet) npc2Bet = 200;
// Hide betting UI and start game
setBettingUIVisible(false);
startGameRound();
}
function setBettingUIVisible(visible) {
bettingText.visible = visible;
currentBetText.visible = visible;
bet200Button.visible = visible;
bet500Button.visible = visible;
bet1000Button.visible = visible;
bet200Button.setEnabled(visible);
bet500Button.setEnabled(visible);
bet1000Button.setEnabled(visible);
}
function createDeck() {
deck = [];
for (var suitIndex = 0; suitIndex < suits.length; suitIndex++) {
var suit = suits[suitIndex];
for (var value = 1; value <= 13; value++) {
deck.push({
suit: suit,
value: value
});
}
}
// Shuffle deck
for (var i = deck.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
// Update deck count display
deckCountText.setText(deck.length.toString());
}
function dealCard(target) {
if (deck.length === 0) createDeck();
var cardData = deck.pop();
var card = new Card(cardData.suit, cardData.value);
game.addChild(card);
// Update deck count display
deckCountText.setText(deck.length.toString());
if (target === 'player') {
playerCards.push(card);
card.x = 400 + (playerCards.length - 1) * 200;
card.y = 2000;
card.flip();
} else if (target === 'npc1') {
npc1Cards.push(card);
card.x = 200 + (npc1Cards.length - 1) * 120;
card.y = 1200;
card.flip();
} else if (target === 'npc2') {
npc2Cards.push(card);
card.x = 1600 + (npc2Cards.length - 1) * 120;
card.y = 1200;
card.flip();
} else {
dealerCards.push(card);
card.x = 400 + (dealerCards.length - 1) * 200;
card.y = 800;
// Only flip first dealer card
if (dealerCards.length === 1) {
card.flip();
}
}
return card;
}
function calculateScore(cards, showAll) {
var score = 0;
var aces = 0;
for (var i = 0; i < cards.length; i++) {
var card = cards[i];
if (!showAll && i > 0 && cards === dealerCards && !card.faceUp) {
continue; // Skip face-down dealer cards
}
var value = card.getValue();
if (card.value === 1) {
aces++;
score += 11;
} else {
score += value;
}
}
// Adjust for aces
while (score > 21 && aces > 0) {
score -= 10;
aces--;
}
return score;
}
function updateScores() {
playerScore = calculateScore(playerCards, true);
dealerScore = calculateScore(dealerCards, gameState === 'dealerTurn' || gameState === 'gameOver');
npc1Score = calculateScore(npc1Cards, true);
npc2Score = calculateScore(npc2Cards, true);
playerScoreText.setText('Player: ' + playerScore);
npc1ScoreText.setText('Celil: ' + npc1Score);
npc2ScoreText.setText('Sampuan: ' + npc2Score);
if (gameState === 'dealerTurn' || gameState === 'gameOver') {
dealerScoreText.setText('Dealer: ' + dealerScore);
} else {
dealerScoreText.setText('Dealer: ?');
}
playerMoneyText.setText('$' + playerMoney);
npc1MoneyText.setText('$' + npc1Money);
npc2MoneyText.setText('$' + npc2Money);
dealerMoneyText.setText('$' + dealerMoney);
}
function npcDecision(npcCards, npcTarget) {
var score = calculateScore(npcCards, true);
// Basic AI: hit if score is 16 or less, stand if 17 or more
if (score < 17) {
dealCard(npcTarget);
updateScores();
} else {
// NPC stands
return 'stand';
}
return 'hit';
}
function processNPCTurns() {
// NPC 1 turn
if (npc1Score < 21) {
npcDecision(npc1Cards, 'npc1');
}
// NPC 2 turn
if (npc2Score < 21) {
npcDecision(npc2Cards, 'npc2');
}
}
function playerHit() {
if (gameState !== 'playing') return;
dealCard('player');
updateScores();
// Process NPC turns after player action
processNPCTurns();
updateScores();
if (playerScore > 21) {
// Player busts
gameState = 'gameOver';
gameResultText.setText('BUST! You lose!');
losses++;
lossesText.setText('Losses: ' + losses);
// Player loses bet amount when busting
playerMoney -= playerBet;
dealerMoney += playerBet;
hitButton.setEnabled(false);
standButton.setEnabled(false);
LK.getSound('lose').play();
// Reveal dealer's hidden card
if (dealerCards.length > 1 && !dealerCards[1].faceUp) {
dealerCards[1].flip();
}
updateScores();
}
}
function playerStand() {
if (gameState !== 'playing') return;
gameState = 'dealerTurn';
hitButton.setEnabled(false);
standButton.setEnabled(false);
// Process final NPC turns
processNPCTurns();
updateScores();
// Reveal dealer's hidden card
if (dealerCards.length > 1 && !dealerCards[1].faceUp) {
dealerCards[1].flip();
}
dealerPlay();
}
function dealerPlay() {
updateScores();
if (dealerScore < 17) {
// Dealer hits
LK.setTimeout(function () {
dealCard(false);
dealerCards[dealerCards.length - 1].flip();
dealerPlay();
}, 1000);
} else {
// Dealer stands
endGame();
}
}
function endGame() {
gameState = 'gameOver';
updateScores();
var playerBlackjack = playerCards.length === 2 && playerScore === 21;
var dealerBlackjack = dealerCards.length === 2 && dealerScore === 21;
// Determine winners and update money - if we lose, subtract bet amount, if we win, add bet amount
if (playerScore > 21) {
gameResultText.setText('BUST! You lose!');
losses++;
playerMoney -= playerBet; // Lose bet amount when busting
dealerMoney += playerBet;
LK.getSound('lose').play();
} else if (dealerScore > 21) {
gameResultText.setText('Dealer busts! You win!');
wins++;
playerMoney += playerBet; // Win bet amount when dealer busts
dealerMoney -= playerBet;
LK.getSound('win').play();
} else if (playerBlackjack && !dealerBlackjack) {
gameResultText.setText('BLACKJACK! You win!');
wins++;
var blackjackWin = Math.floor(playerBet * 1.5);
playerMoney += blackjackWin; // Win 1.5x bet amount for blackjack
dealerMoney -= blackjackWin;
LK.getSound('win').play();
} else if (dealerBlackjack && !playerBlackjack) {
gameResultText.setText('Dealer blackjack! You lose!');
losses++;
playerMoney -= playerBet; // Lose bet amount when dealer has blackjack
dealerMoney += playerBet;
LK.getSound('lose').play();
} else if (playerScore > dealerScore) {
gameResultText.setText('You win!');
wins++;
playerMoney += playerBet; // Win bet amount when score is higher
dealerMoney -= playerBet;
LK.getSound('win').play();
} else if (dealerScore > playerScore) {
gameResultText.setText('You lose!');
losses++;
playerMoney -= playerBet; // Lose bet amount when score is lower
dealerMoney += playerBet;
LK.getSound('lose').play();
} else {
gameResultText.setText('Push! It\'s a tie!');
// No money change on tie
}
// Update NPC money based on their performance - if they lose, subtract bet, if they win, add bet
if (npc1Score > 21) {
npc1Money -= npc1Bet; // NPC loses bet when busting
dealerMoney += npc1Bet;
} else if (dealerScore > 21 || npc1Score > dealerScore) {
npc1Money += npc1Bet; // NPC wins bet amount
dealerMoney -= npc1Bet;
} else if (npc1Score < dealerScore) {
npc1Money -= npc1Bet; // NPC loses bet when score is lower
dealerMoney += npc1Bet;
}
if (npc2Score > 21) {
npc2Money -= npc2Bet; // NPC loses bet when busting
dealerMoney += npc2Bet;
} else if (dealerScore > 21 || npc2Score > dealerScore) {
npc2Money += npc2Bet; // NPC wins bet amount
dealerMoney -= npc2Bet;
} else if (npc2Score < dealerScore) {
npc2Money -= npc2Bet; // NPC loses bet when score is lower
dealerMoney += npc2Bet;
}
winsText.setText('Wins: ' + wins);
lossesText.setText('Losses: ' + losses);
// Update money displays after all calculations
updateScores();
// Check for game ending conditions
if (playerMoney < 200) {
// Player loses - insufficient money to continue
gameResultText.setText('GAME OVER! Not enough money to continue!');
LK.getSound('lose').play();
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
return;
} else if (dealerMoney <= 0) {
// Player wins - dealer out of money
gameResultText.setText('YOU WIN THE GAME! Dealer ran out of money!');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
return;
} else if (npc1Money <= 0 || npc2Money <= 0) {
// NPCs out of money - reset their money and continue
if (npc1Money <= 0) npc1Money = 10000;
if (npc2Money <= 0) npc2Money = 10000;
}
// Auto start next round after 3 seconds
LK.setTimeout(function () {
startNewGame();
}, 3000);
}
function startNewGame() {
// Clear previous cards
for (var i = 0; i < playerCards.length; i++) {
playerCards[i].destroy();
}
for (var i = 0; i < dealerCards.length; i++) {
dealerCards[i].destroy();
}
for (var i = 0; i < npc1Cards.length; i++) {
npc1Cards[i].destroy();
}
for (var i = 0; i < npc2Cards.length; i++) {
npc2Cards[i].destroy();
}
playerCards = [];
dealerCards = [];
npc1Cards = [];
npc2Cards = [];
gameState = 'betting';
gameResultText.setText('');
currentBetText.setText('');
hitButton.setEnabled(false);
standButton.setEnabled(false);
setBettingUIVisible(true);
}
function startGameRound() {
gameState = 'playing';
hitButton.setEnabled(true);
standButton.setEnabled(true);
// Deal initial cards
dealCard('player'); // Player card 1
dealCard('dealer'); // Dealer card 1
dealCard('player'); // Player card 2
dealCard('dealer'); // Dealer card 2 (face down)
dealCard('npc1'); // NPC 1 card 1
dealCard('npc1'); // NPC 1 card 2
dealCard('npc2'); // NPC 2 card 1
dealCard('npc2'); // NPC 2 card 2
updateScores();
// Check for blackjack
if (playerScore === 21) {
playerStand();
}
}
// Initialize game
createDeck();
startNewGame();
game.update = function () {
// Game loop - most logic is event-driven
};