xxxxxxxxxx
110
let cards = [];
let flippedCards = [];
let canvasSize = 400;
let cardSize = canvasSize / 4;
let matchedPairs = 0;
let lockBoard = false;
function setup() {
createCanvas(canvasSize, canvasSize);
let symbols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
symbols = symbols.concat(symbols); // Duplica os símbolos para formar pares
symbols = shuffle(symbols); // Embaralha os símbolos
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
let symbol = symbols.pop();
let card = new Card(i * cardSize, j * cardSize, cardSize, symbol);
cards.push(card);
}
}
}
function draw() {
background(255);
for (let card of cards) {
card.show();
}
if (flippedCards.length === 2 && !lockBoard) {
setTimeout(checkMatch, 1000); // Espera 1 segundo antes de verificar se as cartas são iguais
}
}
function mousePressed() {
for (let card of cards) {
if (card.contains(mouseX, mouseY) && flippedCards.length < 2 && !card.isFlipped && !lockBoard) {
card.flip();
flippedCards.push(card);
if (flippedCards.length === 2) {
if (flippedCards[0] && flippedCards[1] && flippedCards[0].symbol !== flippedCards[1].symbol) {
lockBoard = true;
setTimeout(function() {
if (flippedCards[0]) {
flippedCards[0].flip();
}
if (flippedCards[1]) {
flippedCards[1].flip();
}
flippedCards = [];
lockBoard = false;
}, 2000); // Espera 2 segundos antes de esconder as cartas novamente
}
}
break; // Adicionado para que apenas uma carta vire ao ser clicada
}
}
}
function checkMatch() {
if (flippedCards[0] && flippedCards[1] && flippedCards[0].symbol === flippedCards[1].symbol) {
matchedPairs++;
} else {
if (flippedCards[0]) {
flippedCards[0].flip();
}
if (flippedCards[1]) {
flippedCards[1].flip();
}
}
flippedCards = [];
if (matchedPairs === cards.length / 2) {
textSize(32);
fill(0);
textAlign(CENTER, CENTER);
text('Você ganhou!', canvasSize / 2, canvasSize / 2);
}
}
class Card {
constructor(x, y, size, symbol) {
this.x = x;
this.y = y;
this.size = size;
this.symbol = symbol;
this.isFlipped = false;
}
flip() {
this.isFlipped = !this.isFlipped;
}
contains(x, y) {
return x > this.x && x < this.x + this.size && y > this.y && y < this.y + this.size;
}
show() {
stroke(0);
strokeWeight(2);
fill(this.isFlipped ? 200 : 255);
rect(this.x, this.y, this.size, this.size, 10);
if (this.isFlipped) {
textSize(32);
textAlign(CENTER, CENTER);
fill(0);
text(this.symbol, this.x + this.size / 2, this.y + this.size / 2);
}
}
}