forked from kokonior/Javascript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping-pong.html
205 lines (176 loc) · 7.54 KB
/
ping-pong.html
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
<!DOCTYPE html>
<html>
<head>
<title>Javascript based Ping Pong Game</title>
<style>
/**************** Basic css styling of the canvas starts**************/
*{box-sizing: border-box;}
body{margin:0;
background: #06c05a;
display:flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
#myCanvas{
box-shadow: 0 0 14px 6px rgba(0,0,0,.6);
border-radius: 20px;
}
.instructions{ margin:10px;
color:#0B0500;
font-size: 20px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
text-align: center;
}
#myCanvas{margin-left:20px;}
/**************** Basic css styling of the canvas ends**************/
</style>
</head>
<body>
<canvas id=myCanvas width = 700 height=500></canvas> <!-- Making canvas element in body of html-->
<div class=instructions>
<p>Welcome to Ping Pong</p>
<p>The Instructions are as follows.</p>
<p>Your paddle is mouse controlled.
Whereas Player 2 is AI powered</p>
<p>First to 5 points wins the game</p>
<p>Click to Start Playing the Game</p>
</div>
</body>
<!---- HTML ends------->
<!------------ Script begins ------------>
<script>
const canvas = document.getElementById('myCanvas'); //getting canvas element
const ctx = canvas.getContext('2d'); //canvas declaration
let paddle1Y = 10, paddle2Y = 10;; //Control Variables
const paddle_width = 10, paddle_height = 100;
const radius =10; // Radius of the Ball
let ballX = canvas.width/2, ballY = canvas.height/2; // Ball begins in the middle of the canvas
let speedX = 6, speedY = 6;
let player1 =0, player2 =0;
var gameStatus = false; // False stops the games True is needed to start the game
const winner_score = 5;
window.onload = function game(){ //Script will open after the page has loaded properly
var interval = setInterval(function(){ //Interval is called every 1000/30 ms or 30 Frames per second, for smooth gameplay
moveBall();
drawEverything();
}, 1000/30);
canvas.addEventListener('mousemove', (e)=> paddle1Y = e.offsetY - paddle_height/2); //adding event listener for player paddle movement through mouse
}
canvas.addEventListener('click', ()=> {gameStatus=true});
/////////////////////////////////////////
function computerMove(){ //function controlling computer mouse movement
const padddle_center = (paddle2Y +paddle_height)/2
if(ballX > canvas.width/2 + 150){
if(padddle_center < ballY - 100)
paddle2Y += 7;
else
paddle2Y -= 7;
}
else
paddle2Y = 150;
}
/////////////////////////////////////////
function ballReset(){ //function resets the ball after every score
if(player1 == winner_score || player2 == winner_score){
gameStatus = false; //on reaching the winning score status becomes false thus stoppping the game
console.log(player1,player2);
ctx.fillStyle = "#FE5E41";
ctx.font = "22px Comic Sans MS";
if(player1 > player2)
ctx.fillText("Player 1 Wins", canvas.width/2, 150);
else if(player1 < player2)
ctx.fillText("Player 2 Wins", canvas.width/2 , 150);
else
ctx.fillText("Click to Play Again", canvas.width/2-100,250);
player1= 0;
player2 =0;
console.log(player1,player2);
}
ballX = canvas.width /2;
ballY = canvas.width /2;
speedX = -speedX;
speedY = -speedY;
}
/////////////////////////////////////////
function moveBall(){ //function controlling ball movement
if(!gameStatus)
return;
computerMove();
ballX += speedX;
ballY += speedY;
if(ballX < paddle_width +5){ //To make it more intresting direction and speed changes at every hit
if(ballY > paddle1Y && ballY<paddle1Y+paddle_height){
var delta = ballY + (paddle1Y + paddle_height)/2;
speedY = delta*.025;
speedX = -speedX;
speedX ++;
}
else{
player2 ++;
console.log(player2);
ballReset();
}
}
if(ballX > canvas.width - paddle_width -5){
if(ballY > paddle2Y && ballY < paddle2Y+paddle_height){
var delta = ballY + (paddle2Y + paddle_height)/2;
speedY = delta*.025;
speedX = -speedX;
speedX ++;
}
else{
player1 ++;
console.log(player1);
ballReset();
}
}
if(ballY < 10 || ballY > canvas.height -10)
speedY = -speedY;
}
////////////////////////////////////////////////////////////
function drawEverything(){ //function call other functions which draw all the shapes on the canvas
if(!gameStatus){
ctx.fillStyle = "#4E711E";
ctx.font = "italic 28px Comic Sans MS";
ctx.textAlign = "center";
ctx.fillText("Click to Start The Game", canvas.width/2,250);
return;
}
drawShapes('#D8F1A0',0, 0, canvas.width, canvas.height);
drawShapes('#222',0, paddle1Y, paddle_width, paddle_height);
drawShapes('#222',canvas.width-10,paddle2Y, paddle_width, paddle_height);
drawBall();
scoreBoard();
drawNet();
}
/////////////////////////////////////////////////////////////
function drawShapes(color, x, y, w, h){ //to draw the paddle and canvas
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
ctx.beginPath();
}
/////////////////////////////////////////////////////////////
function drawBall(){ //to draw the ball
ctx.fillStyle= '#DF2201';
ctx.arc(ballX, ballY, radius, 0, Math.PI*2);
ctx.fill();
}
/////////////////////////////////////////////////////////////
function scoreBoard(){ //to draw the score board
ctx.fillStyle = "#FE5E41";
ctx.font = "22px Comic Sans MS";
ctx.fillText("Score Board", canvas.width/2 , 50);
ctx.fillText('Player 1', 50 ,90);
ctx.fillText('Player 2', canvas.width -150 ,90);
ctx.fillText(player1, 80 ,120);
ctx.fillText(player2, canvas.width -120 ,120);
}
/////////////////////////////////////////////////////////////
function drawNet(){ //to draw net
for(let i =10; i<canvas.height; i+= 40)
drawShapes('#0B0500', canvas.width/2, i, 2,25);
}
/////////////////////////////////////////////////////////////
</script>
</html>