const buffer = Graphics.createArrayBuffer(61, 61, 2, { msb: true });
const eye = {
  width: buffer.getWidth(),
  height: buffer.getWidth(),
  bpp: 2,
  transparent: 2,
  buffer: buffer.buffer,
};
function getRock() {
  return {
  width: 240,
  height: 160,
  bpp: 2,
  buffer: require("heatshrink").decompress(atob("[encoded data, 8220 characters]"))
};
}

let px = 0;
let py = 0;
let vx = 0;
let vy = 0;
let ax = 0;
let ay = 0;
let gx = 0;
let gy = 0;

function draw() {
  vx += ax;
  vy += ay;
  px += vx;
  py += vy;

  const pp = Math.sqrt(px * px + py * py);

  if (pp > 14) {
    const vv = Math.sqrt(vx * vx + vy * vy);
    const alpha = Math.atan2(px, py);
    const ratio = 14 / pp;

    px *= ratio;
    py *= ratio;
    vx = -0.9 * vv * Math.sin(alpha);
    vy = -0.9 * vv * Math.cos(alpha);
  }

  buffer.setColor(2);
  buffer.fillRect(0, 0, 60, 60);
  buffer.setColor(3);
  buffer.fillCircle(30, 30, 30);
  buffer.setColor(0);
  buffer.fillCircle(30 + px, 30 + py, 16);

  g.reset(); // ensure we're drawing in white
  g.drawImage(eye, 55, 90);
  g.drawImage(eye, 125, 90);
}

Bangle.setLCDPower(1);
Bangle.setLCDTimeout(0);

g.clear(1); // ensure we're drawing in white
g.drawImage(getRock(), 0, 40);

Bangle.loadWidgets();
Bangle.drawWidgets();

Bangle.on('accel', (accel) => {
  gx += 0.1 * (accel.x - gx);
  gy += 0.1 * (accel.y - gy);
  ax = 90 * gx - 100 * accel.x;
  ay = 90 * gy - 100 * accel.y;
  draw();
});
