-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
190 lines (156 loc) · 4.47 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pong</title>
</head>
<body>
<canvas id="game" width="800" height="600"></canvas>
<script>
var canvas;
var canvasContext;
var ballX = 400;
var ballY = 300;
var ballSpeedX = 8;
var ballSpeedY = 0;
const paddleWidth = 10;
const paddleHeight = 100;
var paddle1Y = 250;
var paddle2Y = 250;
var score1 = 0;
var score2 = 0;
const win = 5;
var winScreen = false;
var loseScreen = false;
window.onload = function () {
canvas = document.getElementById('game');
canvasContext = canvas.getContext('2d');
var FPS = 60;
setInterval(function () {
move();
draw();
}, 1000/FPS);
canvas.addEventListener('mousemove',function (evt) {
var mouse = mousePosition(evt);
paddle1Y = mouse.y-(paddleHeight/2)
})
canvas.addEventListener('mousedown',function (evt) {
if (winScreen || loseScreen) {
score1 = 0;
score2 = 0;
winScreen = false;
loseScreen = false;
}
})
}
function rectangle(X,Y,width,height,color) {
canvasContext.fillStyle = color;
canvasContext.fillRect(X,Y,width,height);
}
function circle(X,Y,radius,color) {
canvasContext.fillStyle = color;
canvasContext.beginPath();
canvasContext.arc(X,Y,radius,0,2*Math.PI,true);
canvasContext.fill();
}
function mousePosition(evt) {
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
var mouseX = evt.clientX - rect.left - root.scrollLeft;
var mouseY = evt.clientY - rect.top - root.scrollTop;
return {
x:mouseX,
y:mouseY
}
}
function AI() {
if (paddle2Y+(paddleHeight/2) < ballY-25) {
paddle2Y = paddle2Y+5;
}
else if (paddle2Y+(paddleHeight/2) > ballY+25) {
paddle2Y = paddle2Y-5;
}
}
function ballReset() {
if (score1 == win) {
winScreen = true;
}
if (score2 == win) {
loseScreen = true;
}
ballSpeedX = -ballSpeedX;
ballX = canvas.width/2;
ballY = canvas.height/2;
}
function draw() {
//you win
if (winScreen) {
canvasContext.fillStyle='green';
canvasContext.font = '30px Arial';
canvasContext.fillText("Player wins",330,300);
return;
}
//you lose
if (loseScreen) {
canvasContext.fillStyle='red';
canvasContext.font = '30px Arial';
canvasContext.fillText("Computer wins",305,300);
return;
}
//canvas
rectangle(0,0,canvas.width,canvas.height,'black');
//net
for (var i = 0;i < canvas.height;i = i+44){
rectangle(399,i,2,28,'white')
}
//ball
circle(ballX,ballY,10,'white');
//left paddle (player)
rectangle(20,paddle1Y,paddleWidth,paddleHeight,'white');
//right paddle (AI)
rectangle(770,paddle2Y,paddleWidth,paddleHeight,'white');
//score
canvasContext.fillStyle='white';
canvasContext.font = '15px Arial';
canvasContext.fillText(score1,50,50)
canvasContext.fillText(score2,750,50)
}
function move() {
if (winScreen) {
return;
}
if (loseScreen) {
return;
}
AI();
ballX = ballX+ballSpeedX;
ballY = ballY+ballSpeedY;
if (ballX < 10) {
score2++;
ballReset();
}
if (ballX > canvas.width-10){
score1++;
ballReset();
}
if (ballX < 45) {
if (ballY > paddle1Y && ballY < paddle1Y+paddleHeight) {
ballSpeedX = -ballSpeedX;
var deltaY = ballY-(paddle1Y+paddleHeight/2);
ballSpeedY = deltaY*0.3;
}
}
if (ballX > 755) {
if (ballY > paddle2Y && ballY < paddle2Y+paddleHeight) {
ballSpeedX = -ballSpeedX;
var deltaY = ballY-(paddle2Y+paddleHeight/2);
ballSpeedY = deltaY*0.3;
}
}
if (ballY < 15 || ballY > canvas.height-15){
ballSpeedY = -ballSpeedY;
}
}
</script>
</body>
</html>