-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
179 lines (153 loc) · 5.08 KB
/
index.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
<html>
<head>
<title>Ping Pong Game</title>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
</body>
<script>
var canvas;
var canvasContext;
var winScreen = false;
const WINNING_SCORE = 3;
var playerScore = 0;
var computerScore = 0;
var ballRadius = 10;
var ballPositionX = 50;
var ballPositionY = 50;
var ballSpeedX = 5;
var ballSpeedY = 4;
const PADDLE_THICKNESS = 10;
const PADDLE_HEIGHT = 100;
var paddlePositionPlayer = 250;
var paddlePositionComputer = 250;
function calculateMousePosition(e){
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
var mouseX = e.clientX - rect.left - root.scrollLeft;
var mouseY = e.clientY - rect.top - root.scrollTop;
return{
x: mouseX,
y: mouseY
};
}
function handleMouseClick(e){
if(winScreen){
playerScore = 0;
computerScore = 0;
winScreen = false;
}
}
window.onload = function(){
canvas = document.getElementById("gameCanvas");
canvasContext = canvas.getContext("2d");
var framesPerSecond = 60;
setInterval(function(){
moveEverything();
drawEverything();
}, 1000/framesPerSecond);
canvas.addEventListener("mousedown", handleMouseClick);
canvas.addEventListener("mousemove",
function(e){
var mousePosition = calculateMousePosition(e);
paddlePositionPlayer = mousePosition.y - (PADDLE_HEIGHT/2);
});
}
function ballReset(){
if(playerScore >= WINNING_SCORE || computerScore >= WINNING_SCORE){
winScreen = true;
}
ballSpeedX = -ballSpeedX;
ballSpeedY = 4; //Reset to default value.
ballPositionX = canvas.width/2;
ballPositionY = canvas.height/2;
}
function computerMovement(){
/*
Ignore chasing the ball while it's within 35 pixels above or below
the paddle center position. (70 pixel span)
*/
if(paddlePositionComputer + (PADDLE_HEIGHT/2) < ballPositionY - 35){
paddlePositionComputer += 5;
}
else if(paddlePositionComputer + (PADDLE_HEIGHT/2) > ballPositionY + 35){
paddlePositionComputer -= 5;
}
}
function moveEverything(){
if(winScreen){
return; //Stop the game
}
computerMovement();
ballPositionX += ballSpeedX;
ballPositionY += ballSpeedY;
// ballRadius is used to make the ball hit the edge of canvas.
if(ballPositionX < 0 + ballRadius){ //Left Wall
if(ballPositionY > paddlePositionPlayer && ballPositionY < paddlePositionPlayer + PADDLE_HEIGHT){
ballSpeedX = -ballSpeedX;
var ballControl = ballPositionY - (paddlePositionPlayer + PADDLE_HEIGHT/2);
ballSpeedY = ballControl * 0.35;
}else{
computerScore++;
ballReset();
}
}
if(ballPositionX > canvas.width - ballRadius){ //Right Wall
if(ballPositionY > paddlePositionComputer && ballPositionY < paddlePositionComputer + PADDLE_HEIGHT){
ballSpeedX = -ballSpeedX;
var ballControl = ballPositionY - (paddlePositionComputer + PADDLE_HEIGHT/2);
ballSpeedY = ballControl * 0.35;
}else{
playerScore++;
ballReset();
}
}
if(ballPositionY < 0 + ballRadius){ //Ceiling
ballSpeedY = -ballSpeedY;
}
if(ballPositionY > canvas.height - ballRadius){ //Floor
ballSpeedY = -ballSpeedY;
}
}
function drawNet(){
for(var i = 0; i < canvas.height; i+=34){
drawRect(canvas.width/2, i, 2, 20, "white");
}
}
function drawEverything(){
canvasContext.font="30px Arial";
//Canvas
drawRect(0, 0, canvas.width, canvas.height, "black");
if(winScreen){
canvasContext.fillStyle = "white";
if(playerScore >= WINNING_SCORE){
canvasContext.fillText("You Won!", 350, 200);
}else if(computerScore >= WINNING_SCORE){
canvasContext.fillText("You Lost!", 350, 200);
}
canvasContext.fillText("Click to continue", 300, 500);
return;
}
//The net
drawNet();
//Left player paddle
drawRect(0, paddlePositionPlayer, PADDLE_THICKNESS, PADDLE_HEIGHT, "blue");
//Right computer paddle
drawRect(canvas.width - PADDLE_THICKNESS, paddlePositionComputer, PADDLE_THICKNESS, PADDLE_HEIGHT, "blue");
//The ball
drawCircle(ballPositionX, ballPositionY, ballRadius, "white");
canvasContext.fillText(playerScore, 100, 100);
canvasContext.fillText(computerScore, canvas.width - 100, 100);
}
function drawCircle(centerX, centerY, radius, drawColor){
canvasContext.fillStyle = drawColor;
canvasContext.beginPath();
canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
canvasContext.fill();
}
function drawRect(leftX, topY, width, height, drawColor){
canvasContext.fillStyle = drawColor;
canvasContext.fillRect(leftX, topY, width, height);
}
</script>
</html>