-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
182 lines (147 loc) · 4.98 KB
/
Copy pathindex.html
File metadata and controls
182 lines (147 loc) · 4.98 KB
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
<html>
<canvas id="gameCanvas" width="800"
height="600"></canvas>
<script type="text/javascript">
var canvas;
var canvasContext;
var ballX=50,ballY=50;
var ballSpeedX=8,ballSpeedY=4.8;
var player1score=0,player2score=0;
const WIN_SCORE = 15;
var paddle1=250,paddle2=250;
const PADDLE_HEIGHT = 100; // constant value
const PADDLE_THICKESS = 10;
var showingWinScreen = false;
function calcMousePos(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
}; // returning the current position of mouse
}
function ballReset()
{
if(player1score>=WIN_SCORE)
{
alert("Player 1 has won!");
player1score=player2score=0;
}
else if(player2score>=WIN_SCORE)
{
alert("Player 2 has won!");
player1score=player2score=0;
}
ballSpeedX=-ballSpeedX;
ballX=canvas.width/2;
ballY=canvas.height/2;
}
window.onload=function()
{
console.log("Finished Loading page");
canvas=document.getElementById('gameCanvas');
canvasContext=canvas.getContext('2d');
var framesPerSecond=30;
setInterval(function(){
moveEverything();
drawEverything();
} ,1000/framesPerSecond);
canvas.addEventListener('mousemove',
function(evt){
var mousePos=calcMousePos(evt);
paddle1=mousePos.y-(PADDLE_HEIGHT/2);
});
}
function computerMovement()
{
// computer controls !
var paddle2centre=paddle2+(PADDLE_HEIGHT/2);
if(paddle2centre<ballY-34)
{
paddle2+=5.8;
}
else if(paddle2centre>ballY+34)
{
paddle2-=5.9;
}
}
function moveEverything()
{
computerMovement();
ballX+=ballSpeedX;
ballY+=ballSpeedY;
if(ballX<=0) {
// bouncing off the paddle
if(ballY>paddle1 && ballY<paddle1+PADDLE_HEIGHT){
ballSpeedX=-ballSpeedX;
// use collision principles for better controls!
var deltaY=ballY-(paddle1+PADDLE_HEIGHT/2);
ballSpeedY=deltaY*0.33;
} else { // reset the ball if missed
player2score++; ballReset();
}
}
// Right controls
if(ballX>=canvas.width) {
// bouncing off the paddle
if(ballY>paddle2 && ballY<paddle2+PADDLE_HEIGHT){
ballSpeedX=-ballSpeedX;
// use collision principles for better controls!
var deltaY=ballY-(paddle2+PADDLE_HEIGHT/2);
ballSpeedY=deltaY*0.30;
} else { // reset the ball if missed
player1score++; ballReset();
}
}
if(ballY<=0) {
ballSpeedY=-ballSpeedY;
}
if(ballY>=canvas.height) {
ballSpeedY=-ballSpeedY;
}
}
function drawNet()
{
for(var i=0;i<=canvas.height;i+=30)
{
colorRect(canvas.width/2-1,i,2,20,'white');
}
}
function drawEverything()
{
// blanks out the canvas
colorRect(0,0,canvas.width,canvas.height,'black');
// net in the middle
drawNet();
// for the player paddle: Left
colorRect(2,paddle1,PADDLE_THICKESS,
PADDLE_HEIGHT,'yellow');
// for the computer paddle: Right
colorRect(canvas.width-PADDLE_THICKESS-2,paddle2,
PADDLE_THICKESS,PADDLE_HEIGHT,'lightgreen');
// drawing the ball
colorCircle(ballX,ballY,10,'hotpink');
// finally, put in the text for updating scores
canvasContext.font='20px Georgia'
canvasContext.fillStyle='yellow'
canvasContext.fillText(player1score,100,50);
canvasContext.fillText(player2score,canvas.width-100,50);
}
function colorRect(leftX,topY,width,height,drawColor)
{
canvasContext.fillStyle=drawColor;
canvasContext.fillRect(leftX,topY,width,height);
}
function colorCircle(centreX,centreY,radius,drawColor)
{
canvasContext.fillStyle=drawColor;
canvasContext.beginPath();
canvasContext.arc(centreX,centreY,radius,0,Math.PI*2,
true);
canvasContext.fill();
}
</script>
</html>