-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
91 lines (81 loc) · 2.09 KB
/
main.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
var canvas = document.getElementById("playArea");
var ctx = canvas.getContext("2d");
var bar = document.getElementById("dvGameBar");
var score = document.getElementById("lblScore");
var hScore = document.getElementById("lblHighScore");
var mess = document.getElementById("lblMessage");
var ball = {
x: 0,
y: 0,
moveX: 3,
moveY: -2,
radius: 12
};
var paddle = {
x: 0,
height: 10,
width: 70,
moveX: 3,
moveBy: 0
};
var game = {
lives: 3,
playing: false,
score: 0,
highScore: 0
};
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = "darkblue";
ctx.fill();
ctx.closePath();
if ((ball.x + ball.moveX + ball.radius) > canvas.width ||
ball.x + ball.moveX < ball.radius) {
ball.moveX = -ball.moveX;
}
if ((ball.y + ball.moveY + ball.radius) > canvas.height ||
ball.y + ball.moveY < ball.radius) {
ball.moveY = -ball.moveY;
}
ball.x += ball.moveX;
ball.y += ball.moveY;
}
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddle.x, canvas.height - paddle.height,
paddle.width, paddle.height);
ctx.fillStyle = "black";
ctx.fill();
ctx.closePath();
}
function mouseHasMoved(event) {
let mouseRelX = event.clientX - canvas.offsetLeft;
if (mouseRelX > 0 && mouseRelX < canvas.width - paddle.width) {
paddle.x = mouseRelX;
}
}
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - bar.clientHeight;
if (ball.x > canvas.width) {
ball.x = canvas.width;
}
if (ball.y > canvas.height) {
ball.y = canvas.height;
}
}
function redraw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
}
function initialise() {
window.addEventListener('resize', resizeCanvas);
ball.x = window.innerWidth / 4;
ball.y = window.innerHeight - ball.radius - bar.clientHeight;
paddle.x = (window.innerWidth - paddle.width) / 2;
resizeCanvas();
setInterval(redraw, 10);
}
initialise();