-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
80 lines (67 loc) · 1.62 KB
/
main.js
File metadata and controls
80 lines (67 loc) · 1.62 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
import Ball from "./model/Ball.js";
import Paddle from "./model/Paddle.js";
import Brick from "./model/Brick.js";
import "./style.css";
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const ball = new Ball(
canvas.width / 2,
canvas.height - 30,
10,
10,
"#0095DD",
2,
-2
);
const paddle = new Paddle(
(canvas.width - 10) / 2,
canvas.height - 10,
75,
10,
"#0095DD"
);
const brickRowCount = 3;
const brickColumnCount = 5;
const bricks = [];
const brickWidth = 75;
const brickHeight = 20;
const brickPadding = 10;
const brickOffsetTop = 30;
const brickOffsetLeft = 30;
for (let c = 0; c < brickColumnCount; c++) {
for (let r = 0; r < brickRowCount; r++) {
let brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
let brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
bricks.push(new Brick(brickX, brickY, brickWidth, brickHeight, "#0095DD"));
}
}
let score = 0;
let isGameOver = false;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ball.draw(ctx);
ball.move();
isGameOver = !ball.bounce(canvas.width, canvas.height);
paddle.draw(ctx);
paddle.move(canvas.width);
ball.colides(paddle);
bricks.forEach((brick) => {
brick.draw(ctx);
if (brick.colides(ball)) {
score++;
}
});
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("Score: " + score, 8, 20);
if (!isGameOver) {
if (score == brickRowCount * brickColumnCount) {
window.alert("You won!");
} else {
window.requestAnimationFrame(draw);
}
} else {
window.alert("Game over!");
}
}
draw();