/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Button = Container.expand(function (label) {
var self = Container.call(this);
self.isEnabled = true;
self.isHovered = false;
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(label, {
size: 90,
fill: '#ffffff'
});
buttonText.anchor.set(0.5, 0.5);
buttonText.y = 15;
self.addChild(buttonText);
self.down = function (x, y, obj) {
if (self.isEnabled && self.onPress) {
self.onPress();
}
};
self.move = function (x, y, obj) {
if (self.isEnabled) {
var localPos = self.toLocal({
x: x,
y: y
});
if (localPos.x > -100 && localPos.x < 100 && localPos.y > -30 && localPos.y < 30) {
if (!self.isHovered) {
self.isHovered = true;
buttonGraphics.tint = 0x357abd;
}
} else {
if (self.isHovered) {
self.isHovered = false;
buttonGraphics.tint = 0x4a90e2;
}
}
}
};
self.setEnabled = function (enabled) {
self.isEnabled = enabled;
if (enabled) {
buttonGraphics.alpha = 1;
} else {
buttonGraphics.alpha = 0.5;
}
};
self.setEnabled(true);
return self;
});
var Card = Container.expand(function () {
var self = Container.call(this);
self.value = 0;
self.isHidden = false;
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
var cardText = new Text2('', {
size: 120,
fill: '#000000'
});
cardText.anchor.set(0.5, 0.5);
self.addChild(cardText);
self.setValue = function (value) {
self.value = value;
cardText.setText(value.toString());
};
self.setHidden = function (hidden) {
self.isHidden = hidden;
if (hidden) {
cardGraphics.tint = 0x1a5f1a;
cardText.setText('?');
cardText.style.fill = '#ffffff';
} else {
cardGraphics.tint = 0xffffff;
cardText.style.fill = '#000000';
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
self.cards = [];
self.total = 0;
self.hasStood = false;
self.isBust = false;
var cardsContainer = new Container();
self.addChild(cardsContainer);
var totalText = new Text2('Total: 0', {
size: 150,
fill: '#ffffff'
});
totalText.anchor.set(0.5, 0);
totalText.y = 220;
self.addChild(totalText);
self.addCard = function (cardValue) {
var card = new Card();
card.setValue(cardValue);
card.x = self.cards.length * 100;
cardsContainer.addChild(card);
self.cards.push(card);
self.total += cardValue;
if (self.total > 21) {
self.isBust = true;
}
totalText.setText('Total: ' + self.total);
};
self.stand = function () {
self.hasStood = true;
};
self.reset = function () {
self.cards = [];
self.total = 0;
self.hasStood = false;
self.isBust = false;
cardsContainer.removeChildren();
totalText.setText('Total: 0');
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a5f1a
});
/****
* Game Code
****/
// Game state
var gameState = 'playing'; // 'playing', 'roundOver', 'gameOver'
var currentTurn = 'player'; // 'player', 'ai'
var deckCards = [];
var playerScore = storage.playerScore || 0;
var aiScore = storage.aiScore || 0;
var roundNumber = storage.roundNumber || 1;
// Initialize deck
function initializeDeck() {
deckCards = [];
for (var i = 0; i < 4; i++) {
// 4 decks
for (var j = 1; j <= 10; j++) {
deckCards.push(j);
}
}
shuffleDeck();
}
// Fisher-Yates shuffle
function shuffleDeck() {
for (var i = deckCards.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = deckCards[i];
deckCards[i] = deckCards[j];
deckCards[j] = temp;
}
}
function drawCard() {
if (deckCards.length === 0) {
initializeDeck();
}
return deckCards.pop();
}
// UI Elements
var titleText = new Text2('Blackjack Duel', {
size: 180,
fill: '#ffffff'
});
titleText.anchor.set(0.5, 0);
titleText.x = 1024;
titleText.y = 40;
game.addChild(titleText);
// Score display
var scoreText = new Text2('Player: ' + playerScore + ' | AI: ' + aiScore + ' | Round: ' + roundNumber, {
size: 105,
fill: '#ffffff'
});
scoreText.anchor.set(0.5, 0);
scoreText.x = 1024;
scoreText.y = 240;
game.addChild(scoreText);
// Player section
var playerLabel = new Text2('You', {
size: 120,
fill: '#ffffff'
});
playerLabel.anchor.set(0.5, 0);
playerLabel.x = 512;
playerLabel.y = 370;
game.addChild(playerLabel);
var player = new Player();
player.x = 512;
player.y = 530;
game.addChild(player);
// AI section
var aiLabel = new Text2('AI Opponent', {
size: 120,
fill: '#ffffff'
});
aiLabel.anchor.set(0.5, 0);
aiLabel.x = 1536;
aiLabel.y = 370;
game.addChild(aiLabel);
var ai = new Player();
ai.x = 1536;
ai.y = 530;
game.addChild(ai);
// Buttons
var hitButton = new Button('Hit');
hitButton.x = 768;
hitButton.y = 1450;
hitButton.onPress = function () {
if (gameState === 'playing' && currentTurn === 'player') {
var newCard = drawCard();
player.addCard(newCard);
LK.getSound('cardDraw').play();
if (player.isBust) {
gameState = 'roundOver';
currentTurn = 'ai';
hitButton.setEnabled(false);
standButton.setEnabled(false);
aiPlay();
} else {
//{1r_new}
currentTurn = 'ai'; //{1r_new2}
hitButton.setEnabled(false); //{1r_new3}
standButton.setEnabled(false); //{1r_new4}
aiPlay(); //{1r_new5}
}
}
};
game.addChild(hitButton);
var standButton = new Button('Stand');
standButton.x = 1280;
standButton.y = 1450;
standButton.onPress = function () {
if (gameState === 'playing' && currentTurn === 'player') {
player.stand();
gameState = 'roundOver';
currentTurn = 'ai';
hitButton.setEnabled(false);
standButton.setEnabled(false);
aiPlay();
}
};
game.addChild(standButton);
// Message display
var messageText = new Text2('', {
size: 135,
fill: '#ffff00'
});
messageText.anchor.set(0.5, 0);
messageText.x = 1024;
messageText.y = 1700;
game.addChild(messageText);
// Next Round button
var nextRoundButton = new Button('Next Round');
nextRoundButton.x = 1024;
nextRoundButton.y = 2050;
nextRoundButton.setEnabled(false);
nextRoundButton.onPress = function () {
startNewRound();
};
game.addChild(nextRoundButton);
function aiPlay() {
// AI plays after player makes choice, first dealing initial card then drawing additional cards
LK.setTimeout(function () {
// Deal initial card to AI
var initialAiCard = drawCard();
ai.addCard(initialAiCard);
LK.getSound('cardDraw').play();
LK.setTimeout(function () {
function drawAiCard() {
if (ai.total < 17 && !ai.isBust) {
var newCard = drawCard();
ai.addCard(newCard);
LK.getSound('cardDraw').play();
LK.setTimeout(drawAiCard, 800);
} else {
ai.stand();
determineWinner();
}
}
drawAiCard();
}, 800);
}, 1000);
}
function determineWinner() {
var playerBust = player.total > 21;
var aiBust = ai.total > 21;
var roundWinner = 'tie';
if (playerBust && aiBust) {
roundWinner = 'tie';
messageText.setText('Both Bust! Tie Round.');
} else if (playerBust) {
roundWinner = 'ai';
aiScore += 1;
messageText.setText('You Bust! AI Wins!');
LK.getSound('lose').play();
} else if (aiBust) {
roundWinner = 'player';
playerScore += 1;
messageText.setText('AI Busts! You Win!');
LK.getSound('win').play();
} else if (player.total > ai.total) {
roundWinner = 'player';
playerScore += 1;
messageText.setText('You Win This Round!');
LK.getSound('win').play();
} else if (ai.total > player.total) {
roundWinner = 'ai';
aiScore += 1;
messageText.setText('AI Wins This Round!');
LK.getSound('lose').play();
} else {
roundWinner = 'tie';
messageText.setText('Tie! Push Round.');
}
hitButton.setEnabled(false);
standButton.setEnabled(false);
nextRoundButton.setEnabled(true);
storage.playerScore = playerScore;
storage.aiScore = aiScore;
storage.roundNumber = roundNumber + 1;
}
function startNewRound() {
roundNumber += 1;
player.reset();
ai.reset();
gameState = 'playing';
currentTurn = 'player'; //{28_new}
messageText.setText('');
hitButton.setEnabled(true);
standButton.setEnabled(true);
nextRoundButton.setEnabled(false);
scoreText.setText('Player: ' + playerScore + ' | AI: ' + aiScore + ' | Round: ' + roundNumber);
// Deal initial card to player only
var initialPlayerCard = drawCard();
player.addCard(initialPlayerCard);
LK.getSound('cardDraw').play();
}
// Game event handlers
game.move = function (x, y, obj) {
hitButton.move(x, y, obj);
standButton.move(x, y, obj);
nextRoundButton.move(x, y, obj);
};
game.down = function (x, y, obj) {
hitButton.down(x, y, obj);
standButton.down(x, y, obj);
nextRoundButton.down(x, y, obj);
};
// Initialize game
var startOverlay = LK.getAsset('startScreen', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
game.addChild(startOverlay);
// Create start button on overlay
var startButton = new Button('Start Game');
startButton.x = 1024;
startButton.y = 1366;
startButton.onPress = function () {
game.removeChild(startOverlay);
game.removeChild(startButton);
initializeDeck();
startNewRound();
LK.playMusic('bgmusic');
};
game.addChild(startButton);
game.update = function () {
// Game logic is handled by event handlers and timed functions
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Button = Container.expand(function (label) {
var self = Container.call(this);
self.isEnabled = true;
self.isHovered = false;
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(label, {
size: 90,
fill: '#ffffff'
});
buttonText.anchor.set(0.5, 0.5);
buttonText.y = 15;
self.addChild(buttonText);
self.down = function (x, y, obj) {
if (self.isEnabled && self.onPress) {
self.onPress();
}
};
self.move = function (x, y, obj) {
if (self.isEnabled) {
var localPos = self.toLocal({
x: x,
y: y
});
if (localPos.x > -100 && localPos.x < 100 && localPos.y > -30 && localPos.y < 30) {
if (!self.isHovered) {
self.isHovered = true;
buttonGraphics.tint = 0x357abd;
}
} else {
if (self.isHovered) {
self.isHovered = false;
buttonGraphics.tint = 0x4a90e2;
}
}
}
};
self.setEnabled = function (enabled) {
self.isEnabled = enabled;
if (enabled) {
buttonGraphics.alpha = 1;
} else {
buttonGraphics.alpha = 0.5;
}
};
self.setEnabled(true);
return self;
});
var Card = Container.expand(function () {
var self = Container.call(this);
self.value = 0;
self.isHidden = false;
var cardGraphics = self.attachAsset('card', {
anchorX: 0.5,
anchorY: 0.5
});
var cardText = new Text2('', {
size: 120,
fill: '#000000'
});
cardText.anchor.set(0.5, 0.5);
self.addChild(cardText);
self.setValue = function (value) {
self.value = value;
cardText.setText(value.toString());
};
self.setHidden = function (hidden) {
self.isHidden = hidden;
if (hidden) {
cardGraphics.tint = 0x1a5f1a;
cardText.setText('?');
cardText.style.fill = '#ffffff';
} else {
cardGraphics.tint = 0xffffff;
cardText.style.fill = '#000000';
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
self.cards = [];
self.total = 0;
self.hasStood = false;
self.isBust = false;
var cardsContainer = new Container();
self.addChild(cardsContainer);
var totalText = new Text2('Total: 0', {
size: 150,
fill: '#ffffff'
});
totalText.anchor.set(0.5, 0);
totalText.y = 220;
self.addChild(totalText);
self.addCard = function (cardValue) {
var card = new Card();
card.setValue(cardValue);
card.x = self.cards.length * 100;
cardsContainer.addChild(card);
self.cards.push(card);
self.total += cardValue;
if (self.total > 21) {
self.isBust = true;
}
totalText.setText('Total: ' + self.total);
};
self.stand = function () {
self.hasStood = true;
};
self.reset = function () {
self.cards = [];
self.total = 0;
self.hasStood = false;
self.isBust = false;
cardsContainer.removeChildren();
totalText.setText('Total: 0');
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a5f1a
});
/****
* Game Code
****/
// Game state
var gameState = 'playing'; // 'playing', 'roundOver', 'gameOver'
var currentTurn = 'player'; // 'player', 'ai'
var deckCards = [];
var playerScore = storage.playerScore || 0;
var aiScore = storage.aiScore || 0;
var roundNumber = storage.roundNumber || 1;
// Initialize deck
function initializeDeck() {
deckCards = [];
for (var i = 0; i < 4; i++) {
// 4 decks
for (var j = 1; j <= 10; j++) {
deckCards.push(j);
}
}
shuffleDeck();
}
// Fisher-Yates shuffle
function shuffleDeck() {
for (var i = deckCards.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = deckCards[i];
deckCards[i] = deckCards[j];
deckCards[j] = temp;
}
}
function drawCard() {
if (deckCards.length === 0) {
initializeDeck();
}
return deckCards.pop();
}
// UI Elements
var titleText = new Text2('Blackjack Duel', {
size: 180,
fill: '#ffffff'
});
titleText.anchor.set(0.5, 0);
titleText.x = 1024;
titleText.y = 40;
game.addChild(titleText);
// Score display
var scoreText = new Text2('Player: ' + playerScore + ' | AI: ' + aiScore + ' | Round: ' + roundNumber, {
size: 105,
fill: '#ffffff'
});
scoreText.anchor.set(0.5, 0);
scoreText.x = 1024;
scoreText.y = 240;
game.addChild(scoreText);
// Player section
var playerLabel = new Text2('You', {
size: 120,
fill: '#ffffff'
});
playerLabel.anchor.set(0.5, 0);
playerLabel.x = 512;
playerLabel.y = 370;
game.addChild(playerLabel);
var player = new Player();
player.x = 512;
player.y = 530;
game.addChild(player);
// AI section
var aiLabel = new Text2('AI Opponent', {
size: 120,
fill: '#ffffff'
});
aiLabel.anchor.set(0.5, 0);
aiLabel.x = 1536;
aiLabel.y = 370;
game.addChild(aiLabel);
var ai = new Player();
ai.x = 1536;
ai.y = 530;
game.addChild(ai);
// Buttons
var hitButton = new Button('Hit');
hitButton.x = 768;
hitButton.y = 1450;
hitButton.onPress = function () {
if (gameState === 'playing' && currentTurn === 'player') {
var newCard = drawCard();
player.addCard(newCard);
LK.getSound('cardDraw').play();
if (player.isBust) {
gameState = 'roundOver';
currentTurn = 'ai';
hitButton.setEnabled(false);
standButton.setEnabled(false);
aiPlay();
} else {
//{1r_new}
currentTurn = 'ai'; //{1r_new2}
hitButton.setEnabled(false); //{1r_new3}
standButton.setEnabled(false); //{1r_new4}
aiPlay(); //{1r_new5}
}
}
};
game.addChild(hitButton);
var standButton = new Button('Stand');
standButton.x = 1280;
standButton.y = 1450;
standButton.onPress = function () {
if (gameState === 'playing' && currentTurn === 'player') {
player.stand();
gameState = 'roundOver';
currentTurn = 'ai';
hitButton.setEnabled(false);
standButton.setEnabled(false);
aiPlay();
}
};
game.addChild(standButton);
// Message display
var messageText = new Text2('', {
size: 135,
fill: '#ffff00'
});
messageText.anchor.set(0.5, 0);
messageText.x = 1024;
messageText.y = 1700;
game.addChild(messageText);
// Next Round button
var nextRoundButton = new Button('Next Round');
nextRoundButton.x = 1024;
nextRoundButton.y = 2050;
nextRoundButton.setEnabled(false);
nextRoundButton.onPress = function () {
startNewRound();
};
game.addChild(nextRoundButton);
function aiPlay() {
// AI plays after player makes choice, first dealing initial card then drawing additional cards
LK.setTimeout(function () {
// Deal initial card to AI
var initialAiCard = drawCard();
ai.addCard(initialAiCard);
LK.getSound('cardDraw').play();
LK.setTimeout(function () {
function drawAiCard() {
if (ai.total < 17 && !ai.isBust) {
var newCard = drawCard();
ai.addCard(newCard);
LK.getSound('cardDraw').play();
LK.setTimeout(drawAiCard, 800);
} else {
ai.stand();
determineWinner();
}
}
drawAiCard();
}, 800);
}, 1000);
}
function determineWinner() {
var playerBust = player.total > 21;
var aiBust = ai.total > 21;
var roundWinner = 'tie';
if (playerBust && aiBust) {
roundWinner = 'tie';
messageText.setText('Both Bust! Tie Round.');
} else if (playerBust) {
roundWinner = 'ai';
aiScore += 1;
messageText.setText('You Bust! AI Wins!');
LK.getSound('lose').play();
} else if (aiBust) {
roundWinner = 'player';
playerScore += 1;
messageText.setText('AI Busts! You Win!');
LK.getSound('win').play();
} else if (player.total > ai.total) {
roundWinner = 'player';
playerScore += 1;
messageText.setText('You Win This Round!');
LK.getSound('win').play();
} else if (ai.total > player.total) {
roundWinner = 'ai';
aiScore += 1;
messageText.setText('AI Wins This Round!');
LK.getSound('lose').play();
} else {
roundWinner = 'tie';
messageText.setText('Tie! Push Round.');
}
hitButton.setEnabled(false);
standButton.setEnabled(false);
nextRoundButton.setEnabled(true);
storage.playerScore = playerScore;
storage.aiScore = aiScore;
storage.roundNumber = roundNumber + 1;
}
function startNewRound() {
roundNumber += 1;
player.reset();
ai.reset();
gameState = 'playing';
currentTurn = 'player'; //{28_new}
messageText.setText('');
hitButton.setEnabled(true);
standButton.setEnabled(true);
nextRoundButton.setEnabled(false);
scoreText.setText('Player: ' + playerScore + ' | AI: ' + aiScore + ' | Round: ' + roundNumber);
// Deal initial card to player only
var initialPlayerCard = drawCard();
player.addCard(initialPlayerCard);
LK.getSound('cardDraw').play();
}
// Game event handlers
game.move = function (x, y, obj) {
hitButton.move(x, y, obj);
standButton.move(x, y, obj);
nextRoundButton.move(x, y, obj);
};
game.down = function (x, y, obj) {
hitButton.down(x, y, obj);
standButton.down(x, y, obj);
nextRoundButton.down(x, y, obj);
};
// Initialize game
var startOverlay = LK.getAsset('startScreen', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
game.addChild(startOverlay);
// Create start button on overlay
var startButton = new Button('Start Game');
startButton.x = 1024;
startButton.y = 1366;
startButton.onPress = function () {
game.removeChild(startOverlay);
game.removeChild(startButton);
initializeDeck();
startNewRound();
LK.playMusic('bgmusic');
};
game.addChild(startButton);
game.update = function () {
// Game logic is handled by event handlers and timed functions
};