/**** * Plugins ****/ var storage = LK.import("@upit/storage.v1"); /**** * Initialize Game ****/ // No plugins needed for MVP // No custom classes needed for MVP var game = new LK.Game({ backgroundColor: 0x0a1a2f, title: "Cricket AI Battle" }); /**** * Game Code ****/ // --- State Variables --- // Button for scoring // Button for "Next" (AI turn or restart) // Player and AI icons var playerScores = []; var aiScores = []; var playerTotal = 0; var aiTotal = 0; var turn = 'player'; // 'player' or 'ai' var presses = 0; // Number of presses in current turn var maxPresses = 6; var gameEnded = false; // --- Persistent Stats --- // Use storage.<property> for persistent stats (returns value or undefined) var gamesPlayed = storage.gamesPlayed || 0; var playerWins = storage.playerWins || 0; var aiWins = storage.aiWins || 0; // --- UI Elements --- // Player icon var playerIcon = LK.getAsset('playerIcon', { anchorX: 0.5, anchorY: 0.5, x: 524, y: 350 }); game.addChild(playerIcon); // AI icon var aiIcon = LK.getAsset('aiIcon', { anchorX: 0.5, anchorY: 0.5, x: 2048 - 524, y: 350 }); game.addChild(aiIcon); // Games played and win counters var gamesPlayedTxt = new Text2('Games: ' + gamesPlayed, { size: 60, fill: "#fff" }); gamesPlayedTxt.anchor.set(0.5, 0.5); gamesPlayedTxt.x = 2048 / 2; gamesPlayedTxt.y = 80; game.addChild(gamesPlayedTxt); var playerWinsTxt = new Text2('Wins: ' + playerWins, { size: 60, fill: 0xFFD700 }); playerWinsTxt.anchor.set(0.5, 0.5); playerWinsTxt.x = playerIcon.x; playerWinsTxt.y = playerIcon.y - 220; game.addChild(playerWinsTxt); var aiWinsTxt = new Text2('Wins: ' + aiWins, { size: 60, fill: "#aaa" }); aiWinsTxt.anchor.set(0.5, 0.5); aiWinsTxt.x = aiIcon.x; aiWinsTxt.y = aiIcon.y - 220; game.addChild(aiWinsTxt); function updateStatsDisplay() { gamesPlayedTxt.setText('Games: ' + gamesPlayed); playerWinsTxt.setText('Wins: ' + playerWins); aiWinsTxt.setText('Wins: ' + aiWins); } // Player label var playerLabel = new Text2('YOU', { size: 80, fill: "#fff" }); playerLabel.anchor.set(0.5, 0); playerLabel.x = playerIcon.x; playerLabel.y = playerIcon.y + 90; game.addChild(playerLabel); // AI label var aiLabel = new Text2('AI', { size: 80, fill: "#fff" }); aiLabel.anchor.set(0.5, 0); aiLabel.x = aiIcon.x; aiLabel.y = aiIcon.y + 90; game.addChild(aiLabel); // Player score text var playerScoreTxt = new Text2('0', { size: 120, fill: "#fff" }); playerScoreTxt.anchor.set(0.5, 0.5); playerScoreTxt.x = playerIcon.x; playerScoreTxt.y = playerIcon.y - 140; game.addChild(playerScoreTxt); // AI score text var aiScoreTxt = new Text2('0', { size: 120, fill: "#fff" }); aiScoreTxt.anchor.set(0.5, 0.5); aiScoreTxt.x = aiIcon.x; aiScoreTxt.y = aiIcon.y - 140; game.addChild(aiScoreTxt); // "Press to Score" button var scoreBtn = LK.getAsset('scoreBtn', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 1500 }); game.addChild(scoreBtn); var scoreBtnLabel = new Text2('PRESS TO SCORE', { size: 80, fill: "#fff" }); scoreBtnLabel.anchor.set(0.5, 0.5); scoreBtnLabel.x = scoreBtn.x; scoreBtnLabel.y = scoreBtn.y; game.addChild(scoreBtnLabel); // "Next" button (hidden by default) var nextBtn = LK.getAsset('nextBtn', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2100 }); nextBtn.visible = false; game.addChild(nextBtn); var nextBtnLabel = new Text2('NEXT', { size: 70, fill: "#fff" }); nextBtnLabel.anchor.set(0.5, 0.5); nextBtnLabel.x = nextBtn.x; nextBtnLabel.y = nextBtn.y; nextBtnLabel.visible = false; game.addChild(nextBtnLabel); // Info text (shows "Your Turn", "AI Turn", "You Win", etc) var infoTxt = new Text2('', { size: 100, fill: "#fff" }); infoTxt.anchor.set(0.5, 0.5); infoTxt.x = 2048 / 2; infoTxt.y = 1200; game.addChild(infoTxt); // Per-ball score display var ballScoreTxt = new Text2('', { size: 120, fill: 0xFFD700 }); ballScoreTxt.anchor.set(0.5, 0.5); ballScoreTxt.x = 2048 / 2; ballScoreTxt.y = scoreBtn.y - 220; game.addChild(ballScoreTxt); // Ball-by-ball history var playerBallsTxt = new Text2('', { size: 60, fill: "#fff" }); playerBallsTxt.anchor.set(0.5, 0.5); playerBallsTxt.x = playerIcon.x; playerBallsTxt.y = playerIcon.y + 220; game.addChild(playerBallsTxt); var aiBallsTxt = new Text2('', { size: 60, fill: "#fff" }); aiBallsTxt.anchor.set(0.5, 0.5); aiBallsTxt.x = aiIcon.x; aiBallsTxt.y = aiIcon.y + 220; game.addChild(aiBallsTxt); // --- Functions --- function resetGame() { playerScores = []; aiScores = []; playerTotal = 0; aiTotal = 0; turn = 'player'; presses = 0; gameEnded = false; playerScoreTxt.setText('0'); aiScoreTxt.setText('0'); playerBallsTxt.setText(''); aiBallsTxt.setText(''); ballScoreTxt.setText(''); infoTxt.setText('Your Turn'); scoreBtn.visible = true; scoreBtnLabel.visible = true; nextBtn.visible = false; nextBtnLabel.visible = false; nextBtnLabel.setText('NEXT'); updateStatsDisplay(); } function updateScores() { playerTotal = 0; for (var i = 0; i < playerScores.length; i++) playerTotal += playerScores[i]; aiTotal = 0; for (var i = 0; i < aiScores.length; i++) aiTotal += aiScores[i]; playerScoreTxt.setText('' + playerTotal); aiScoreTxt.setText('' + aiTotal); playerBallsTxt.setText(playerScores.join(' ')); aiBallsTxt.setText(aiScores.join(' ')); } function endGame() { gameEnded = true; scoreBtn.visible = false; scoreBtnLabel.visible = false; nextBtn.visible = true; nextBtnLabel.visible = true; // Update stats gamesPlayed++; if (playerTotal > aiTotal) { playerWins++; infoTxt.setText('You Win!'); nextBtnLabel.setText('PLAY AGAIN'); } else if (playerTotal < aiTotal) { aiWins++; infoTxt.setText('AI Wins!'); nextBtnLabel.setText('PLAY AGAIN'); } else { infoTxt.setText('Draw!'); nextBtnLabel.setText('PLAY AGAIN'); } // Persist stats storage.gamesPlayed = gamesPlayed; storage.playerWins = playerWins; storage.aiWins = aiWins; updateStatsDisplay(); } function startAITurn() { turn = 'ai'; presses = 0; infoTxt.setText('AI Turn'); scoreBtn.visible = false; scoreBtnLabel.visible = false; nextBtn.visible = false; nextBtnLabel.visible = false; ballScoreTxt.setText(''); // Start AI "presses" LK.setTimeout(aiPress, 700); } function aiPress() { if (presses >= maxPresses) { // AI turn over updateScores(); infoTxt.setText('Your Turn'); turn = 'player'; presses = 0; scoreBtn.visible = true; scoreBtnLabel.visible = true; ballScoreTxt.setText(''); // If both have played, end game if (playerScores.length === maxPresses && aiScores.length === maxPresses) { endGame(); } return; } // AI "presses" the button var run = Math.floor(Math.random() * 7); // 0-6 aiScores.push(run); updateScores(); ballScoreTxt.setText('AI scores: ' + run); presses++; LK.setTimeout(aiPress, 700); } // --- Event Handlers --- // Score button press (player's turn) scoreBtn.down = function (x, y, obj) { if (gameEnded) return; if (turn !== 'player') return; if (presses >= maxPresses) return; var run = Math.floor(Math.random() * 7); // 0-6 playerScores.push(run); updateScores(); ballScoreTxt.setText('You scored: ' + run); presses++; if (presses >= maxPresses) { // Player turn over scoreBtn.visible = false; scoreBtnLabel.visible = false; nextBtn.visible = true; nextBtnLabel.visible = true; nextBtnLabel.setText('AI TURN'); infoTxt.setText('AI Turn'); } }; // Next button press (AI turn or restart) nextBtn.down = function (x, y, obj) { if (gameEnded) { resetGame(); return; } if (turn === 'player' && presses >= maxPresses) { // Start AI turn startAITurn(); } }; // --- Game Start --- resetGame(); // Play background music (loops by default) LK.playMusic('Sound'); // --- Game Update (not needed for this MVP) --- // --- GUI: Place info at top center --- LK.gui.top.addChild(infoTxt); // --- Prevent elements in top left 100x100 (done) --- // --- End of file ---
/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Initialize Game
****/
// No plugins needed for MVP
// No custom classes needed for MVP
var game = new LK.Game({
backgroundColor: 0x0a1a2f,
title: "Cricket AI Battle"
});
/****
* Game Code
****/
// --- State Variables ---
// Button for scoring
// Button for "Next" (AI turn or restart)
// Player and AI icons
var playerScores = [];
var aiScores = [];
var playerTotal = 0;
var aiTotal = 0;
var turn = 'player'; // 'player' or 'ai'
var presses = 0; // Number of presses in current turn
var maxPresses = 6;
var gameEnded = false;
// --- Persistent Stats ---
// Use storage.<property> for persistent stats (returns value or undefined)
var gamesPlayed = storage.gamesPlayed || 0;
var playerWins = storage.playerWins || 0;
var aiWins = storage.aiWins || 0;
// --- UI Elements ---
// Player icon
var playerIcon = LK.getAsset('playerIcon', {
anchorX: 0.5,
anchorY: 0.5,
x: 524,
y: 350
});
game.addChild(playerIcon);
// AI icon
var aiIcon = LK.getAsset('aiIcon', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 - 524,
y: 350
});
game.addChild(aiIcon);
// Games played and win counters
var gamesPlayedTxt = new Text2('Games: ' + gamesPlayed, {
size: 60,
fill: "#fff"
});
gamesPlayedTxt.anchor.set(0.5, 0.5);
gamesPlayedTxt.x = 2048 / 2;
gamesPlayedTxt.y = 80;
game.addChild(gamesPlayedTxt);
var playerWinsTxt = new Text2('Wins: ' + playerWins, {
size: 60,
fill: 0xFFD700
});
playerWinsTxt.anchor.set(0.5, 0.5);
playerWinsTxt.x = playerIcon.x;
playerWinsTxt.y = playerIcon.y - 220;
game.addChild(playerWinsTxt);
var aiWinsTxt = new Text2('Wins: ' + aiWins, {
size: 60,
fill: "#aaa"
});
aiWinsTxt.anchor.set(0.5, 0.5);
aiWinsTxt.x = aiIcon.x;
aiWinsTxt.y = aiIcon.y - 220;
game.addChild(aiWinsTxt);
function updateStatsDisplay() {
gamesPlayedTxt.setText('Games: ' + gamesPlayed);
playerWinsTxt.setText('Wins: ' + playerWins);
aiWinsTxt.setText('Wins: ' + aiWins);
}
// Player label
var playerLabel = new Text2('YOU', {
size: 80,
fill: "#fff"
});
playerLabel.anchor.set(0.5, 0);
playerLabel.x = playerIcon.x;
playerLabel.y = playerIcon.y + 90;
game.addChild(playerLabel);
// AI label
var aiLabel = new Text2('AI', {
size: 80,
fill: "#fff"
});
aiLabel.anchor.set(0.5, 0);
aiLabel.x = aiIcon.x;
aiLabel.y = aiIcon.y + 90;
game.addChild(aiLabel);
// Player score text
var playerScoreTxt = new Text2('0', {
size: 120,
fill: "#fff"
});
playerScoreTxt.anchor.set(0.5, 0.5);
playerScoreTxt.x = playerIcon.x;
playerScoreTxt.y = playerIcon.y - 140;
game.addChild(playerScoreTxt);
// AI score text
var aiScoreTxt = new Text2('0', {
size: 120,
fill: "#fff"
});
aiScoreTxt.anchor.set(0.5, 0.5);
aiScoreTxt.x = aiIcon.x;
aiScoreTxt.y = aiIcon.y - 140;
game.addChild(aiScoreTxt);
// "Press to Score" button
var scoreBtn = LK.getAsset('scoreBtn', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 1500
});
game.addChild(scoreBtn);
var scoreBtnLabel = new Text2('PRESS TO SCORE', {
size: 80,
fill: "#fff"
});
scoreBtnLabel.anchor.set(0.5, 0.5);
scoreBtnLabel.x = scoreBtn.x;
scoreBtnLabel.y = scoreBtn.y;
game.addChild(scoreBtnLabel);
// "Next" button (hidden by default)
var nextBtn = LK.getAsset('nextBtn', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2100
});
nextBtn.visible = false;
game.addChild(nextBtn);
var nextBtnLabel = new Text2('NEXT', {
size: 70,
fill: "#fff"
});
nextBtnLabel.anchor.set(0.5, 0.5);
nextBtnLabel.x = nextBtn.x;
nextBtnLabel.y = nextBtn.y;
nextBtnLabel.visible = false;
game.addChild(nextBtnLabel);
// Info text (shows "Your Turn", "AI Turn", "You Win", etc)
var infoTxt = new Text2('', {
size: 100,
fill: "#fff"
});
infoTxt.anchor.set(0.5, 0.5);
infoTxt.x = 2048 / 2;
infoTxt.y = 1200;
game.addChild(infoTxt);
// Per-ball score display
var ballScoreTxt = new Text2('', {
size: 120,
fill: 0xFFD700
});
ballScoreTxt.anchor.set(0.5, 0.5);
ballScoreTxt.x = 2048 / 2;
ballScoreTxt.y = scoreBtn.y - 220;
game.addChild(ballScoreTxt);
// Ball-by-ball history
var playerBallsTxt = new Text2('', {
size: 60,
fill: "#fff"
});
playerBallsTxt.anchor.set(0.5, 0.5);
playerBallsTxt.x = playerIcon.x;
playerBallsTxt.y = playerIcon.y + 220;
game.addChild(playerBallsTxt);
var aiBallsTxt = new Text2('', {
size: 60,
fill: "#fff"
});
aiBallsTxt.anchor.set(0.5, 0.5);
aiBallsTxt.x = aiIcon.x;
aiBallsTxt.y = aiIcon.y + 220;
game.addChild(aiBallsTxt);
// --- Functions ---
function resetGame() {
playerScores = [];
aiScores = [];
playerTotal = 0;
aiTotal = 0;
turn = 'player';
presses = 0;
gameEnded = false;
playerScoreTxt.setText('0');
aiScoreTxt.setText('0');
playerBallsTxt.setText('');
aiBallsTxt.setText('');
ballScoreTxt.setText('');
infoTxt.setText('Your Turn');
scoreBtn.visible = true;
scoreBtnLabel.visible = true;
nextBtn.visible = false;
nextBtnLabel.visible = false;
nextBtnLabel.setText('NEXT');
updateStatsDisplay();
}
function updateScores() {
playerTotal = 0;
for (var i = 0; i < playerScores.length; i++) playerTotal += playerScores[i];
aiTotal = 0;
for (var i = 0; i < aiScores.length; i++) aiTotal += aiScores[i];
playerScoreTxt.setText('' + playerTotal);
aiScoreTxt.setText('' + aiTotal);
playerBallsTxt.setText(playerScores.join(' '));
aiBallsTxt.setText(aiScores.join(' '));
}
function endGame() {
gameEnded = true;
scoreBtn.visible = false;
scoreBtnLabel.visible = false;
nextBtn.visible = true;
nextBtnLabel.visible = true;
// Update stats
gamesPlayed++;
if (playerTotal > aiTotal) {
playerWins++;
infoTxt.setText('You Win!');
nextBtnLabel.setText('PLAY AGAIN');
} else if (playerTotal < aiTotal) {
aiWins++;
infoTxt.setText('AI Wins!');
nextBtnLabel.setText('PLAY AGAIN');
} else {
infoTxt.setText('Draw!');
nextBtnLabel.setText('PLAY AGAIN');
}
// Persist stats
storage.gamesPlayed = gamesPlayed;
storage.playerWins = playerWins;
storage.aiWins = aiWins;
updateStatsDisplay();
}
function startAITurn() {
turn = 'ai';
presses = 0;
infoTxt.setText('AI Turn');
scoreBtn.visible = false;
scoreBtnLabel.visible = false;
nextBtn.visible = false;
nextBtnLabel.visible = false;
ballScoreTxt.setText('');
// Start AI "presses"
LK.setTimeout(aiPress, 700);
}
function aiPress() {
if (presses >= maxPresses) {
// AI turn over
updateScores();
infoTxt.setText('Your Turn');
turn = 'player';
presses = 0;
scoreBtn.visible = true;
scoreBtnLabel.visible = true;
ballScoreTxt.setText('');
// If both have played, end game
if (playerScores.length === maxPresses && aiScores.length === maxPresses) {
endGame();
}
return;
}
// AI "presses" the button
var run = Math.floor(Math.random() * 7); // 0-6
aiScores.push(run);
updateScores();
ballScoreTxt.setText('AI scores: ' + run);
presses++;
LK.setTimeout(aiPress, 700);
}
// --- Event Handlers ---
// Score button press (player's turn)
scoreBtn.down = function (x, y, obj) {
if (gameEnded) return;
if (turn !== 'player') return;
if (presses >= maxPresses) return;
var run = Math.floor(Math.random() * 7); // 0-6
playerScores.push(run);
updateScores();
ballScoreTxt.setText('You scored: ' + run);
presses++;
if (presses >= maxPresses) {
// Player turn over
scoreBtn.visible = false;
scoreBtnLabel.visible = false;
nextBtn.visible = true;
nextBtnLabel.visible = true;
nextBtnLabel.setText('AI TURN');
infoTxt.setText('AI Turn');
}
};
// Next button press (AI turn or restart)
nextBtn.down = function (x, y, obj) {
if (gameEnded) {
resetGame();
return;
}
if (turn === 'player' && presses >= maxPresses) {
// Start AI turn
startAITurn();
}
};
// --- Game Start ---
resetGame();
// Play background music (loops by default)
LK.playMusic('Sound');
// --- Game Update (not needed for this MVP) ---
// --- GUI: Place info at top center ---
LK.gui.top.addChild(infoTxt);
// --- Prevent elements in top left 100x100 (done) ---
// --- End of file ---