-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
146 lines (137 loc) · 4.56 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pokémemory</title>
<link rel="stylesheet" href="style.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900&display=swap"
rel="stylesheet"
/>
<script src="./js/lib/jquery-3.7.1.min.js"></script>
</head>
<body>
<header>
<h1>Pokémemory</h1>
<nav>
<a href="/">Pokédex</a>
<!-- <a href="/">Pokéduel</a> -->
</nav>
</header>
<main>
<div id="win-text"></div>
<div class="main-container"></div>
<div class="main-bottom">
<span id="moves">Moves</span>
<button onclick="window.location = '/'">Restart</button>
</div>
</main>
<footer>
Build with Jquery, HTML and CSS
<br />
<a
href="https://github.com/fabriciovo/jquery-pokememory"
target="_blank"
className="underline"
>Repository Link</a
>
</footer>
<script>
$(document).ready(function () {
const apiUrl =
"https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json";
const selectedCard = [];
const pairs = 6;
var moves = 0;
var pairCount = 0;
var canSelectCard = true;
function createCard(pokemon) {
return `
<div class="card" data-id="${pokemon.id}">
<div class="pokeball"></div>
<img src="${pokemon.img}" class="pokemon-img">
</div>
`;
}
function win() {
console.log($("confetti"));
$.confetti.start();
return `
<h2>Congratulations you matched all pairs!</h2>
`;
}
function handleCardClick(card) {
if (!canSelectCard) return;
const $card = $(card);
if ($card.hasClass("card-green")) return;
$card.removeClass("card-rotate-reverse");
$card.toggleClass("card-rotate");
$card.find(".pokeball").fadeOut("slow", function () {
$card.find(".pokemon-img").fadeIn("slow");
});
selectedCard.push($card);
if (selectedCard.length === 2) {
checkForMatch();
}
}
function checkForMatch() {
canSelectCard = false;
const firstCard = selectedCard[0];
const secondCard = selectedCard[1];
moves++;
$("#moves").text("Moves: " + moves);
const firstCardImg = firstCard.find("img").attr("src");
const secondCardImg = secondCard.find("img").attr("src");
if (firstCardImg === secondCardImg) {
selectedCard.length = 0;
canSelectCard = true;
pairCount++;
setTimeout(() => {
firstCard.addClass("card-green");
secondCard.addClass("card-green");
}, 1000);
console.log(pairCount);
if (pairCount == 6) {
$("#win-text").prepend(win());
}
} else {
setTimeout(() => {
selectedCard.forEach((card) => {
card.shake(20, 2, 0.5, function () {
card.removeClass("card-rotate");
card.toggleClass("card-rotate-reverse");
card.find(".pokemon-img").fadeOut("slow", function () {
card.find(".pokeball").fadeIn("slow");
selectedCard.length = 0;
canSelectCard = true;
});
});
});
}, 1000);
}
}
$.getJSON(apiUrl, function (data) {
const shuffledPokemon = data.pokemon
.sort(() => 0.5 - Math.random())
.slice(0, pairs);
const duplicated = [...shuffledPokemon, ...shuffledPokemon];
const cardsHtml = duplicated
.map((pokemon) => createCard(pokemon))
.join("");
$(".main-container").html(cardsHtml);
$(".pokemon-img").hide();
$(".card").on("click", function () {
handleCardClick(this);
});
}).fail(function () {
alert("Failed to load the API! Please reload the page.");
});
});
</script>
<script src="./js/jquery-shake.js"></script>
<script src="./js/jquery-confetti.js"></script>
</body>
</html>