-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
93 lines (69 loc) · 2.63 KB
/
logic.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
document.addEventListener("keydown", startGame);
var botom = "./image/bottom.png";
var left = "./image/left.png";
var right = "./image/right.png";
var topimg = "./image/top.png";
var arrowKeys = [botom, left, right, topimg];
let arrowKeyCode;
var video = document.getElementsByTagName("video")[0];
var img = document.createElement("img");
let winCounter = 0;
let seconds = 0;
let timerId;
function starTimer() {
timerId = setInterval(function(){
seconds++;
document.getElementById("tiempo").innerHTML = "Tiempo: " + seconds + " segundos";
}, 1000);
}
function stopTimer() {
clearInterval(timerId);
}
function startGame(event) {
if (event.keyCode == 32 ) {
starTimer();
document.body.style.backgroundColor = "white";
document.removeEventListener("keydown", startGame);
video.parentNode.replaceChild(img, video);
showArrow();
document.addEventListener("keydown", playGame);
}
}
function playGame(event) {
if(event.code == "ArrowLeft" && arrowKeyCode == left) {
winCounter += 1;
document.getElementById("container").style.backgroundColor = "green";
document.getElementById("intentos").innerHTML = "Intentos: "+winCounter;
} else if(event.code == "ArrowUp" && arrowKeyCode == topimg) {
winCounter += 1;
document.getElementById("container").style.backgroundColor = "green";
document.getElementById("intentos").innerHTML = "Intentos: "+winCounter;
} else if(event.code == "ArrowRight" && arrowKeyCode == right) {
winCounter += 1;
document.getElementById("container").style.backgroundColor = "green";
document.getElementById("intentos").innerHTML = "Intentos: "+winCounter;
} else if(event.code == "ArrowDown"&& arrowKeyCode == botom) {
winCounter += 1;
document.getElementById("container").style.backgroundColor = "green";
document.getElementById("intentos").innerHTML = "Intentos: "+winCounter;
} else {
stopTimer();
winCounter = 0;
document.getElementById("container").style.backgroundColor = "red";
document.getElementById("intentos").innerHTML = "Intentos: "+winCounter;
}
if (winCounter === 3) {
document.getElementById("intentos").innerHTML = "Intentos: "+winCounter;
stopTimer();
alert("¿Has ganado!");
document.removeEventListener("keydown", playGame);
} else {
showArrow();
}
}
function showArrow() {
const randomArrow = Math.floor(Math.random() * arrowKeys.length);
arrowKeyCode = arrowKeys[randomArrow];
img.src = arrowKeyCode;
img.width = 200;
}