-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
167 lines (144 loc) · 3.42 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
160
161
162
163
164
165
166
167
const canvas=document.getElementById("pong");
const context=canvas.getContext("2d");
let paddlesound=new Audio();
let wallsound=new Audio();
let scoresound=new Audio();
paddlesound.src="sounds/paddle.mp3";
wallsound.src="sounds/wall.mp3";
scoresound.src="sounds/score.mp3";
const user={
x:50,
y:(canvas.height-100)/2,
width:10,
height:70,
color:"#8A2BE2",
score:0
}
const cpu={
x:canvas.width-50,
y:(canvas.height-100)/2,
width:10,
height:70,
score:0,
color:"#8A2BE2"
}
const ball={
x:canvas.width/2,
y:canvas.height/2,
radius:9,
speed:7,
velocityx:5,
velocityy:5,
color:"#8A2BE2"
}
const net={
x:canvas.width/2-1,
y:0,
width:1,
height:8,
color:"#8A2BE2"
}
function drawnet(){
for(let i=0;i<=canvas.height;i+=15)
{
drawrect(net.x,net.y+i,net.width,net.height,net.color);
}
}
function drawrect(x,y,w,h,color){
context.fillStyle=color;
context.fillRect(x,y,w,h);
}
function drawball(x,y,r,color)
{
context.fillStyle=color;
context.beginPath();
context.arc(x,y,r,0,Math.PI*2,true);
context.closePath();
context.fill();
}
function drawtext(text,x,y,color){
context.fillStyle=color;
context.font="75px fantasy";
context.fillText(text,x,y);
}
function render(){
drawrect(0,0,canvas.width,canvas.height,"#282828");
drawnet();
drawtext(user.score,canvas.width/4,canvas.height/5,"#8A2BE2");
drawtext(cpu.score,3*canvas.width/4,canvas.height/5,"#8A2BE2");
drawrect(user.x,user.y,user.width,user.height,user.color);
drawrect(cpu.x,cpu.y,cpu.width,cpu.height,cpu.color);
drawball(ball.x,ball.y,ball.radius,ball.color);
}
canvas.addEventListener("mousemove",movePaddle);
function movePaddle(event)
{
let rect=canvas.getBoundingClientRect();
user.y=event.clientY-rect.top-user.height/2;
}
function collision(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 b.right>p.left && b.bottom>p.top && b.left< p.right && b.top<p.bottom;
}
function resetBall(){
ball.x=canvas.width/2;
ball.y=canvas.height/2;
ball.speed=7;
ball.velocityx=-ball.velocityx;
}
function update(){
ball.x += ball.velocityx;
ball.y += ball.velocityy;
cpu.y+=(ball.y-(cpu.y+cpu.height/2))*0.1;
if(ball.y + ball.radius>canvas.height || ball.y-ball.radius <0){
wallsound.play();
ball.velocityy=-ball.velocityy;
}
let player=(ball.x<canvas.width/2)?user:cpu;
if(collision(ball,player))
{
paddlesound.play();
let collidePoint=ball.y-(player.y+player.height/2);
collidePoint=collidePoint/(player.height/2);
let angleRad=collidePoint*Math.PI/4;
let direction=(ball.x<canvas.width/2)?1:-1;
ball.velocityx=direction*ball.speed*Math.cos(angleRad);
ball.velocityy=ball.speed*Math.sin(angleRad);
ball.speed+=0.2;
}
if(ball.x-ball.radius<0)
{
cpu.score++;
if(cpu.score==11)
{
user.score=0;
cpu.score=0;
}
scoresound.play();
resetBall();
}
else if(ball.x+ball.radius>canvas.width)
{
user.score++;
if(user.score==11)
{
user.score=0;
cpu.score=0;
}
scoresound.play();
resetBall();
}
}
function gameinit(){
update();
render();
}
const framePerSecond=50;
setInterval(gameinit,1000/framePerSecond);