-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
218 lines (153 loc) · 3.87 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// DRAW CANVAS
const canvas=document.getElementById("myGame");
const context=canvas.getContext("2d");
// DRAW RECTANGLE FUNCTION
function drawrectangle(x,y,w,h,color){
context.fillStyle=color;// to specify colour
context.fillRect(x,y,w,h);// to specify coordinates and dimensions
}
/* computer paddle*/
const com={
x: canvas.width/2 - 50/2,
y:10,
width:50,
height:10,
color:"white",
score:0,
}
/* player paddle*/
const user={
x: canvas.width/2 - 50/2,
y:canvas.height-20,
width:50,
height:10,
color:"yellow",
score:0,
}
// CENTRE LINE
function centreline(){
context.beginPath();
context.setLineDash([10]);
context.moveTo(0,canvas.height/2);
context.lineTo(canvas.width,canvas.height/2);
context.strokeStyle="white";
context.stroke();
}
// CREATE BALL
function drawcirle(x,y,r,color){
context.beginPath()
context.fillStyle=color
context.arc(x,y,r,0,Math.PI*2,false)
context.closePath()
context.fill()
}
const ball={
x:canvas.width/2,
y:canvas.height/2,
color:"red",
radius:10,
speed:1,
velocityX:5,
velocityY:5,
}
// SCORES
function drawtext(text,x,y,color){
context.fillStyle=color
context.font="32px josefin sans"
context.fillText(text,x,y)
}
// CREATING RENDER FUNCTION
function render(){
// MAKE CANVAS
drawrectangle(0,0,400,600,"black");
// computer paadle
drawrectangle(com.x,com.y,com.width,com.height,com.color);
// player paddle
drawrectangle(user.x,user.y,user.width,user.height,user.color);
// centre line
centreline();
// create ball
drawcirle(ball.x,ball.y,ball.radius,ball.color);
// create ball
drawtext(com.score,20,canvas.height/2 -30,"white") // COMP score
drawtext(user.score,20,canvas.height/2 +50,"white") //USER SCORE
}
// collision detector
// b-ball
// p- peddle of either player or computer depend upong Y axis of ball
function coolision(b,p)
{
b.top=b.y-b.radius;
b.bottom=b.y+b.radius;
b.left=b.x-b.radius;
b.right=b.x+b.radius;
p.top=p.y;
p.bottom=p.y+p.height;
p.left=p.x;
p.right=p.x+p.width;
return p.right>b.left && p.left<b.right && b.bottom>p.top && b.top<p.bottom;
}
// control user paddle
canvas.addEventListener("mousemove",movepeddle);
function movepeddle(e){
let rect=canvas.getBoundingClientRect();
user.x=e.clientX - rect.left - user.width/2;
}
// RESET BALL
function resetball(){
ball.x=canvas.width/2;
ball.y=canvas.height/2;
ball.speed=1;
ball.velocityY=-ball.velocityY;
}
function gameover(){
// HIDE CANVAS
canvas.style.display="none";
const can=document.getElementById("can");
can.style.display="none";
// GAME OVER
const result=document.getElementById("result");
result.style.display="block";
}
// UPDATE
function update(){
ball.x+=ball.velocityX * ball.speed ;
ball.y+=ball.velocityY * ball.speed;
// computer paddle
let computerlevel=0.1;
com.x+= (ball.x -( com.x + com.width/2 ) )+computerlevel;
if(ball.speed>2){
com.x+=ball.x+10;
}
// reflect
if(ball.x+ball.radius>canvas.width || ball.x-ball.radius<0){
ball.velocityX=-ball.velocityX;
}
// collision
let p=(ball.y<canvas.height/2) ? com : user ; // checking for paddle on basis of postion of ball w.r.t canvas height
if(coolision(ball,p)){
ball.velocityY=-ball.velocityY;
ball.speed+=0.1;
}
// points
if(ball.y-ball.radius<0){
user.score++
resetball()
}
else if(ball.y + ball.radius> canvas.height){
com.score++
resetball()
}
// GAME OVER
if(user.score>=4 || com.score>=4){
gameover();
clearInterval(loop);
}
}
// start the game
function start(){
update();
render();
}
//loop
const loop=setInterval(start,1000/50);