-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
159 lines (109 loc) · 4.13 KB
/
script.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
var ball = document.getElementById('ball');
var rod1 = document.getElementById('rod1');
var rod2 = document.getElementById('rod2');
const storeName = "PPName";
const storeScore = "PPMaxScore";
const rod1Name = "Rod 1";
const rod2Name = "Rod 2";
let score,
maxScore,
movement,
rod,
ballSpeedX = 2,
ballSpeedY = 2;
let gameOn = false;
let windowWidth = window.innerWidth,
windowHeight = window.innerHeight;
(function () {
rod = localStorage.getItem(storeName);
maxScore = localStorage.getItem(storeScore);
if (rod === "null" || maxScore === "null") {
alert("This is the first time you are playing this game. LET'S START");
maxScore = 0;
rod = "Rod1"
} else {
alert(rod + " has maximum score of " + maxScore * 100);
}
resetBoard(rod);
})();
function resetBoard(rodName) {
rod1.style.left = (window.innerWidth - rod1.offsetWidth) / 2 + 'px';
rod2.style.left = (window.innerWidth - rod2.offsetWidth) / 2 + 'px';
ball.style.left = (windowWidth - ball.offsetWidth) / 2 + 'px';
// Lossing player gets the ball
if (rodName === rod2Name) {
ball.style.top = (rod1.offsetTop + rod1.offsetHeight) + 'px';
ballSpeedY = 2;
} else if (rodName === rod1Name) {
ball.style.top = (rod2.offsetTop - rod2.offsetHeight) + 'px';
ballSpeedY = -2;
}
score = 0;
gameOn = false;
}
function storeWin(rod, score) {
if (score > maxScore) {
maxScore = score;
localStorage.setItem(storeName, rod);
localStorage.setItem(storeScore, maxScore);
}
clearInterval(movement);
resetBoard(rod);
alert(rod + " wins with a score of " + (score * 100) + ". Max score is: " + (maxScore * 100));
}
window.addEventListener('keypress', function () {
let rodSpeed = 20;
let rodRect = rod1.getBoundingClientRect();
if (event.code === "KeyD" && ((rodRect.x + rodRect.width) < window.innerWidth)) {
rod1.style.left = (rodRect.x) + rodSpeed + 'px';
rod2.style.left = rod1.style.left;
} else if (event.code === "KeyA" && (rodRect.x > 0)) {
rod1.style.left = (rodRect.x) - rodSpeed + 'px';
rod2.style.left = rod1.style.left;
}
if (event.code === "Enter") {
if (!gameOn) {
gameOn = true;
let ballRect = ball.getBoundingClientRect();
let ballX = ballRect.x;
let ballY = ballRect.y;
let ballDia = ballRect.width;
let rod1Height = rod1.offsetHeight;
let rod2Height = rod2.offsetHeight;
let rod1Width = rod1.offsetWidth;
let rod2Width = rod2.offsetWidth;
movement = setInterval(function () {
// Move ball
ballX += ballSpeedX;
ballY += ballSpeedY;
rod1X = rod1.getBoundingClientRect().x;
rod2X = rod2.getBoundingClientRect().x;
ball.style.left = ballX + 'px';
ball.style.top = ballY + 'px';
if ((ballX + ballDia) > windowWidth || ballX < 0) {
ballSpeedX = -ballSpeedX; // Reverses the direction
}
// It specifies the center of the ball on the viewport
let ballPos = ballX + ballDia / 2;
// Check for Rod 1
if (ballY <= rod1Height) {
ballSpeedY = -ballSpeedY; // Reverses the direction
score++;
// Check if the game ends
if ((ballPos < rod1X) || (ballPos > (rod1X + rod1Width))) {
storeWin(rod2Name, score);
}
}
// Check for Rod 2
else if ((ballY + ballDia) >= (windowHeight - rod2Height)) {
ballSpeedY = -ballSpeedY; // Reverses the direction
score++;
// Check if the game ends
if ((ballPos < rod2X) || (ballPos > (rod2X + rod2Width))) {
storeWin(rod1Name, score);
}
}
}, 10);
}
}
});