-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (126 loc) · 3.82 KB
/
script.js
File metadata and controls
144 lines (126 loc) · 3.82 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
/** @type {HTMLCanvasElement} */
const canvas = document.querySelector("#game");
const ctx = canvas.getContext("2d");
CanvasItem.ctx = ctx;
const h = canvas.height,
w = canvas.width;
const settings = {
gridSize: w / 70,
lines: 16,
running: false,
life: 0,
maxScore: 0,
score: 0
}
CanvasBrick.width = settings.gridSize;
CanvasBrick.height = settings.gridSize / 1;
const grid = {},
paddle = new CanvasPaddle(w / 2 - w / 8 / 2, h * (1 - 0.01), w / 8, h * 0.01, "#fff");
/** @type {[CanvasBall]} */
let balls = [];
window.addEventListener("keydown", (e) => {
if (e.key == "ArrowRight")
paddle.direction = 1
if (e.key == "ArrowLeft")
paddle.direction = -1
})
window.addEventListener("keyup", (e) => {
if (e.key == "ArrowRight" && paddle.direction == 1)
paddle.direction = 0
if (e.key == "ArrowLeft" && paddle.direction == -1)
paddle.direction = 0
})
function startGame() {
if (settings.running)
return;
settings.running = true;
settings.life = 2;
settings.maxScore = CreateBrickGrid();
settings.score = 0;
balls = [new CanvasBall(w / 2, h / 2, settings.gridSize / 3, "#fff")]
gameLoop();
}
function gameLoop() {
updateGame();
drawGame();
if (settings.running)
requestAnimationFrame(gameLoop);
}
function gameOver() {
settings.running = false;
}
function CreateBrickGrid() {
const powers = ["ball", "multi", "mega"]
const lines = settings.lines;
let count = 0;
const colors = ["mediumorchid", "deeppink", "red", "tomato", "orange", "gold", "yellow", "lime", "turquoise"]
for (let line = 0; line < lines; line++) {
if (!grid[line])
grid[line] = {};
for (let x = 0; x < w / settings.gridSize; x++) {
const brick = new CanvasBrick(x * CanvasBrick.width,
line * CanvasBrick.height,
colors[~~((line + x ** 0.55 + (1 + Math.cos(x))) % colors.length)],
Math.random() < 0.5 ? powers[~~(Math.random() * powers.length)] : null
)
grid[line][x] = brick;
count++;
}
}
return count;
}
//update
function updateGame() {
let ballLost = true;
for (const ball of balls) {
ballLost = ball.update() && ballLost
}
//test if life lost & handle it
if (ballLost) {
settings.life--;
if (settings.life <= 0)
return gameOver();
balls = [new CanvasBall(w / 2, h / 2, settings.gridSize / 3, "#fff")]
}
balls.forEach(b => {
const hitBrick = b.testCollision(grid)
if (hitBrick) {
b.speed *= 1.05;
settings.score++;
if (settings.score >= settings.maxScore)
settings.running = false;
switch (hitBrick?.power) {
case "ball":
balls.push(new CanvasBall(w / 2, h / 2, settings.gridSize / 3, "#fff"))
break;
case "mega":
const sizeIncrease = -b.r * 0.2;
b.r += sizeIncrease;
setTimeout(() => { b.r -= sizeIncrease }, 5 * 1000)
break;
case "multi":
const oldBalls = balls.map(a => a).filter(b => !b.checkOutbound());
if (oldBalls.length < 100)
oldBalls.forEach(b => balls.push(new CanvasBall(b.x, b.y, settings.gridSize / 3, "red")))
break;
}
}
})
paddle.update(balls);
}
//draw staff
function drawGame() {
ctx.clearRect(0, 0, w, h);
drawBricks()
balls.forEach(b => b.draw());
paddle.draw();
}
function drawBricks() {
for (let key in grid) {
const line = grid[key]
for (let lineKey in line) {
const brick = line[lineKey]
brick.draw()
}
}
}