-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
160 lines (146 loc) · 5.07 KB
/
app.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const game = () => {
let pScore = 0;
let cScore = 0;
const startGame = () => {
const playBtn = document.querySelector(".intro button");
const introScreen = document.querySelector(".intro");
const match = document.querySelector(".match");
playBtn.addEventListener("click", () => {
introScreen.classList.add("fadeOut");
match.classList.add("fadeIn");
});
};
//Play Match
const playMatch = () => {
const options = document.querySelectorAll(".options button");
const playerHand = document.querySelector(".player-hand");
const computerHand = document.querySelector(".computer-hand");
const hands = document.querySelectorAll(".hands img");
hands.forEach((hand) => {
hand.addEventListener("animationend", function () {
this.style.animation = "";
});
});
//Computer Options
const computerOptions = ["piedra", "papel", "tijera"];
options.forEach((option) => {
option.addEventListener("click", function () {
//computer Choice
const computerNumber = Math.floor(Math.random() * 3);
const computerChoice = computerOptions[computerNumber];
setTimeout(() => {
//Here is where we call compare hands
compareHands(this.textContent, computerChoice);
//Update Images
playerHand.src = `./assets/${this.textContent}.png`;
computerHand.src = `./assets/${computerChoice}.png`;
}, 2000);
playerHand.style.animation = "shakePlayer 2s ease";
computerHand.style.animation = "shakeComputer 2s ease";
});
});
};
const updateScore = () => {
const playerScore = document.querySelector(".player-score p");
const computerScore = document.querySelector(".computer-score p");
const playerHand = document.querySelector(".player-hand");
const computerHand = document.querySelector(".computer-hand");
playerScore.textContent = pScore;
computerScore.textContent = cScore;
};
const compareHands = (playerChoice, computerChoice) => {
const playerHand = document.querySelector(".player-hand");
const computerHand = document.querySelector(".computer-hand");
//Update Text
const winner = document.querySelector(".winner");
//Checking for a tie
if (playerChoice === computerChoice) {
playerHand.style.animation = "playerTie 250ms linear";
computerHand.style.animation = "computerTie 250ms linear";
winner.textContent = "Empate";
return;
}
//Check for piedra
if (playerChoice === "piedra") {
if (computerChoice === "tijera") {
winner.textContent = "Jugador Gana!!";
playerHand.style.animation = "playerWins 250ms linear";
computerHand.style.animation = "computerLose 250ms ease-out 250ms";
pScore++;
setInterval(createHeart, 300);
updateScore();
removeHeart();
return;
} else {
winner.textContent = "Gana Computadora";
computerHand.style.animation = "computerWins 250ms linear";
playerHand.style.animation = "playerLose 250ms ease-out 250ms";
cScore++;
updateScore();
return;
}
}
//Check for papel
if (playerChoice === "papel") {
if (computerChoice === "tijera") {
winner.textContent = "Gana Computadora";
computerHand.style.animation = "computerWins 250ms linear";
playerHand.style.animation = "playerLose 250ms ease-out 250ms";
cScore++;
updateScore();
return;
} else {
winner.textContent = "Jugador Gana!!";
playerHand.style.animation = "playerWins 250ms linear";
computerHand.style.animation = "computerLose 500ms ease-out";
pScore++;
setInterval(createHeart, 300);
updateScore();
removeHeart();
return;
}
}
//Check for tijera
if (playerChoice === "tijera") {
if (computerChoice === "piedra") {
winner.textContent = "Gana Computadora";
computerHand.style.animation = "computerWins 250ms linear";
playerHand.style.animation = "playerLose 250ms ease-out 250ms";
cScore++;
return;
} else {
winner.textContent = "Jugador Gana!!";
playerHand.style.animation = "playerWins 250ms linear";
computerHand.style.animation = "computerLose 500ms ease-out";
pScore++;
setInterval(createHeart, 300);
updateScore();
removeHeart();
return;
}
}
};
startGame();
playMatch();
};
//start the game funcion
game();
function removeHeart() {
const corazones = document.querySelectorAll("div.heart");
console.log(corazones);
corazones.forEach((corazon) => {
console.log(corazon);
// corazon.addEventListener("animationend", function () {
corazon.style.animation = "";
corazon.remove();
// });
});
}
function createHeart() {
const heart = document.createElement("div");
heart.classList.add("heart");
heart.style.left = Math.random() * 100 + "vw";
heart.style.animationDuration = Math.random() * 2 + 3 + "s";
heart.innerText = "👏";
document.body.appendChild(heart);
}