/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0,
	currentLevel: 1
});
/**** 
* Classes
****/ 
var Card = Container.expand(function (symbol, soundId) {
	var self = Container.call(this);
	self.symbol = symbol;
	self.soundId = soundId;
	self.isFlipped = false;
	self.isMatched = false;
	// Create card back (visible initially)
	var back = self.attachAsset('cardBack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create card front (hidden initially)
	var front = self.attachAsset('cardFront', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0
	});
	// Add image to front
	var image = self.attachAsset(symbol, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	front.addChild(image);
	// Make card interactive
	self.interactive = true;
	// Flip animation
	self.flip = function () {
		if (self.isMatched || self.isFlipped || !gameActive || flippedCards.length >= 2) {
			return;
		}
		self.isFlipped = true;
		LK.getSound('flip').play();
		// Play the card's sound
		if (self.soundId) {
			LK.getSound(self.soundId).play();
		}
		// Animate card flip
		tween(back, {
			scaleX: 0
		}, {
			duration: 150,
			onFinish: function onFinish() {
				tween(front, {
					alpha: 1
				}, {
					duration: 0
				});
				tween(front, {
					scaleX: 1
				}, {
					duration: 150
				});
			}
		});
		// Add to flipped cards array
		flippedCards.push(self);
		// Check for match if two cards are flipped
		if (flippedCards.length === 2) {
			checkForMatch();
		}
	};
	// Reset card to face down
	self.reset = function () {
		self.isFlipped = false;
		tween(front, {
			scaleX: 0
		}, {
			duration: 150,
			onFinish: function onFinish() {
				tween(front, {
					alpha: 0
				}, {
					duration: 0
				});
				tween(back, {
					scaleX: 1
				}, {
					duration: 150
				});
			}
		});
	};
	// Show match animation
	self.showMatch = function () {
		self.isMatched = true;
		// Pulse animation for matched cards
		tween(self, {
			scaleX: 1.1,
			scaleY: 1.1
		}, {
			duration: 200,
			onFinish: function onFinish() {
				tween(self, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 200
				});
			}
		});
	};
	// Handle card press
	self.down = function (x, y, obj) {
		self.flip();
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xFFFFFF
});
/**** 
* Game Code
****/ 
// Create back button
var backButton = new Text2("Back", {
	size: 60,
	fill: 0x000000 // Black color
});
backButton.anchor.set(0, 1);
backButton.x = 10;
backButton.y = 2732 - 10;
backButton.interactive = true;
backButton.down = function () {
	// Logic for going back a level or screen
	if (level > 1) {
		level--;
		startGame();
	}
};
LK.gui.bottomLeft.addChild(backButton);
// Create next button
var nextButton = new Text2("Next", {
	size: 60,
	fill: 0x000000 // Black color
});
nextButton.anchor.set(1, 1);
nextButton.x = 2048 - 10;
nextButton.y = 2732 - 10;
nextButton.interactive = true;
nextButton.down = function () {
	// Logic for going to the next level or screen
	if (level < 5) {
		level++;
		startGame();
	}
};
LK.gui.bottomRight.addChild(nextButton);
var background = LK.getAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
game.addChild(background);
// Game variables
// Initialize animal images
var level = storage.currentLevel || 1;
var rows = 3;
var cols = 4;
var cardSpacing = 30;
var cardWidth = 300;
var cardHeight = 400;
var flippedCards = [];
var matchedPairs = 0;
var requiredPairs = 0;
var gameActive = false;
var timeLeft = 60;
var timeWarningGiven = false;
var gameTimer = null;
var cards = [];
var symbols = [];
var soundMap = {
	'A': 'dog',
	'B': 'cat',
	'C': 'elephant',
	'D': 'monkey',
	'E': 'lion',
	'F': 'bird',
	'G': 'frog',
	'H': 'dolphin'
};
// Difficulty settings based on level
function setDifficulty() {
	if (level === 1) {
		rows = 3;
		cols = 4;
		timeLeft = 60;
	} else if (level === 2) {
		rows = 4;
		cols = 4;
		timeLeft = 75;
	} else {
		rows = 4;
		cols = 5;
		timeLeft = 90;
	}
	requiredPairs = rows * cols / 2;
}
// Create symbols array
function createSymbols() {
	var availableSymbols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
	symbols = [];
	// Create pairs of symbols
	for (var i = 0; i < requiredPairs; i++) {
		var symbol = availableSymbols[i];
		symbols.push(symbol);
		symbols.push(symbol);
	}
	// Shuffle
	shuffleArray(symbols);
}
// Fisher-Yates shuffle algorithm
function shuffleArray(array) {
	for (var i = array.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
}
// Create game board
function createBoard() {
	var boardWidth = cols * cardWidth + (cols - 1) * cardSpacing;
	var boardHeight = rows * cardHeight + (rows - 1) * cardSpacing;
	var startX = (2048 - boardWidth) / 2 + cardWidth / 2;
	var startY = (2732 - boardHeight) / 2 + cardHeight / 2;
	var index = 0;
	for (var r = 0; r < rows; r++) {
		for (var c = 0; c < cols; c++) {
			var x = startX + c * (cardWidth + cardSpacing);
			var y = startY + r * (cardHeight + cardSpacing);
			var symbol = symbols[index];
			var soundId = soundMap[symbol] || null;
			var imageId = 'animal_' + symbol; // Assuming images are named like 'animal_A', 'animal_B', etc.
			var card = new Card(imageId, soundId);
			card.x = x;
			card.y = y;
			game.addChild(card);
			cards.push(card);
			index++;
		}
	}
}
// Check for match when two cards are flipped
function checkForMatch() {
	var card1 = flippedCards[0];
	var card2 = flippedCards[1];
	LK.setTimeout(function () {
		if (card1.symbol === card2.symbol) {
			// Match found
			LK.getSound('match').play();
			card1.showMatch();
			card2.showMatch();
			matchedPairs++;
			// Update score
			LK.setScore(LK.getScore() + 100);
			scoreText.setText(LK.getScore());
			// Check if all pairs are matched
			if (matchedPairs === requiredPairs) {
				gameWon();
			}
		} else {
			// No match, flip cards back
			LK.getSound('nomatch').play();
			card1.reset();
			card2.reset();
		}
		flippedCards = [];
	}, 1000);
}
// Timer update
function updateTimer() {
	timeLeft--;
	timerText.setText("Time: " + timeLeft);
	// Time running out warning
	if (timeLeft <= 10 && !timeWarningGiven) {
		timeWarningGiven = true;
		timerText.setStyle({
			fill: 0xE74C3C
		});
		LK.getSound('timewarning').play();
	}
	// Game over if time runs out
	if (timeLeft <= 0) {
		gameOver();
	}
}
// Game won
function gameWon() {
	LK.clearInterval(gameTimer);
	gameActive = false;
	// Add time bonus
	var timeBonus = timeLeft * 10;
	LK.setScore(LK.getScore() + timeBonus);
	scoreText.setText(LK.getScore());
	// Update high score
	if (LK.getScore() > storage.highScore) {
		storage.highScore = LK.getScore();
	}
	// Update level
	level++;
	if (level > 5) {
		level = 1; // Reset to level 1 after level 5
	}
	storage.currentLevel = level;
	// Play win sound
	LK.getSound('win').play();
	// Show success message
	LK.showYouWin();
}
// Game over
function gameOver() {
	LK.clearInterval(gameTimer);
	gameActive = false;
	// Show game over screen
	LK.showGameOver();
}
// Start a new game
function startGame() {
	// Reset game state
	matchedPairs = 0;
	flippedCards = [];
	timeWarningGiven = false;
	LK.setScore(0);
	scoreText.setText("0");
	// Clear previous game
	for (var i = 0; i < cards.length; i++) {
		cards[i].destroy();
	}
	cards = [];
	// Set up new game
	setDifficulty();
	createSymbols();
	createBoard();
	// Reset timer
	timerText.setText("Time: " + timeLeft);
	timerText.setStyle({
		fill: 0xFFFFFF
	});
	// Start timer
	if (gameTimer) {
		LK.clearInterval(gameTimer);
	}
	gameTimer = LK.setInterval(updateTimer, 1000);
	// Start game
	gameActive = true;
	// Play background music
	LK.playMusic('bgmusic', {
		fade: {
			start: 0,
			end: 0.3,
			duration: 1000
		}
	});
}
// Create UI elements
var titleText = new Text2("Crazy Card Match", {
	size: 80,
	fill: 0x000000
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
titleText.y = 50;
var scoreText = new Text2("0", {
	size: 60,
	fill: 0x000000
});
scoreText.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreText);
scoreText.x = -10;
scoreText.y = 50;
var timerText = new Text2("Time: 60", {
	size: 60,
	fill: 0x000000
});
timerText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(timerText);
timerText.y = -10;
var levelText = new Text2("Level: " + level, {
	size: 60,
	fill: 0x000000
});
levelText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(levelText);
levelText.y = -100;
// Start the game
LK.setTimeout(function () {
	startGame();
}, 500);
// Main game update loop
game.update = function () {
	// Update level text (in case level changes)
	levelText.setText("Level: " + level);
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0,
	currentLevel: 1
});
/**** 
* Classes
****/ 
var Card = Container.expand(function (symbol, soundId) {
	var self = Container.call(this);
	self.symbol = symbol;
	self.soundId = soundId;
	self.isFlipped = false;
	self.isMatched = false;
	// Create card back (visible initially)
	var back = self.attachAsset('cardBack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create card front (hidden initially)
	var front = self.attachAsset('cardFront', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0
	});
	// Add image to front
	var image = self.attachAsset(symbol, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	front.addChild(image);
	// Make card interactive
	self.interactive = true;
	// Flip animation
	self.flip = function () {
		if (self.isMatched || self.isFlipped || !gameActive || flippedCards.length >= 2) {
			return;
		}
		self.isFlipped = true;
		LK.getSound('flip').play();
		// Play the card's sound
		if (self.soundId) {
			LK.getSound(self.soundId).play();
		}
		// Animate card flip
		tween(back, {
			scaleX: 0
		}, {
			duration: 150,
			onFinish: function onFinish() {
				tween(front, {
					alpha: 1
				}, {
					duration: 0
				});
				tween(front, {
					scaleX: 1
				}, {
					duration: 150
				});
			}
		});
		// Add to flipped cards array
		flippedCards.push(self);
		// Check for match if two cards are flipped
		if (flippedCards.length === 2) {
			checkForMatch();
		}
	};
	// Reset card to face down
	self.reset = function () {
		self.isFlipped = false;
		tween(front, {
			scaleX: 0
		}, {
			duration: 150,
			onFinish: function onFinish() {
				tween(front, {
					alpha: 0
				}, {
					duration: 0
				});
				tween(back, {
					scaleX: 1
				}, {
					duration: 150
				});
			}
		});
	};
	// Show match animation
	self.showMatch = function () {
		self.isMatched = true;
		// Pulse animation for matched cards
		tween(self, {
			scaleX: 1.1,
			scaleY: 1.1
		}, {
			duration: 200,
			onFinish: function onFinish() {
				tween(self, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 200
				});
			}
		});
	};
	// Handle card press
	self.down = function (x, y, obj) {
		self.flip();
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xFFFFFF
});
/**** 
* Game Code
****/ 
// Create back button
var backButton = new Text2("Back", {
	size: 60,
	fill: 0x000000 // Black color
});
backButton.anchor.set(0, 1);
backButton.x = 10;
backButton.y = 2732 - 10;
backButton.interactive = true;
backButton.down = function () {
	// Logic for going back a level or screen
	if (level > 1) {
		level--;
		startGame();
	}
};
LK.gui.bottomLeft.addChild(backButton);
// Create next button
var nextButton = new Text2("Next", {
	size: 60,
	fill: 0x000000 // Black color
});
nextButton.anchor.set(1, 1);
nextButton.x = 2048 - 10;
nextButton.y = 2732 - 10;
nextButton.interactive = true;
nextButton.down = function () {
	// Logic for going to the next level or screen
	if (level < 5) {
		level++;
		startGame();
	}
};
LK.gui.bottomRight.addChild(nextButton);
var background = LK.getAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
game.addChild(background);
// Game variables
// Initialize animal images
var level = storage.currentLevel || 1;
var rows = 3;
var cols = 4;
var cardSpacing = 30;
var cardWidth = 300;
var cardHeight = 400;
var flippedCards = [];
var matchedPairs = 0;
var requiredPairs = 0;
var gameActive = false;
var timeLeft = 60;
var timeWarningGiven = false;
var gameTimer = null;
var cards = [];
var symbols = [];
var soundMap = {
	'A': 'dog',
	'B': 'cat',
	'C': 'elephant',
	'D': 'monkey',
	'E': 'lion',
	'F': 'bird',
	'G': 'frog',
	'H': 'dolphin'
};
// Difficulty settings based on level
function setDifficulty() {
	if (level === 1) {
		rows = 3;
		cols = 4;
		timeLeft = 60;
	} else if (level === 2) {
		rows = 4;
		cols = 4;
		timeLeft = 75;
	} else {
		rows = 4;
		cols = 5;
		timeLeft = 90;
	}
	requiredPairs = rows * cols / 2;
}
// Create symbols array
function createSymbols() {
	var availableSymbols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
	symbols = [];
	// Create pairs of symbols
	for (var i = 0; i < requiredPairs; i++) {
		var symbol = availableSymbols[i];
		symbols.push(symbol);
		symbols.push(symbol);
	}
	// Shuffle
	shuffleArray(symbols);
}
// Fisher-Yates shuffle algorithm
function shuffleArray(array) {
	for (var i = array.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
}
// Create game board
function createBoard() {
	var boardWidth = cols * cardWidth + (cols - 1) * cardSpacing;
	var boardHeight = rows * cardHeight + (rows - 1) * cardSpacing;
	var startX = (2048 - boardWidth) / 2 + cardWidth / 2;
	var startY = (2732 - boardHeight) / 2 + cardHeight / 2;
	var index = 0;
	for (var r = 0; r < rows; r++) {
		for (var c = 0; c < cols; c++) {
			var x = startX + c * (cardWidth + cardSpacing);
			var y = startY + r * (cardHeight + cardSpacing);
			var symbol = symbols[index];
			var soundId = soundMap[symbol] || null;
			var imageId = 'animal_' + symbol; // Assuming images are named like 'animal_A', 'animal_B', etc.
			var card = new Card(imageId, soundId);
			card.x = x;
			card.y = y;
			game.addChild(card);
			cards.push(card);
			index++;
		}
	}
}
// Check for match when two cards are flipped
function checkForMatch() {
	var card1 = flippedCards[0];
	var card2 = flippedCards[1];
	LK.setTimeout(function () {
		if (card1.symbol === card2.symbol) {
			// Match found
			LK.getSound('match').play();
			card1.showMatch();
			card2.showMatch();
			matchedPairs++;
			// Update score
			LK.setScore(LK.getScore() + 100);
			scoreText.setText(LK.getScore());
			// Check if all pairs are matched
			if (matchedPairs === requiredPairs) {
				gameWon();
			}
		} else {
			// No match, flip cards back
			LK.getSound('nomatch').play();
			card1.reset();
			card2.reset();
		}
		flippedCards = [];
	}, 1000);
}
// Timer update
function updateTimer() {
	timeLeft--;
	timerText.setText("Time: " + timeLeft);
	// Time running out warning
	if (timeLeft <= 10 && !timeWarningGiven) {
		timeWarningGiven = true;
		timerText.setStyle({
			fill: 0xE74C3C
		});
		LK.getSound('timewarning').play();
	}
	// Game over if time runs out
	if (timeLeft <= 0) {
		gameOver();
	}
}
// Game won
function gameWon() {
	LK.clearInterval(gameTimer);
	gameActive = false;
	// Add time bonus
	var timeBonus = timeLeft * 10;
	LK.setScore(LK.getScore() + timeBonus);
	scoreText.setText(LK.getScore());
	// Update high score
	if (LK.getScore() > storage.highScore) {
		storage.highScore = LK.getScore();
	}
	// Update level
	level++;
	if (level > 5) {
		level = 1; // Reset to level 1 after level 5
	}
	storage.currentLevel = level;
	// Play win sound
	LK.getSound('win').play();
	// Show success message
	LK.showYouWin();
}
// Game over
function gameOver() {
	LK.clearInterval(gameTimer);
	gameActive = false;
	// Show game over screen
	LK.showGameOver();
}
// Start a new game
function startGame() {
	// Reset game state
	matchedPairs = 0;
	flippedCards = [];
	timeWarningGiven = false;
	LK.setScore(0);
	scoreText.setText("0");
	// Clear previous game
	for (var i = 0; i < cards.length; i++) {
		cards[i].destroy();
	}
	cards = [];
	// Set up new game
	setDifficulty();
	createSymbols();
	createBoard();
	// Reset timer
	timerText.setText("Time: " + timeLeft);
	timerText.setStyle({
		fill: 0xFFFFFF
	});
	// Start timer
	if (gameTimer) {
		LK.clearInterval(gameTimer);
	}
	gameTimer = LK.setInterval(updateTimer, 1000);
	// Start game
	gameActive = true;
	// Play background music
	LK.playMusic('bgmusic', {
		fade: {
			start: 0,
			end: 0.3,
			duration: 1000
		}
	});
}
// Create UI elements
var titleText = new Text2("Crazy Card Match", {
	size: 80,
	fill: 0x000000
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
titleText.y = 50;
var scoreText = new Text2("0", {
	size: 60,
	fill: 0x000000
});
scoreText.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreText);
scoreText.x = -10;
scoreText.y = 50;
var timerText = new Text2("Time: 60", {
	size: 60,
	fill: 0x000000
});
timerText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(timerText);
timerText.y = -10;
var levelText = new Text2("Level: " + level, {
	size: 60,
	fill: 0x000000
});
levelText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(levelText);
levelText.y = -100;
// Start the game
LK.setTimeout(function () {
	startGame();
}, 500);
// Main game update loop
game.update = function () {
	// Update level text (in case level changes)
	levelText.setText("Level: " + level);
};
:quality(85)/https://cdn.frvr.ai/67f12d34f88b0ea922fb718a.png%3F3) 
 sheep. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f12f71e23adb9492ed8808.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67f1308816ec5fd5078e3e3f.png%3F3) 
 generate a cute cat image. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f130ce16ec5fd5078e3e49.png%3F3) 
 generate a elephant. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f1315116ec5fd5078e3e6f.png%3F3) 
 generate a monkey. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f1317c16ec5fd5078e3e79.png%3F3) 
 lion. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f131ca16ec5fd5078e3e84.png%3F3) 
 a colorful parrot. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f1323e16ec5fd5078e3ea5.png%3F3) 
 generate a butterfly. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f1327916ec5fd5078e3eb3.png%3F3) 
 dolphin. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows