-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (112 loc) · 2.84 KB
/
index.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
window.addEventListener("load", function() {
function redRectangle() {
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 50, 50);
}
function whiteRectangle() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 50, 50);
}
function redTriangle() {
ctx.fillStyle = "red";
ctx.moveTo(0, 0);
ctx.lineTo(40, 40);
ctx.lineTo(0, 80);
ctx.fill();
}
function whiteTriangle() {
ctx.fillStyle = "white";
ctx.moveTo(0, 0);
ctx.lineTo(40, 40);
ctx.lineTo(0, 80);
ctx.fill();
}
function clear() {
ctx.clearRect(0, 0, 800, 750);
drawRandomShape();
}
function drawRandomShape(ctx) {
var rand = Math.floor(Math.random() * 20);
if (rand > 15) {
redRectangle();
expectedKey = 40;
} else if (rand > 10 && rand <= 15) {
whiteTriangle();
expectedKey = 38;
} else if (rand > 5 && rand <= 10) {
redTriangle();
expectedKey = 37;
} else {
whiteRectangle()
expectedKey = 39;
}
}
function drawGameStartText() {
ctx.font = "20px veranda";
ctx.fillStyle = "blue";
ctx.textAlign = "center";
ctx.fillText = "white"
}
function restartGame(ctx, width, height) {}
var canvas = document.getElementById("shapes-game"),
height = canvas.scrollHeight,
width = canvas.scrollWidth,
gameOn = false,
expectedKey = undefined,
ctx = canvas.getContext('2d'),
// white triangle = up, red square = down,
// red triangle = left, white square = right
expectedKeysMap = {
white0: 38,
red1: 40,
red0: 37,
white1: 39
},
timerSpan = document.getElementById("time-remaining"),
scoreSpan = document.getElementById("score-val"),
seconds = 30,
intervalId;
canvas.width = width;
canvas.height = height;
document.addEventListener("keydown", function(up) {
});
drawRandomShape()
document.addEventListener("keydown", function(x) {
if (x.keyCode == 32) {
clear();
}
if (x.keyCode == 38) {
if (expectedKey === x.keyCode) {
scoreSpan.innerHTML = Number(scoreSpan.innerHTML) + 1;
clear();
} else {
scoreSpan.innerHTML -= 1;
clear();
}
} else if (x.keyCode == 39) {
if (expectedKey === x.keyCode) {
scoreSpan.innerHTML = Number(scoreSpan.innerHTML) + 1
clear();
} else {
scoreSpan.innerHTML -= 1;
clear();
}
} else if (x.keyCode == 40) {
if (expectedKey === x.keyCode) {
scoreSpan.innerHTML = Number(scoreSpan.innerHTML) + 1;
clear();
} else {
scoreSpan.innerHTML -= 1;
clear();
}
} else if (x.keyCode == 37) {
if (expectedKey === x.keyCode) {
scoreSpan.innerHTML = Number(scoreSpan.innerHTML) + 1;
clear();
} else {
scoreSpan.innerHTML -= 1;
clear();
}
}
});
});