-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
186 lines (161 loc) · 4.27 KB
/
app.js
File metadata and controls
186 lines (161 loc) · 4.27 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
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
Bangle.setLCDBrightness(1);
Bangle.setLCDMode("doublebuffered");
let points = 0;
let level = 1;
let levelSpeedStart = 0.8;
let nextLevelPoints = 20;
let levelSpeedFactor = 0.2;
let counterWidth = 10;
let gWidth = g.getWidth() - counterWidth;
let gHeight = g.getHeight();
let counter = 160;
let counterMax = 160;
let ballDims = 20;
let ballx = g.getWidth() / 2 - ballDims;
let bally = g.getHeight() / 2 - ballDims;
let dotx = g.getWidth() / 2;
let doty = g.getWidth() / 2;
let ballBuzzTime = 5;
let ballSpeedFactor = 40;
let redrawspeed = 5;
let dotwidth = 5;
let running = false;
let drawInterval;
let xBuzzed = false;
let yBuzzed = false;
let BALL = require("heatshrink").decompress(
atob(
"ikUyAROvkQ3v4405AIYHBGq9KpMhktz1/W7feAJAtBEZ9jhkhs0ZgkQ8lKxW+jAdB516627E4X8AIPWzelmolKlpJBjMFEYIpC4kQ0YBBqWKynTFYPe7gpE3ec6gnHkNFrXL7372u2E4WjhGCAIliqWrUIPeKoIpB7h9HoUoqWq999///FIJ3BhGDEIIBBgFBAoWCoUI3vY62aQIW7ymSJooLBEoIADwkQEYVhEoInEGIOjR4O1y/OrIrBUYdr198iH/74nF88cE4gpCA4MY8k59CzBAINrx2164nBtduufPWYIlF++/xkxNoMAAIJPBoSdB52a30ZkNGE4IvBoUpwkxLIOMyWEmAmE7+MqKbEsLLBH4P3zw1BAYJFBFIMY8sQ4cx44nB0tVHYITBEoO967lDgDDC1tVQ4QBD37xBjMmJ4I3BE4IxBPoOMuSrBHYL1BJYbrDvfPLoYBD889jMlEoMhkpJBwkRE4O+jB7B405LoJPEYYUx0xPG7/3vxvBmOnrXsdIOc6jxBE4JfBvfwHIafDFoMRgh3H99+zsUDIOMqWU2YlBAAO1/AnBToN76EhgpTBFYKPBGIIhBEovOrWliuc2YlBE4oABE4etu2UyVrpqJBMoKvBEIPnjvWze97ATBE4YPBEopRC64BC27nBzn0znTAIOlimtq21y4BCEoM1HYOMqIVBE44AB0tVCYIBEigVBE4U1GYIFBymywkwEoJzHABIRBMIIXBWoIDCqOEmOEiABCmIjPAA51BFoVSEoUwAIIZNA"
)
);
function reset() {
g.clear();
level = 1;
points = 0;
ballx = g.getWidth() / 2 - ballDims;
bally = g.getHeight() / 2 - ballDims;
counter = counterMax;
createRandomDot();
drawInterval = setInterval(play, redrawspeed);
running = true;
}
function collide() {
try {
Bangle.buzz(ballBuzzTime, 0.8);
} catch (e) {}
}
function createRandomDot() {
dotx = Math.floor(
Math.random() * Math.floor(gWidth - dotwidth / 2) + dotwidth / 2
);
doty = Math.floor(
Math.random() * Math.floor(gHeight - dotwidth / 2) + dotwidth / 2
);
}
function checkIfDotEaten() {
if (
ballx + ballDims > dotx &&
ballx <= dotx + dotwidth &&
bally + ballDims > doty &&
bally <= doty + dotwidth
) {
collide();
createRandomDot();
counter = counterMax;
points++;
if (points % nextLevelPoints == 0) {
level++;
}
}
}
function drawLevelText() {
g.setColor("#26b6c7");
g.setFontAlign(0, 0);
g.setFont("4x6", 5);
g.drawString("Level " + level, 120, 80);
}
function draw() {
//bg
g.setColor("#71c6cf");
g.fillRect(0, 0, g.getWidth(), g.getHeight());
//counter
drawCounter();
//draw level
drawLevelText();
//dot
g.setColor("#ff0000");
g.fillCircle(dotx, doty, dotwidth);
//ball
g.drawImage(BALL, ballx, bally);
g.flip();
}
function drawCounter() {
g.setColor("#000000");
g.fillRect(g.getWidth() - counterWidth, 0, g.getWidth(), gHeight);
if(counter < 40 ) g.setColor("#fc0303");
else if (counter < 80 ) g.setColor("#fc9803");
else g.setColor("#0318fc");
g.fillRect(
g.getWidth() - counterWidth,
gHeight,
g.getWidth(),
gHeight - counter
);
}
function checkCollision() {
if (ballx < 0) {
ballx = 0;
if (!xBuzzed) collide();
xBuzzed = true;
} else if (ballx > gWidth - ballDims) {
ballx = gWidth - ballDims;
if (!xBuzzed) collide();
xBuzzed = true;
} else {
xBuzzed = false;
}
if (bally < 0) {
bally = 0;
if (!yBuzzed) collide();
yBuzzed = true;
} else if (bally > gHeight - ballDims) {
bally = gHeight - ballDims;
if (!yBuzzed) collide();
yBuzzed = true;
} else {
yBuzzed = false;
}
}
function count() {
counter -= levelSpeedStart + level * levelSpeedFactor;
if (counter <= 0) {
running = false;
clearInterval(drawInterval);
setTimeout(function(){ E.showMessage("Press Button 1\nto restart.", "Gameover!");},50);
}
}
function accel(values) {
ballx -= values.x * ballSpeedFactor;
bally -= values.y * ballSpeedFactor;
}
function play() {
if (running) {
accel(Bangle.getAccel());
checkCollision();
checkIfDotEaten();
count();
draw();
}
}
setTimeout(() => {
reset();
drawInterval = setInterval(play, redrawspeed);
setWatch(
() => {
if(!running) reset();
},
BTN1,
{ repeat: true }
);
running = true;
}, 10);