-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathPingPongGame.html
188 lines (163 loc) · 4.57 KB
/
PingPongGame.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
<!DOCTYPE html>
<html>
<head>
<title>Javascript based Ping Pong Game</title>
<style>
*{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;}
</style>
</head>
<body>
<canvas id=myCanvas width = 700 height=500></canvas>
<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>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
let paddle1Y = 10, paddle2Y = 10;;
const paddle_width = 10, paddle_height = 100;
const radius =10;
let ballX = canvas.width/2, ballY = canvas.height/2;
let speedX = 6, speedY = 6;
let player1 =0, player2 =0;
var gameStatus = false;
const winner_score = 5;
window.onload = function game(){
var interval = setInterval(function(){
moveBall();
drawEverything();
}, 1000/30);
canvas.addEventListener('mousemove', (e)=> paddle1Y = e.offsetY - paddle_height/2);
}
canvas.addEventListener('click', ()=> {gameStatus=true});
function computerMove(){
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(){
if(player1 == winner_score || player2 == winner_score){
gameStatus = false;
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(){
if(!gameStatus)
return;
computerMove();
ballX += speedX;
ballY += speedY;
if(ballX < paddle_width +5){
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(){
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){
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
ctx.beginPath();
}
function drawBall(){
ctx.fillStyle= '#DF2201';
ctx.arc(ballX, ballY, radius, 0, Math.PI*2);
ctx.fill();
}
function scoreBoard(){
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(){
for(let i =10; i<canvas.height; i+= 40)
drawShapes('#0B0500', canvas.width/2, i, 2,25);
}
</script>
</html>