-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
211 lines (182 loc) · 5.39 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
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
<!DOCTYPE html>
<head>
<title>Pong </title>
<style type="text/css">
* {
margin: 0;
padding: 0;
background-color: black;
}
#gameCanvas{
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
border-width: 40px
}
</style>
</head>
<body>
<div id="header">
<h1>Pong Master</h1>
</div>
<canvas id="gameCanvas" width="1200" height="800"></canvas>
<script type="text/javascript">
var canvas;
var canvasContext;
var ballXCoordinate;
var rules;
var ballXSpeed = 20;
var ballYCoordinate;
var ballYSpeed = 20;
var ballRadius = 10;
var leftPlayerY = 250;
var rightPlayerY = 250;
var leftPlayerScore = 0;
var rightPlayerScore = 0;
var winScreen = false;
const WINNING_SCORE = 5;
const PADDLE_HEIGHT = 200;
const PADDLE_WIDTH = 20;
function mouseClick(event){
if(winScreen){
leftPlayerScore = 0;
rightPlayerScore = 0;
winScreen = false;
} else if (rules){
rules = false;
}
};
function computerMovement(){
var rightPlayerYCenter = rightPlayerY + (PADDLE_HEIGHT / 2)
if (ballYCoordinate - 10 <= rightPlayerYCenter){
rightPlayerY -= 7;
} else if (ballYCoordinate - 10 >= rightPlayerYCenter){
rightPlayerY += 7;
};
};
function mousePosition(event){
var rect = canvas.getBoundingClientRect();
var page = document.documentElement;
var mouseX = event.clientX - rect.left - page.scrollLeft;
var mouseY = event.clientY - rect.top - page.scrollTop;
return {
x: mouseX,
y: mouseY
};
};
window.onload = function(){
rules = true;
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
ballXCoordinate = canvas.width / 2;
ballYCoordinate = canvas.height / 2;
var framesPerSecond = 60;
setInterval(function(){
moveBall();
drawOnCanvas();
}, 1000 / framesPerSecond);
canvas.addEventListener('mousedown', mouseClick);
canvas.addEventListener('mousemove',
function(event){
var mousePos = mousePosition(event);
leftPlayerY = mousePos.y - (PADDLE_HEIGHT / 2);
});
};
function moveBall(){
if (winScreen){
return
}
if (rules){
return
}
computerMovement();
ballXCoordinate += ballXSpeed;
ballYCoordinate += ballYSpeed;
// Left paddle hits the ball
if (ballYCoordinate < (leftPlayerY + PADDLE_HEIGHT) && ballYCoordinate > leftPlayerY && ballXCoordinate < 5 +PADDLE_WIDTH + ballRadius){
ballXSpeed *= -1;
var angle = ballYCoordinate - (leftPlayerY + (PADDLE_HEIGHT / 2))
ballYSpeed = angle * .33
// Right paddle hits the ball
} else if (ballYCoordinate < (rightPlayerY + PADDLE_HEIGHT) && ballYCoordinate > rightPlayerY && ballXCoordinate > (canvas.width - 5 - PADDLE_WIDTH - ballRadius)){
ballXSpeed *= -1;
var angle = ballYCoordinate - (rightPlayerY + (PADDLE_HEIGHT / 2))
ballYSpeed = angle * .33
} else if (ballXCoordinate >= canvas.width - ballRadius){
leftPlayerScore++;
resetBallPosition();
} else if (ballXCoordinate <= ballRadius){
rightPlayerScore++;
resetBallPosition();
};
if (ballYCoordinate >= (canvas.height - ballRadius) || ballYCoordinate <= ballRadius){
ballYSpeed *= -1;
};
}
function drawNet(){
for(var i=10; i < canvas.height; i+=50){
makeRect((canvas.width / 2)- 1, i, 15, 30, 'white')
};
};
function noticeStyle(color, font, align){
canvasContext.fillStyle = color;
canvasContext.font = font;
canvasContext.textAlign = align;
};
function drawOnCanvas(){
// play area
makeRect(0, 0, canvas.width, canvas.height, 'black');
if (winScreen){
canvasContext.fillStyle = 'white';
canvasContext.font = '40px Arial';
canvasContext.textAlign = 'center';
if (leftPlayerScore >= WINNING_SCORE){
canvasContext.fillText("You won!", (canvas.width / 2), canvas.height / 2);
} else if (rightPlayerScore >= WINNING_SCORE){
canvasContext.fillText("Computer wins!", (canvas.width / 2), canvas.height / 2);
}
canvasContext.fillText("Click to play again.", (canvas.width / 2), canvas.height / 2 + 100);
return
}
if (rules){
canvasContext.fillStyle = 'white';
canvasContext.font = '40px Arial';
canvasContext.textAlign = 'center';
canvasContext.fillText("Click to play!", (canvas.width / 2), canvas.height / 2);
return
}
drawNet();
// left player paddle
makeRect(5, leftPlayerY, PADDLE_WIDTH, PADDLE_HEIGHT, 'white');
// right player paddle
makeRect((canvas.width - PADDLE_WIDTH - 5), rightPlayerY, PADDLE_WIDTH, PADDLE_HEIGHT, 'white');
// ball
makeBall(ballXCoordinate, ballYCoordinate, ballRadius, 'white');
canvasContext.font="30px Arial"
canvasContext.fillText(rightPlayerScore, canvas.width - 100, 40)
canvasContext.fillText(leftPlayerScore, 100, 40)
}
function makeBall(xPosition, yPosition, radius, color){
canvasContext.fillStyle = color;
canvasContext.beginPath();
canvasContext.arc(xPosition, yPosition, radius, 0, Math.PI * 2, true);
canvasContext.fill();
}
function makeRect(xPosition, yPosition, width, height, color){
canvasContext.fillStyle = color;
canvasContext.fillRect(xPosition, yPosition, width, height);
}
function resetBallPosition(){
if (leftPlayerScore >= WINNING_SCORE || rightPlayerScore >= WINNING_SCORE){
winScreen = true;
}
ballYCoordinate = canvas.height / 2;
ballXCoordinate = canvas.width / 2;
ballXSpeed *= -1;
ballYSpeed = 5
}
</script>
</body>
</html>