/
common.js
81 lines (71 loc) · 2.06 KB
/
common.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
function playSound(sound){
var audio = new Audio('sounds/' + sound + '.mp3');
audio.play();
}
function writeText(text) {
ctx.font = "30px Arial";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
}
function writePoints(text) {
ctx.font = "12px Arial";
ctx.textAlign = "left";
ctx.fillText(text, 12, canvas.height);
}
function writeSubText(text) {
ctx.font = "20px Arial";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 30);
}
function writeScore(text) {
ctx.font = "20px Arial";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, 20);
}
var initialSeconds = Math.floor(Date.now() / 1000)
function writeTime() {
var seconds = Math.floor(Date.now() / 1000) - initialSeconds;
ctx.font = "12px Arial";
ctx.textAlign = "right";
ctx.fillText(seconds, canvas.width - 12, canvas.height);
}
function clearCanvas(canvas, canvasContext) {
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
}
var controls = {
rightPressed: false,
leftPressed: false,
upPressed: false,
downPressed: false
}
console.log("listening to arrows");
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);
function keyDownHandler(e) {
if (e.keyCode == 39 || e.keyCode == 68) {
controls.rightPressed = true;
}
else if (e.keyCode == 37 || e.keyCode == 65) {
controls.leftPressed = true;
}
else if (e.keyCode == 38 || e.keyCode == 87) {
controls.upPressed = true;
}
else if (e.keyCode == 40 || e.keyCode == 83) {
controls.downPressed = true;
}
}
function keyUpHandler(e) {
if (e.keyCode == 39 || e.keyCode == 68) {
controls.rightPressed = false;
}
else if (e.keyCode == 37 || e.keyCode == 65) {
controls.leftPressed = false;
}
else if (e.keyCode == 38 || e.keyCode == 87) {
controls.upPressed = false;
}
else if (e.keyCode == 40 || e.keyCode == 83) {
controls.downPressed = false;
}
}