-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
130 lines (106 loc) · 3.19 KB
/
script.js
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
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
let width = canvas.width;
let height = canvas.height;
const BALL_SIZE = 5;
let ballPosition = {x:20, y: 30};
let xSpeed = 4; //Controls the horizontal speed of the ball.
let ySpeed = 2; //Controls the vertical speed of the ball
const PADDLE_WIDTH = 5;
const PADDLE_HEIGHT = 20;
const PADDLE_OFFSET = 10;
let leftPaddleTop = 10;
let rightPaddleTop = 30;
document.addEventListener("mousemove", e => {
rightPaddleTop = e.y - canvas.offsetTop;
});
function draw () {
// Fills the canvas with black.
ctx.fillStyle = "black";
ctx.fillRect(0, 0, width, height);
// Fills everything else with white.
ctx.fillStyle = "white";
//Draws the ball on the canvas.
ctx.fillRect(ballPosition.x, ballPosition.y, BALL_SIZE, BALL_SIZE);
//Draws the paddles.
ctx.fillRect(
PADDLE_OFFSET,
leftPaddleTop,
PADDLE_WIDTH,
PADDLE_HEIGHT
);
ctx.fillRect(
width - PADDLE_WIDTH - PADDLE_OFFSET,
rightPaddleTop,
PADDLE_WIDTH,
PADDLE_HEIGHT
);
}
function update() {
ballPosition.x += xSpeed
ballPosition.y += ySpeed
}
function checkPaddleCollision(ball, paddle) {
// Check if the paddle and ball overlap vertically and horizontally.
return (
ball.left < paddle.right &&
ball.right > paddle.left &&
ball.top < paddle.bottom &&
ball.bottom > paddle.top
);
}
function adjustAngle(distanceFromTop, distanceFromBottom) {
if (distanceFromTop < 0) {
//If the ball hits near the top of the paddle, reduce ySpeed.
ySpeed -= 0.5;
}
else if (distanceFromBottom < 0) {
//If the ball hits near the bottom of the paddle, increase ySpeed.
ySpeed += 0.5;
}
}
//This function checks if the ball has collided with one of the four edges of the canvas.
function checkCollision() {
let ball = {
left: ballPosition.x,
right: ballPosition.x + BALL_SIZE,
top: ballPosition.y,
bottom: ballPosition.y + BALL_SIZE
}
if(ball.left < 0 || ball.right > width) {
xSpeed = -xSpeed;
}
if(ball.top < 0 || ball.bottom > height) {
ySpeed = -ySpeed;
}
let leftPaddle = {
left: PADDLE_OFFSET,
right: PADDLE_OFFSET + PADDLE_WIDTH,
top: leftPaddleTop,
bottom: leftPaddleTop + PADDLE_HEIGHT
}
let rightPaddle = {
left: width - PADDLE_WIDTH - PADDLE_OFFSET,
right: width - PADDLE_OFFSET,
top: rightPaddleTop,
bottom: rightPaddleTop + PADDLE_HEIGHT
}
if(checkPaddleCollision(ball, leftPaddle)) {
//Left paddle collision happened.
let distanceFromTop = ball.top - rightPaddle.top;
let distanceFromBottom = leftPaddle.bottom - ball.bottom;
adjustAngle(distanceFromTop, distanceFromBottom);
xSpeed = Math.abs(xSpeed);
}
if(checkPaddleCollision(ball, rightPaddle)) {
//Right paddle collision happened.
xSpeed = -Math.abs(xSpeed);
}
}
function gameLoop(){
draw();
update();
checkCollision();
setTimeout(gameLoop, 30); //Calls the function after 30 seconds timeout.
}
gameLoop();