-
Notifications
You must be signed in to change notification settings - Fork 48
/
breakout.js
228 lines (188 loc) · 4.89 KB
/
breakout.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
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
// Core Breakout game. Logic derived from:
// https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript/Finishing_up
// Collision detection is not so great.
var score;
var lives;
var x;
var y;
var dx;
var dy;
var bands;
var bricks;
var bricksRemaining;
function init() {
// Page open event (and so this script file) might get called again,
// so head it off here.
if (global.initialized) return;
global.initialized = true;
score = 0;
lives = 3;
x = CANVAS_WIDTH/2;
y = 430;
dx = 2;
dy = 2;
bands = [];
bricks = [];
global.count = 3;
global.mouseX = CANVAS_WIDTH/2;
global.paused = false;
initBricks();
// Hide all the mouse-X detection bands until the game starts.
// The user can then see the callout telling them to move their mouse
// during the countdown.
for (var bx = 0; bx < CANVAS_WIDTH; bx++) {
bands[bx] = this.getField('band' + bx);
bands[bx].display = display.hidden;
}
countdown();
}
function initBricks() {
for (var c = 0; c < BRICK_COLUMN_COUNT; c++) {
bricks[c] = [];
for (var r = 0; r < BRICK_ROW_COUNT; r++) {
bricks[c][r] = {
x: r*(BRICK_WIDTH + BRICK_PADDING) + BRICK_OFFSET_LEFT,
y: c*(BRICK_HEIGHT + BRICK_PADDING) + BRICK_OFFSET_BOTTOM,
status: 1,
field: this.getField('brick' + c + ',' + r)
};
}
}
bricksRemaining = BRICK_ROW_COUNT * BRICK_COLUMN_COUNT;
}
function collisionDetection() {
for (var c = 0; c < BRICK_COLUMN_COUNT; c++) {
for (var r = 0; r < BRICK_ROW_COUNT; r++) {
var b = bricks[c][r];
if (b.status != 1) continue;
if (x > b.x && x < b.x + BRICK_WIDTH &&
y > b.y && y < b.y + BRICK_HEIGHT) {
dy = -dy;
b.status = 0;
score++;
bricksRemaining--;
if (bricksRemaining === 0) {
app.alert("YOU WIN, CONGRATS!");
dx *= 1.1;
dy *= 1.1;
initBricks();
}
}
}
}
}
var ball = this.getField('ball');
function drawBall() {
// Rect changes are almost the only changes you can carry out on a
// Field in Chrome's subset of PDF JS:
// https://pdfium.googlesource.com/pdfium/+/chromium/2524/fpdfsdk/src/javascript/Field.cpp#2356
ball.rect = [
x, y, x + BALL_WIDTH, y + BALL_HEIGHT
];
}
function paddleX() {
return global.mouseX - PADDLE_WIDTH/2;
}
var paddle = this.getField('paddle');
function drawPaddle() {
paddle.rect = [
paddleX(),
paddle.rect[1],
paddleX() + PADDLE_WIDTH,
paddle.rect[3]
]
}
function drawBricks() {
for (var c = 0; c < BRICK_COLUMN_COUNT; c++) {
for (var r = 0; r < BRICK_ROW_COUNT; r++) {
if (bricks[c][r].status == 1) {
bricks[c][r].field.display = display.visible;
} else {
bricks[c][r].field.display = display.hidden;
}
}
}
}
var scoreField = this.getField('score');
function drawScore() {
scoreField.value = "Score: " + score;
}
var livesField = this.getField('lives');
function drawLives() {
livesField.value = 'Lives: ' + lives;
}
var countdownField = this.getField('countdown');
function draw() {
if (global.paused) {
countdownField.display = display.visible;
countdownField.value = 'Paused';
return;
}
countdownField.display = display.hidden;
drawBricks();
drawBall();
drawPaddle();
drawScore();
drawLives();
collisionDetection();
if (x + dx > CANVAS_WIDTH - BALL_WIDTH || x + dx < 0) {
dx = -dx;
}
if (y + dy > CANVAS_BOTTOM + CANVAS_HEIGHT - BALL_HEIGHT) {
dy = -dy;
} else if (y + dy < PADDLE_OFFSET_BOTTOM + PADDLE_HEIGHT) {
if (x + BALL_WIDTH > paddleX() && x < paddleX() + PADDLE_WIDTH) {
dy = -dy;
} else {
lives--;
if (lives === 0) {
app.alert("GAME OVER");
score = 0;
lives = 3;
x = CANVAS_WIDTH/2;
y = 430;
dx = 2;
dy = 2;
initBricks();
} else {
x = CANVAS_WIDTH / 2;
y = CANVAS_BOTTOM + CANVAS_HEIGHT - 30;
if (dx < 0) dx = -dx;
if (dy > 0) dy = -dy;
}
}
}
x += dx;
y += dy;
}
// This 'whole' thing blanks out the whole screen while we render,
// because Chrome doesn't expect us to actually move objects around,
// so if you do it naively you get terrible artifacts. Breaks Acrobat,
// though (or at least they're too slow for this to work nicely).
var whole = this.getField('whole');
function wrappedDraw() {
try {
whole.display = display.visible;
draw();
whole.display = display.hidden;
} catch (e) {
app.alert(e.toString())
}
}
function start() {
for (var bx = 0; bx < CANVAS_WIDTH; bx++) {
bands[bx].display = display.visible;
}
// TODO Some kind of speed regulation.
app.setInterval('wrappedDraw()', 15);
}
function countdown() {
countdownField.value = global.count.toString();
global.count--;
if (global.count < 0) {
start();
} else {
app.setTimeOut('countdown()', 1000);
}
}
init();