-
Notifications
You must be signed in to change notification settings - Fork 0
/
arcade.html
229 lines (204 loc) · 6.02 KB
/
arcade.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#############################################################################################################################
# Project 2020
# Developer: Aparna Naik
# Project Name: Tennis Game
# Project Aim: Arcade Game Using Canvas and a AI controlled paddle
# Date: 11/04/2020
#############################################################################################################################
<HTML>
<CANVAS Id="game canvas", WIDTH=1420, HEIGHT=800></CANVAS>
<SCRIPT>
var canvas;
var CanvasContext;
//ballX and ballY denote the positions of the balls on the canvas
var ballX=50;
var ballY=50;
//ballSpeedX and ballSpeedY is the speed of the ball with respect to the X and Y co-ordinate axes.
var ballSpeedX=10;
var ballSpeedY=4;
//paddle1Y represents the left side or the player of the game. paddle2Y represents the AI bot.
var paddle1Y=250;
var paddle2Y=250;
//Constant Paddle Parameters
const paddle_height=100;
const paddle_thickness =10;
//Display of Scores
var player1score=0;
var player2score=0;
//Maximum winning score
const winning_score =3;
//Boolean value which helps in switching the screen and reseting the values of the scores.
var showingWinScreen =false;
//Most important: Calculates the position of the mouse using the co-ordinates of the mouse on the canvas.
function calculateMousePos(evt)
{
//Gets the actual position of the canvas element. In our case it is the entire screen. However, it can vary. And that is why this is important.
var rect = canvas.getBoundingClientRect();
//Assigns the <HTML> document element to the root.
var root = document.documentElement;
//Calculates the mouse position relative to the CANVAS and NOT the computer screen.
var mouseX = evt.clientX - rect.left - root.scrollLeft;
var mouseY = evt.clientY - rect.top - root.scrollTop;
return {
x:mouseX,
y:mouseY
};
}
//On a mouse click, the scores get reset and the playing area or the canvas can be seen again.
function handlemouseclick()
{
if(showingWinScreen)
{
player1score =0;
player2score =0;
showingWinScreen = false;
}
}
//Waits until all the things on the webpage get loaded, this relaxes the sequence in which the functions appear in the code.
window.onload= function ()
{
//Canvas is the element. We are drawing 2D elements on the webpage/screen.
canvas = document.getElementById('game canvas');
CanvasContext = canvas.getContext('2d');
//Sets the ball in motion, Calls the setInterval function for every 1000/framespersecond
var framespersecond =30;
setInterval( function() {
moveEverything();
drawEverything();
}, 1000/framespersecond);
//This is for displaying the original canvas again after showing the winner
canvas.addEventListener('mousedown',handlemouseclick);
//Helps in allocation of the mouse on the screen or its movement.
canvas.addEventListener('mousemove',
function(evt){
var mousePos= calculateMousePos(evt);
paddle1Y = mousePos.y-(paddle_height/2);
});
}
//If the paddle misses to hit, the ball is reset, direction is opposite.
function ballReset()
{
if(player1score >= winning_score || player2score >= winning_score)
{
showingWinScreen = true;
}
ballSpeedX = -ballSpeedX;
ballX = canvas.width/2;
ballY = canvas.height/2;
}
//AI bot control
function computerMovement()
{
var paddle2YCenter = paddle2Y + paddle_height/2;
if(paddle2YCenter <ballY-35)
{
paddle2Y = paddle2Y +6;
}
else if(paddle2YCenter > ballY+35){
paddle2Y = paddle2Y -6;
}
}
//Controls the movement
function moveEverything()
{
if(showingWinScreen)
{
return;
}
computerMovement();
ballX= ballX + ballSpeedX;
ballY= ballY + ballSpeedY;
if (ballX < 0)
{
if(ballY > paddle1Y &&
ballY < paddle1Y+paddle_height)
{
ballSpeedX = -ballSpeedX;
var deltaY =ballY - (paddle1Y + paddle_height/2);
ballSpeedY = deltaY *0.35;
}
else
{
player2score++;
ballReset();
}
}
if (ballX> canvas.width)
{
if(ballY > paddle2Y &&
ballY < paddle2Y+paddle_height)
{
ballSpeedX = -ballSpeedX;
var deltaY =ballY - (paddle2Y + paddle_height/2);
ballSpeedY = deltaY * 0.35;
}
else
{
player1score++;
ballReset();
}
}
if (ballY < 0)
{
ballSpeedY = -ballSpeedY;
}
if (ballY > canvas.height)
{
ballSpeedY = -ballSpeedY;
}
}
//Draws the central Net(Division)
function drawNet() {
for (var i =0; i<canvas.height; i+=40)
{
colourRect(canvas.width/2-1,i,2,20,'white');
}
}
//Draws all the graphics on the canvas
function drawEverything()
{
//Next Line blanks out the screen with black
colourRect(0, 0, canvas.width, canvas.height, 'black');
if(showingWinScreen)
{
CanvasContext.fillStyle = 'white';
if(player1score >= winning_score)
{
CanvasContext.font="30px Times";
CanvasContext.fillText ("Left Player Won", 630, 400);
}
else if(player2score >= winning_score)
{
CanvasContext.font="30px Times";
CanvasContext.fillText ("Right Player Won", 630, 400);
}
CanvasContext.fillText("Click to continue", 630, 450);
return;
}
drawNet();
//This is left player paddle
colourRect(0, paddle1Y, paddle_thickness, paddle_height, 'white');
//This is right player paddle
colourRect(canvas.width-paddle_thickness, paddle2Y, paddle_thickness, paddle_height, 'white');
//Next line draws the ball
colourCircle(ballX, ballY, 10, 10, 'white');
CanvasContext.font="30px Times";
CanvasContext.fillText(player1score, 100, 100);
CanvasContext.fillText(player2score, canvas.width-100, 100);
}
//Helper function to color the circles
function colourCircle(centerX, centerY, radius, drawColour)
{
CanvasContext.fillStyle= drawColour;
CanvasContext.beginPath();
CanvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
CanvasContext.fill();
}
//Helper function to color the rectangles.
function colourRect( leftX, topY, width, height, drawColour)
{
CanvasContext.fillStyle= drawColour;
CanvasContext.fillRect ( leftX, topY, width, height);
}
</SCRIPT>
</HTML>