-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
126 lines (109 loc) · 3.39 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
/*Selecting ball*/
let ball=document.querySelector(".ball");
/*Selecting board*/
let board =document.querySelector(".board");
/*Getting coordinates of the board*/
let bound=board.getBoundingClientRect();
let leftPaddle=document.querySelector(".left");
let rightPaddle=document.querySelector('.right');
let leftPlayerLife=3;
let rightPlayerLife=3;
let y=true;
let x=true;
/*Chnage color of lives*/
function setColor(indx){
let allIcon=document.querySelectorAll(".fas.fa-circle");
allIcon[indx].style.color="white";
}
/*EventListener for left and right paddle*/
document.addEventListener("keydown", ev => {
if(ev.key==='w'){
movePaddle(leftPaddle, -window.innerHeight*0.1)
}
else if (ev.key==='s') {
movePaddle(leftPaddle, window.innerHeight*0.1)
}
else if (ev.key==='ArrowUp') {
movePaddle(rightPaddle, -window.innerHeight*0.1)
}
else if (ev.key==='ArrowDown') {
movePaddle(rightPaddle, window.innerHeight*0.1)
}
});
/*Movement of paddle*/
function movePaddle(cPaddle, change){
let cPaddleBound=cPaddle.getBoundingClientRect();
/*Boundary condition*/
if(cPaddleBound.top+change>=bound.top && cPaddleBound.bottom+change<=bound.bottom){
cPaddle.style.top=cPaddleBound.top+change+"px";
}
}
/*Rest the game*/
function resetGame(){
ball.style.top=window.innerHeight*0.45+"px";
ball.style.left=window.innerWidth*0.45+"px";
requestAnimationFrame(moveBall);
}
function moveBall(){
/*Getting coordinates of the ball*/
let cordinate=ball.getBoundingClientRect();
let top=cordinate.top;
let left=cordinate.left;
let right=cordinate.right;
let bottom=cordinate.bottom;
//**********************No Collison between paddle and ball *************
/*If the ball misses the paddle*/
let hasTouchedLeft= left <= bound.left;
let hasTouchedRight=right >= bound.right;
if(hasTouchedLeft || hasTouchedRight){
if(hasTouchedLeft){
leftPlayerLife--;
setColor(leftPlayerLife);
if(leftPlayerLife==0){
alert("Game over! Player 2 wins");
document.location.reload();
}
else{
setTimeout(() => resetGame(), 1000);
return;
}
}else{
rightPlayerLife--;
setColor(rightPlayerLife+3);
if(rightPlayerLife==0){
alert("Game over! Player 1 wins");
document.location.reload();
}
else{
setTimeout(() => resetGame(), 1000);
return;
}
}
}
//***********************************************************
//*************************** Collison check **********************
let leftPaddleBound=leftPaddle.getBoundingClientRect();
let rightPaddleBound=rightPaddle.getBoundingClientRect();
if(left<=leftPaddleBound.right && right>=leftPaddleBound.left &&
top+20>=leftPaddleBound.top && bottom-20<=leftPaddleBound.bottom){
x=!x;
}
if(left<=rightPaddleBound.right && right>=rightPaddleBound.left &&
top+20>=rightPaddleBound.top && bottom-20<=rightPaddleBound.bottom){
x=!x;
}
//******************************************************************
/*Vertiacl check for ball*/
if(top<=bound.top || bottom>=bound.bottom){
y=!y;
}
/*Horizontal check for ball*/
if(left<=bound.left || right>=bound.right){
x=!x;
}
/*Ball free movement*/
ball.style.top= y===true? top+4+"px": top-4+"px";
ball.style.left= x===true? left+4+"px": left-4+"px";
requestAnimationFrame(moveBall);
}
requestAnimationFrame(moveBall);