/**** * Classes ****/ // Assets will be automatically created based on usage in the code. // Card class var Card = Container.expand(function () { var self = Container.call(this); // Card front (image) and back (number) sides var cardFront, cardBack; var isFront = true; // Track if the card's front side is currently shown // Initialize card with an image and a number self.init = function (imageId, number) { cardFront = self.attachAsset(imageId, { anchorX: 0.5, anchorY: 0.5 }); cardBack = new Text2(number.toString(), { size: 200, fill: "#ffffff", anchorX: 0.5, anchorY: 0.5 }); self.addChild(cardBack); cardBack.visible = false; // Initially, show the image side }; // Flip the card self.flip = function () { if (cardFront && cardBack) { if (isFront) { cardFront.visible = false; cardBack.visible = true; } else { cardFront.visible = true; cardBack.visible = false; } isFront = !isFront; } }; }); // Shuffle Button class var ShuffleButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('button', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { shuffleCards(); }); }); /**** * Initialize Game ****/ // Function to shuffle cards var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Global variables var cards = []; // Array to hold card instances var cardStack = []; // Array to hold the stack of cards var cardData = [ // Example card data { id: 'card1', number: 1900 }, { id: 'card2', number: 2014 }, { id: 'card3', number: 1955 }, { id: 'card4', number: 2022 }]; // Function to initialize cards function initCards() { var cardWidth = 200; var cardHeight = 300; var startX = (2048 - (cardWidth * cardData.length + 100 * (cardData.length - 1))) / 2; // Center cards horizontally var startY = (2732 - cardHeight) / 2; // Center cards vertically for (var i = 0; i < cardData.length; i++) { var card = new Card(); card.init(cardData[i].id, cardData[i].number); if (i == 0) { // Position the first card in the middle of the screen card.x = 2048 / 2; card.y = 2732 / 2; cards.push(card); } else { // Stack the other cards in a corner card.x = 0; card.y = 0; cardStack.push(card); } game.addChild(card); // Removed card flip event listener from here } } // Initialize cards when the game starts initCards(); // Initialize shuffle button var shuffleButton = new ShuffleButton(); shuffleButton.x = 2048 / 2; shuffleButton.y = 2732 - 200; // Position the button at the bottom of the screen game.addChild(shuffleButton); // Function to shuffle cards function shuffleCards() { if (cardStack.length > 0) { // Remove the current card from the middle of the screen var currentCard = cards.pop(); currentCard.x = 0; currentCard.y = 0; cardStack.push(currentCard); // Pick a random card from the stack var randomIndex = Math.floor(Math.random() * cardStack.length); var newCard = cardStack.splice(randomIndex, 1)[0]; newCard.x = 2048 / 2; newCard.y = 2732 / 2; cards.push(newCard); // Ensure only one event listener is bound by removing any existing ones before adding a new one newCard.off('down').on('down', function (obj) { newCard.flip(); }); // Move the picked card to the middle of the screen newCard.x = 2048 / 2; newCard.y = 2732 / 2; } } // Main game tick function LK.on('tick', function () { // Game logic that needs to be updated every frame can go here });
/****
* Classes
****/
// Assets will be automatically created based on usage in the code.
// Card class
var Card = Container.expand(function () {
var self = Container.call(this);
// Card front (image) and back (number) sides
var cardFront, cardBack;
var isFront = true; // Track if the card's front side is currently shown
// Initialize card with an image and a number
self.init = function (imageId, number) {
cardFront = self.attachAsset(imageId, {
anchorX: 0.5,
anchorY: 0.5
});
cardBack = new Text2(number.toString(), {
size: 200,
fill: "#ffffff",
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(cardBack);
cardBack.visible = false; // Initially, show the image side
};
// Flip the card
self.flip = function () {
if (cardFront && cardBack) {
if (isFront) {
cardFront.visible = false;
cardBack.visible = true;
} else {
cardFront.visible = true;
cardBack.visible = false;
}
isFront = !isFront;
}
};
});
// Shuffle Button class
var ShuffleButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
self.on('down', function (obj) {
shuffleCards();
});
});
/****
* Initialize Game
****/
// Function to shuffle cards
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Global variables
var cards = []; // Array to hold card instances
var cardStack = []; // Array to hold the stack of cards
var cardData = [
// Example card data
{
id: 'card1',
number: 1900
}, {
id: 'card2',
number: 2014
}, {
id: 'card3',
number: 1955
}, {
id: 'card4',
number: 2022
}];
// Function to initialize cards
function initCards() {
var cardWidth = 200;
var cardHeight = 300;
var startX = (2048 - (cardWidth * cardData.length + 100 * (cardData.length - 1))) / 2; // Center cards horizontally
var startY = (2732 - cardHeight) / 2; // Center cards vertically
for (var i = 0; i < cardData.length; i++) {
var card = new Card();
card.init(cardData[i].id, cardData[i].number);
if (i == 0) {
// Position the first card in the middle of the screen
card.x = 2048 / 2;
card.y = 2732 / 2;
cards.push(card);
} else {
// Stack the other cards in a corner
card.x = 0;
card.y = 0;
cardStack.push(card);
}
game.addChild(card);
// Removed card flip event listener from here
}
}
// Initialize cards when the game starts
initCards();
// Initialize shuffle button
var shuffleButton = new ShuffleButton();
shuffleButton.x = 2048 / 2;
shuffleButton.y = 2732 - 200; // Position the button at the bottom of the screen
game.addChild(shuffleButton);
// Function to shuffle cards
function shuffleCards() {
if (cardStack.length > 0) {
// Remove the current card from the middle of the screen
var currentCard = cards.pop();
currentCard.x = 0;
currentCard.y = 0;
cardStack.push(currentCard);
// Pick a random card from the stack
var randomIndex = Math.floor(Math.random() * cardStack.length);
var newCard = cardStack.splice(randomIndex, 1)[0];
newCard.x = 2048 / 2;
newCard.y = 2732 / 2;
cards.push(newCard);
// Ensure only one event listener is bound by removing any existing ones before adding a new one
newCard.off('down').on('down', function (obj) {
newCard.flip();
});
// Move the picked card to the middle of the screen
newCard.x = 2048 / 2;
newCard.y = 2732 / 2;
}
}
// Main game tick function
LK.on('tick', function () {
// Game logic that needs to be updated every frame can go here
});