-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddlesUpDown.js
73 lines (68 loc) · 2.41 KB
/
paddlesUpDown.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
// Keydown movement function for W, S, (paddle1) Up Arrow, Down Arrow (paddle2)
document.addEventListener('keydown', function (e) {
if (e.keyCode == 87 || e.which == 87) { // W key
paddle1Position -= 20;
document.getElementById("paddle1").style.top = (paddle1Position) + "px";
}
if (e.keyCode == 83 || e.which == 83) { // S key
paddle1Position += 20;
document.getElementById("paddle1").style.top = (paddle1Position) + "px";
}
if (e.keyCode == 38 || e.which == 38) { // up arrow key
paddle2Position -= 20;
document.getElementById("paddle2").style.top = (paddle2Position) + "px";
}
if (e.keyCode == 40 || e.which == 40) { // down arrow key
paddle2Position += 20;
document.getElementById("paddle2").style.top = (paddle2Position) + "px";
}
}, false);
// Keydown movement speed for each paddle
document.addEventListener('keydown', function (e) {
if (e.keyCode == 87 || e.which == 87) { // W key
paddleSpeed1 = -20;
}
if (e.keyCode == 83 || e.which == 83) { // S key
paddleSpeed1 = 20;
}
if (e.keyCode == 38 || e.which == 38) { // up arrow key
paddleSpeed2 = -20;
}
if (e.keyCode == 40 || e.which == 40) { // down arrow key
paddleSpeed2 = 20;
}
}, false);
// setInterval function for paddles speed and height barrier
window.setInterval(function show() {
paddle1Position += paddleSpeed1;
paddle2Position += paddleSpeed2;
if (paddle1Position <= 150) {
paddle1Position = 150;
}
if (paddle2Position <= 150) {
paddle2Position = 150;
}
if (paddle1Position >= window.innerHeight - paddleHeight) {
paddle1Position = window.innerHeight - paddleHeight;
}
if (paddle2Position >= window.innerHeight - paddleHeight) {
paddle2Position = window.innerHeight - paddleHeight;
}
document.getElementById("paddle1").style.top = (paddle1Position) + "px";
document.getElementById("paddle2").style.top = (paddle2Position) + "px";
}, 1000 / 60);
// Function to stop paddles when key is let go
document.addEventListener('keyup', function (e) {
if (e.keyCode == 87 || e.which == 87) {
paddleSpeed1 = 0;
}
if (e.keyCode == 83 || e.which == 83) {
paddleSpeed1 = 0;
}
if (e.keyCode == 38 || e.which == 38) {
paddleSpeed2 = 0;
}
if (e.keyCode == 40 || e.which == 40) {
paddleSpeed2 = 0;
}
}, false);