     { 0xFE, 0xFE, 0xFE, 0xE2, 0xBE, 0xBE, 0xE2, 0xFE },
     { 0xFE, 0xC6, 0xBE, 0xBE, 0xC6, 0xFE, 0xFE, 0xFE },
     { 0xFE, 0xFE, 0xC6, 0xBE, 0xBE, 0xC6, 0xFE, 0xFE },
-    { 0xFE, 0xFE, 0xFE, 0xC6, 0xBE, 0xBE, 0xC6, 0xFE },
+    { 0xFE, 0xFE, 0xFE, 0xC6, 0xBE, 0xBE, 0xC6, 0xFE }
+};
+
+PROGMEM static const uint8_t imgCookie[4] = { // 4x4
+    0x06, 0x09, 0x09, 0x06
+};
+
+PROGMEM static const uint8_t imgFigures[10][16] = { // 8x12 x10
+    { 0x78, 0xFC, 0xFE, 0x86, 0xFE, 0xFC, 0x78, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00 },
+    { 0x8C, 0x8C, 0xFE, 0xFE, 0xFE, 0x86, 0x80, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00 },
+    { 0x0C, 0x86, 0xE6, 0xFE, 0xBE, 0xDC, 0xC0, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x00 },
+    { 0x04, 0x26, 0x36, 0x3E, 0xFE, 0xF4, 0xE0, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00 },
+    { 0xE0, 0xF0, 0xD8, 0xFC, 0xFE, 0xFE, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x00, 0x00 },
+    { 0x38, 0x3E, 0x36, 0x76, 0xF6, 0xE6, 0xE6, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00 },
+    { 0x78, 0xFE, 0xFF, 0x9F, 0xF8, 0xF8, 0x70, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00 },
+    { 0x0E, 0x0E, 0x06, 0xC6, 0xF6, 0x7E, 0x06, 0x00, 0x00, 0x00, 0x07, 0x07, 0x03, 0x00, 0x00, 0x00 },
+    { 0xEE, 0xFF, 0x3F, 0x7D, 0xF9, 0xFF, 0xE6, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 },
+    { 0x3C, 0x7E, 0x7E, 0xE6, 0xFE, 0xFC, 0x7C, 0x00, 0x00, 0x02, 0x07, 0x07, 0x03, 0x01, 0x00, 0x00 }
+};
+
+PROGMEM static const uint8_t imgTen[12] = { // 12x8
+    0x08, 0x08, 0x68, 0x98, 0x08, 0x00, 0xC0, 0x70, 0x28, 0xC0, 0x80, 0x00
+};
+
+PROGMEM static const byte soundStart[] = {
+    0x90, 0x44, 0x00, 0x7D, 0x90, 0x43, 0x00, 0x7D, 0x90, 0x42, 0x00, 0x7D, 0x90, 0x41, 0x00, 0x7D,
+    0x90, 0x40, 0x00, 0x7D, 0x90, 0x41, 0x00, 0x7D, 0x90, 0x42, 0x00, 0x7D, 0x90, 0x43, 0x00, 0x7D,
+    0x90, 0x60, 0x00, 0x14, 0x90, 0x41, 0x00, 0x14, 0x90, 0x5B, 0x00, 0x14, 0x90, 0x56, 0x00, 0x14,
+    0x90, 0x51, 0x00, 0x14, 0x90, 0x4C, 0x00, 0x14, 0x90, 0x47, 0x00, 0x14, 0x90, 0x3C, 0x00, 0x14,
+    0x80, 0xF0
 };
 
 PROGMEM static const byte soundStep[] = {
-    0x90, 60, 0, 10, 0x80, 0xF0
+    0x90, 0x3C, 0x00, 0x0A, 0x80, 0xF0
 };
 
 PROGMEM static const byte soundCapture[] = {
-    0x90, 80, 0, 20, 0x90, 84, 0, 40, 0x90, 88, 0, 40, 0x80, 0xF0
+    0x90, 0x50, 0x00, 0x14, 0x90, 0x54, 0x00, 0x28, 0x90, 0x58, 0x00, 0x28, 0x80, 0xF0
+};
+
+PROGMEM static const byte soundOver[] = {
+    0x90, 0x37, 0x00, 0xC8, 0x90, 0x35, 0x00, 0xC8, 0x90, 0x37, 0x00, 0xC8, 0x90, 0x35, 0x00, 0xC8,
+    0x90, 0x32, 0x01, 0xF4, 0x80, 0xF0
+};
+
+PROGMEM static const uint8_t ledValues[] = {
+    LED_VALUE(2, 2, 2), LED_VALUE(4, 0, 0), LED_VALUE(0, 0, 4), LED_VALUE(3, 3, 0), LED_VALUE(0, 4, 0),
+    LED_VALUE(3, 1, 1), LED_VALUE(2, 1, 0), LED_VALUE(2, 0, 3), LED_VALUE(1, 3, 0), LED_VALUE(0, 2, 2)
 };
 
 MyArduboy   arduboy;
-int16_t     px, py;
-int16_t     cx[COOKIES], cy[COOKIES];
-int8_t      cvx[COOKIES], cvy[COOKIES];
-uint16_t    score;
-uint8_t     pc, pa;
+bool        isPlaying;
+int16_t     playerX, playerY;
+uint8_t     playerChr, playerAnim;
+COOKIE_T    cookie[COOKIES];
+int16_t     score, gameTime;
+uint8_t     ledRed, ledGreen, ledBlue, ledLevel;
 
 /*  For Debugging  */
 

    if (arduboy.buttonPressed(RIGHT_BUTTON)) vx++;
    if (arduboy.buttonPressed(UP_BUTTON))    vy--;
    if (arduboy.buttonPressed(DOWN_BUTTON))  vy++;
    pc = (vy + 1) * 3 + (vx + 1);
    int8_t vs = (vx != 0 && vy != 0) ? (SCALE * 3 / 8) : (SCALE / 2);
    vx *= vs;
    vy *= vs;
    if (px + vx >= PL_SIZE_S && px + vx < WIDTH_S - PL_SIZE_S) px += vx;
    if (py + vy >= PL_SIZE_S && py + vy < HEIGHT_S - PL_SIZE_S) py += vy;

    /*  Animation  */
    if (pc == 4) {
        pa = 0;
    } else {
        if (++pa >= ANIM_MAX) {
            arduboy.playScore2(soundStep, 1);
            pa = 0;
        }
    }
}

void newCookie(uint8_t idx)
{
    do {
        cx[idx] = random(CK_SIZE_S, WIDTH_S - CK_SIZE_S);
        cy[idx] = random(CK_SIZE_S, HEIGHT_S - CK_SIZE_S);
    } while (abs(px - cx[idx]) < NEW_DIFF_S && abs(py - cy[idx]) < NEW_DIFF_S);
    float d = random(2048) * PI / 1024.0;
    cvx[idx] = (int8_t) (cos(d) * (float) SCALE);
    cvy[idx] = (int8_t) (sin(d) * (float) SCALE);
}

void draw(void)
{
    arduboy.clear();
    arduboy.setCursor(0, 0);
    arduboy.print(score);
    int16_t dx = px / SCALE - PL_SIZE;
    int16_t dy = py / SCALE - PL_SIZE - (pa >= ANIM_MAX / 2);
    arduboy.drawBitmap(dx, dy, imgPlayer[pc], PL_SIZE * 2, PL_SIZE * 2, WHITE);
    for (uint8_t idx = 0; idx < COOKIES; idx++) {
        dx = cx[idx] / SCALE - CK_SIZE;
        dy = cy[idx] / SCALE - CK_SIZE;
        arduboy.drawRect2(dx, dy, CK_SIZE * 2, CK_SIZE * 2, WHITE);
    }
}

#define FPS             60
#define APP_TITLE       "RYSK"
#define APP_CODE        "OBN-Y00"
#define APP_VERSION     "0.03"
#define APP_RELEASED    "NOVEMBER 2019"

#define COOKIES         8
#define ANIM_MAX        16

#define PL_SIZE         4
#define CK_SIZE         2
#define SCALE           64
#define WIDTH_S         (WIDTH * SCALE)
#define HEIGHT_S        (HEIGHT * SCALE)
#define PL_SIZE_S       (PL_SIZE * SCALE)
#define CK_SIZE_S       (CK_SIZE * SCALE)
#define HIT_DIFF_S      PL_SIZE_S
#define NEW_DIFF_S      (PL_SIZE_S * 4)

#define GAME_DURATION   (FPS * 60)

#define IMG_TITLE_W     128
#define IMG_TITLE_H     16
#define IMG_START1_W    48
#define IMG_START1_H    16
#define IMG_START2_W    32
#define IMG_START2_H    16
#define IMG_OVER_W      64
#define IMG_OVER_H      16
#define IMG_FIGURE_W    8
#define IMG_FIGURE_H    12
#define IMG_TEN_W       12
#define IMG_TEN_H       8
#define CLOCK_R         9
#define CLOCK_X         (WIDTH - (CLOCK_R + 1))
#define CLOCK_Y         (HEIGHT - (CLOCK_R + 1))
#define LED_LEVEL_START 16
#define LED_VALUE(r, g, b)  ((r) * 25 + (g) * 5 + (b))

enum {
    ALIGN_LEFT = 0,
    ALIGN_CENTER,
    ALIGN_RIGHT,
};

/*  Typedefs  */

typedef struct {
    int16_t x, y;
    int8_t  vx, vy;
} COOKIE_T;

/*  Functions  */

static void initialize(void);
static void update(void);
static void initGame(void);
static void updateGame(void);
static void movePlayer(void);
static void moveCookie(COOKIE_T *pCookie);
static void newCookie(COOKIE_T *pCookie);
static bool isInRange(COOKIE_T *pCookie, int16_t range);

static void draw(void);
static void drawObject(int16_t x, int16_t y, uint8_t size, const uint8_t *bitmap);
static void drawStatus(void);
static int  drawFigure(int16_t x, int16_t y, int value, uint8_t align);
static void drawClock(int16_t x, int16_t y, uint8_t r, uint8_t sec);

/*  Variables  */

PROGMEM static const uint8_t imgTitle[256] = { // 128x16
    0x00, 0xF8, 0xFC, 0x00, 0x00, 0x00, 0x98, 0x98, 0x98, 0xF8, 0xF8, 0xDA, 0xC4, 0x0D, 0x0B, 0x06,
    0x00, 0x60, 0x60, 0xE0, 0xF0, 0xFC, 0x3C, 0x30, 0x30, 0xA0, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x80, 0xE0, 0xF0, 0x3C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x10, 0x90, 0xF0, 0xF8, 0x9E, 0x9E, 0x98, 0x18, 0x88, 0x80, 0x80, 0x00, 0x00, 0x00,
    0x00, 0x60, 0x60, 0x60, 0xFE, 0xFC, 0x30, 0x30, 0x30, 0x00, 0x08, 0x18, 0x38, 0x30, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xFC, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x10, 0x98, 0xD8, 0x78, 0x38, 0x18, 0x18, 0x08, 0x00, 0x00,
    0x40, 0xC0, 0x60, 0xF8, 0xFC, 0x64, 0x80, 0xC0, 0x60, 0x30, 0x30, 0x30, 0xF0, 0xC0, 0x00, 0x00,
    0x00, 0x7F, 0x7F, 0x38, 0x00, 0x30, 0x79, 0x49, 0x49, 0x7F, 0x3F, 0x30, 0x30, 0x60, 0x00, 0x00,
    0x00, 0x60, 0x7C, 0x1F, 0x07, 0x00, 0x00, 0x33, 0x31, 0x61, 0x61, 0x61, 0x61, 0x41, 0x00, 0x00,
    0x00, 0x70, 0x3E, 0x1F, 0x0F, 0x06, 0x1F, 0x7F, 0x70, 0x60, 0x60, 0x70, 0x38, 0x1C, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x21, 0x79, 0x7C, 0x66, 0x6F, 0x6F, 0x63, 0x61, 0x61, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x38, 0x7C, 0x4C, 0xFF, 0x7F, 0x02, 0x03, 0x43, 0x43, 0x63, 0x63, 0x7E, 0x3C, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x0E, 0x3F, 0x7F, 0x60, 0x60, 0x60, 0x60, 0x70, 0x38, 0x0C, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x3F, 0x38, 0x60, 0x60, 0x40, 0x00, 0x00, 0x00, 0x00,
    0x10, 0x38, 0x1E, 0xFF, 0xFF, 0x01, 0x01, 0x30, 0x78, 0x68, 0x68, 0x78, 0x3F, 0x3F, 0x60, 0x00
};

PROGMEM static const uint8_t imgStart1[96] = { // 48x16
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFC, 0x60, 0x60, 0x30, 0x20, 0x00, 0x00, 0x00,
    0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00,
    0x00, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0xE0, 0xC0, 0x80, 0x00, 0x00,
    0x00, 0x30, 0x78, 0x68, 0x4C, 0x6C, 0x68, 0x7F, 0x1F, 0x18, 0x30, 0x30, 0x60, 0x00, 0x00, 0x00,
    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
    0x00, 0x03, 0x0F, 0x3C, 0x70, 0x3C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x00, 0x00
};

PROGMEM static const uint8_t imgStart2[64] = { // 32x16
    0x00, 0x00, 0x00, 0x1C, 0x7C, 0xF0, 0x80, 0xC0, 0xE0, 0x60, 0x70, 0x30, 0x0C, 0x1A, 0x06, 0x04,
    0x00, 0x00, 0x00, 0x80, 0xE0, 0xF0, 0x3C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x3E, 0x7F, 0x63, 0x61, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x70, 0x3E, 0x1F, 0x0F, 0x06, 0x1F, 0x7F, 0x70, 0x60, 0x60, 0x70, 0x38, 0x1C, 0x00, 0x00
};

PROGMEM static const uint8_t imgOver[128] = { // 64x16
    0x00, 0x60, 0x60, 0x60, 0xFE, 0xFC, 0x30, 0x30, 0x30, 0x00, 0x08, 0x18, 0x38, 0x30, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xFC, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x10, 0x30, 0xB0, 0xB0, 0xB0, 0xFE, 0xFE, 0x90, 0x98, 0x10, 0x00, 0x00, 0x00, 0x00,
    0x00, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0xE0, 0xC0, 0x80, 0x00, 0x00,
    0x00, 0x38, 0x7C, 0x4C, 0xFF, 0x7F, 0x02, 0x03, 0x43, 0x43, 0x63, 0x63, 0x7E, 0x3C, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x0E, 0x3F, 0x7F, 0x60, 0x60, 0x60, 0x60, 0x70, 0x38, 0x0C, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x30, 0x79, 0x59, 0xD9, 0x59, 0x7F, 0x3F, 0x31, 0x31, 0x61, 0x40, 0x00, 0x00, 0x00,
    0x00, 0x03, 0x0F, 0x3C, 0x70, 0x3C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x00, 0x00
};

PROGMEM static const uint8_t imgPlayer[9][8] = { // 8x8 x9
    { 0xFE, 0xF2, 0xDE, 0xDE, 0xF2, 0xFE, 0xFE, 0xFE },
