> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.WW5OGXb6yfT/rev.1768
 * 
 * authors: 
 *   Benjamin Y

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/*
This sketch builds on a prior work, "Blur", created by [unnamed author] & Mitch Davis & Cerulean & Sarah & Charlie Wallace & Charles Wallace & Matthew Laitinen
http://studio.sketchpad.cc/sp/pad/view/ro.9P2AwQrF-tPLz/rev.7280
*/

// 16 rows



// Pressing Control-R will render this sketch.

boolean left = false;
boolean right = false;
GameObject player = new GameObject(0,300,275);
boolean[] keysPressed;
GameObject[] blocks = new GameObject[300];
int blockCount = 0;
int lvl = 9;
boolean checkpoint = false;

int[][][] level = {
    //Level 1
    {
       {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
       {2,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 2
    {
       {0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
       {2,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 3
    {
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
       {2,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,2},
       {2,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,7,0,0,0,2,0,2},
       {2,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,7,0,0,0,0,2,0,2},
       {2,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,7,0,0,0,0,0,2,1,2},
       {2,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,7,0,0,0,0,0,0,2,1,2},
       {2,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,7,0,0,0,0,0,0,0,2,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,7,0,0,0,0,0,0,0,0,2,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,7,0,0,0,0,0,0,0,0,0,2,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,5,0,7,0,0,0,0,0,0,0,0,0,0,2,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,7,7,0,0,0,0,0,0,0,0,0,0,2,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,7,0,0,0,0,0,0,0,0,0,2,2,2,0,0,2},
       {2,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,2},
       {2,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,0,0,4,1,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },

    //Level 4
    {
       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,6,6,0,6,0,0,0,0,0,0,0,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,6,0,6,0,0,6,6,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0,2},
       {2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 5
    {
       {0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,3,0,0,0,0,0,2,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 6
    {
       {0,0,0,0,0,0,6,6,6,0,6,0,0,0,0,0,0,0,0,6,0,0,6,6,0,6,0,0,0,0},
       {2,0,0,0,0,0,6,6,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,6,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,2,2,0,0,0,7,0,0,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,2,2,0,0,0,7,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,2},
       {2,2,0,0,2,0,0,7,0,0,0,0,0,0,0,0,5,7,0,0,0,7,7,0,0,0,0,0,0,2},
       {2,0,0,2,2,0,7,1,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0,7,0,0,0,0,0,2},
       {2,2,0,0,2,1,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,2},
       {2,0,0,2,2,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,2},
       {2,2,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,0,0,2},
       {2,3,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 7
    {
       {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,7,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,6,0,7,7,7,7,7,7,0,7,7,7,7,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,5,7,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,7,7,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,4,2},
       {2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1,2},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,2,2,2,2}
    },
    //Level 8
    {
       {0,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,7,0,0,0,0,7,0,7,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,7,6,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,2},
       {2,6,0,0,0,0,0,0,0,0,7,0,0,0,0,7,7,7,0,7,7,7,0,7,7,0,0,6,0,2},
       {2,6,0,0,0,0,0,0,0,7,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,2},
       {2,6,0,0,0,0,0,0,7,0,0,0,0,6,7,0,0,0,0,0,0,0,0,0,0,0,0,6,0,2},
       {2,6,0,0,0,0,0,7,0,0,0,0,0,6,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,7,0,0,0,0,0,0,6,0,2,7,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,2,6,0,2,7,7,0,0,0,0,0,0,0,0,0,0,4,2},
       {2,6,2,3,0,0,0,0,0,1,0,0,2,6,0,5,0,0,0,7,0,0,0,0,0,0,6,0,1,2},
       {2,6,2,2,2,2,2,2,2,2,2,2,2,6,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 9
    {
       {0,6,0,0,0,0,0,0,0,0,6,0,0,6,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0},
       {2,6,0,0,0,0,0,0,0,0,6,0,0,6,0,0,0,0,0,0,7,7,7,7,7,7,7,7,2,2},
       {2,6,0,0,0,0,0,0,0,0,6,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,6,0,0,6,0,0,0,7,0,7,0,0,7,7,7,7,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,0,0,7,0,7,0,0,0,2,0,0,0,0,2,2,2},
       {2,6,0,0,0,0,0,0,0,0,6,0,0,6,0,7,0,0,0,0,0,0,2,0,0,0,0,0,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,0,6,7,0,0,0,0,0,0,0,0,7,7,2,2,6,0,2},
       {2,6,0,0,0,0,0,0,0,0,0,0,7,6,0,0,0,0,0,0,0,0,0,0,0,0,0,6,7,7},
       {2,6,0,0,0,0,0,0,0,7,7,0,0,6,0,0,0,2,7,2,7,2,7,2,7,2,7,6,0,2},
       {2,6,0,0,0,0,0,0,7,0,0,0,0,6,7,0,0,7,2,7,2,7,2,7,2,7,2,0,0,2},
       {2,6,5,0,0,0,0,7,0,0,0,0,0,6,0,2,0,0,7,2,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,3,0,7,0,0,0,0,0,0,6,0,2,7,0,0,7,0,0,0,0,0,0,0,0,0,2},
       {2,6,0,0,2,0,0,0,0,0,0,0,2,6,0,2,7,7,0,2,0,0,0,0,0,0,0,0,4,2},
       {2,6,2,0,0,0,0,0,0,1,0,0,2,6,0,0,0,0,0,7,0,0,0,0,0,0,6,0,1,2},
       {2,6,2,2,2,2,2,2,2,2,2,2,2,6,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
    },
    //Level 10
    {
       {0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0},
       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,7,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,6,0,4,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,7,0,0,0,0,7,7,0,0,7},
       {2,0,0,0,0,0,0,0,0,2,2,0,2,2,2,2,2,0,0,0,0,0,7,0,7,0,0,7,7,7},
       {2,0,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,7,0,7,7,0,0,2,0,6},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,6,6,6,6,6,6,7,6,2},
       {2,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,7,0,6,6,6,6,6,6,6,6,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,7,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,2,0,2,0,0,5,0,6,7,0,0,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,1,0,0,2,0,2,0,2,0,0,0,6,7,0,0,0,0,0,0,0,6,6,6,6,6,6,6,6,2},
       {2,2,2,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,6,6,6,6,6,6,6,6,2}
    },
    // Level 11
        {
       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,6,0,0,0},
       {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,6,0,7,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,7,0,7,7,7,0,0,6,0,0,0},
       {2,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,1,0,2,0,7,0,0,0,0,7,7,0,4,7},
       {2,0,0,0,0,0,0,0,0,2,2,0,2,2,2,2,7,0,0,0,0,0,7,0,7,0,0,0,7,7},
       {2,0,2,2,2,2,2,2,2,0,0,2,0,1,0,0,1,0,7,0,0,7,0,0,7,0,0,0,0,6},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,7,0,0,7,6,6,6,6,6,6,7,6,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,7,0,0,7,0,6,6,6,6,6,6,6,6,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,5,1,0,7,1,0,7,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,0,0,0,2,2,2,1,7,0,1,7,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,0,0,2,0,0,0,1,0,0,7,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,1,0,1,0,0,0,1,0,7,1,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,0,2,2,2,0,0,0,1,7,0,1,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,0,0,0,0,0,2,2,2,1,0,0,7,7,7,0,1,0,0,0,0,6,6,6,6,6,6,6,6,0},
       {2,1,0,0,2,0,2,2,2,1,0,0,6,6,0,0,1,0,0,5,0,6,6,6,6,6,6,6,6,2},
       {2,2,2,0,0,0,0,2,2,7,2,2,2,1,2,2,1,2,2,2,2,2,0,2,6,2,0,2,6,2}
       },
    //GAME OVER
    {
       {0,0,0,0,0,1,6,6,0,1,0,0,1,6,1,0,0,1,0,0,6,0,0,6,0,0,1,6,0,0},
       {2,0,0,0,0,1,0,0,0,1,0,0,1,6,1,0,0,1,0,0,6,0,0,6,0,0,1,6,0,2},
       {2,0,0,0,0,6,0,0,0,0,0,0,0,6,1,0,0,1,0,0,6,0,0,6,0,0,1,6,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,6,0,7,6,7,0,6,0,0,1,6,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,6,0,0,6,0,0,6,7,0,1,6,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,6,0,0,0,0,0,0,0,7,1,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,0,7,0,0,0,7,0,6,0,0,0,0,0,0,0,0,7,0,0,2},
       {2,0,0,0,0,0,0,0,0,0,7,7,0,0,7,0,7,6,0,0,0,0,0,0,0,0,1,0,0,2},
       {2,0,0,0,0,0,0,0,7,0,0,0,0,7,0,0,0,7,0,7,7,7,0,7,0,0,1,7,0,2},
       {2,0,0,0,0,0,0,0,7,7,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,3,2},
       {2,0,0,2,0,0,0,2,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,2},
       {2,0,0,0,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2},
       {2,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,1,0,0,2},
       {2,0,0,0,0,0,0,7,7,2,0,0,2,7,2,0,0,2,7,6,0,0,0,0,0,0,1,0,0,2},
       {2,0,0,0,0,2,0,0,0,0,2,2,0,0,0,2,2,0,0,7,6,0,0,0,0,0,1,0,5,2},
       {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,7,7,1,2,2,2}
    }
};


void setup() {  // this is run once.   
    
    // set the background color
    background(255);
    
    keysPressed = new boolean[255];
    
    // canvas size (Variable aren't evaluated. Integers only, please.)
    size(600, 300); 
      
    // smooth edges
    smooth();
    
    // limit the number of frames per second
    frameRate(30);
    
    // set the width of the line. 
    strokeWeight(12);
    
    stroke(200,200,255,0);
    fill(100,100,255,255);
    //Clear the background, somewhat
    rect(0,0,600,300);
    
    loadLevel(true);
} 

void loadLevel(boolean reset) {
    
    if (reset) {
        //Set the player  out of collision range first
        player = new GameObject(-100,-100);
        blockCount = 0;
        checkpoint = false;
    }
    
    for (int row = 0; row < level[lvl].length(); row++) {
        for (int col = 0; col < level[lvl][row].length(); col++) {
            int type = level[lvl][row][col];
            if (type != 0 && type != 3) {
                blocks[blockCount++] = new GameObject(type, col * 21, row * 20);
            } else if (type==3){
                 player = new GameObject(0, col * 21, row * 20);  
            }
        }    
    }    
}

void draw() {  // this is run repeatedly.  
    stroke(200,200,255,0);
    fill(100,100,255,20);
    //Clear the background, somewhat
    rect(0,0,600,300);
    
        
    if (keysPressed[SHIFT]) {
        lvl++;
        loadLevel(true);
        keysPressed[SHIFT] = false;
    }
        
    
    
    
    //Draw our person
    stroke(0,0,0,255);
    
    for (int j = 0; j < blockCount; j++) {
        blocks[j].update();
        blocks[j].draw();   
    }
    
    player.update();
    player.draw();
    
}


void keyPressed() {
    keysPressed[keyCode] = true;
}

void keyReleased() {
    keysPressed[keyCode] = false;
}

class GameObject {
    int x = 300;
    int y = 0;
    float velocity;  
    float gravity = -0.7;
    int width;
    int height;
    int offsetX;
    int offsetY;
    
    boolean canJump = true;
    int type = 0;    //0 = player, 1 = block, 2 = floatingBlock
    
    public GameObject(int t, int xPos, int yPos) {
        x = xPos;
        y = yPos;
        type = t;
        
        if (t == 1 || t == 2 || type == 6 || type==7) {
            width = height = 20;
        }
        if (t == 0) {
            width = 5; height = width * 2.5;
        }
        if (t == 4 || t == 5) {
            width = 5; height = 20;
        }
        
        offsetX = -width/2;
        offsetY = -height;
    }
    
    public void draw() {
        if (type == 0) {
            drawPlayer();   
        } else if (type == 1 || type == 2 || type==6 || type==7) {
             drawBlock();
        } else if (type == 4 || type == 5) {
            drawFlag();    
        }
        
        //drawCollisionBox();  
    }
    
    public void jump() {
        if (canJump) {
            canJump = false;
            velocity = 6.2;    
        }    
    }
    
    public void update() {
        if (type==2 || type == 7 || type==4 || type==5) {return;}
        if (!checkpoint && (type==6 || type==7)) {return;}
        if (checkpoint && type==5) {return;}
        velocity += gravity;
        y -= velocity;
        if (y > 300 && this==player) { loadLevel(true); }

        //Check collision with block
        if (isCollidingWithBlock()>-1 || (this != player && isCollidingWith(player))) {
            y += velocity;
            this.velocity = 0;    
            canJump = true;       
        }
        
        
        //X movement
        if (type == 0) {
            int xChange = 0;
            if (keysPressed[LEFT]) { xChange -= 4; }
            if (keysPressed[RIGHT]) { xChange += 4; }
            if (keysPressed[UP]) { player.jump(); }
            
            x += xChange;
            int direction = 1;
            if (xChange < 0) { direction = -1; }
            int index = isCollidingWithBlock();
            while (index > -1) {
                if (blocks[index].type == 2 || blocks[index].type==7) {
                    //Not able to push this
                    x -= xChange;   
                    xChange = 0;
                }
                //Try to push the block
                
                for (int j = 0; j != xChange; j+=direction ) {
                    blocks[index].x += direction ;
                    
                    //Did we push into another block?
                    if (blocks[index].isCollidingWithBlock() > -1) {
                        //Can't push it anymore
                        //reset it, and limit player movement
                        blocks[index].x -= direction;
                        
                        //Undo player movement...
                        x -= xChange;
                        
                        //Then move them how far the block moved
                        x += j;  
                        break;
                    }  
                    
                    if (!isCollidingWith(blocks[index])) { break; }
                    
                }
            
                //Before we loop again, re check collision
                index = isCollidingWithBlock();    
            }
        }
    }
    
    int isCollidingWithBlock() {

        for (int i =  0; i < blockCount; i++) {
            //Check for collision
            
            if (this != blocks[i] && blocks[i].type != 0) {
                //If this is a checkpoint block and the checkpoint hasnt
                //been reached, skip it
                if (!checkpoint && (blocks[i].type == 6 || blocks[i].type==7)) { continue; } 
                
                if (this.isCollidingWith(blocks[i])) {
                    if (blocks[i].type == 4) {
                            if (this == player) {
                                lvl++;
                                loadLevel(true);
                                return;
                            }
                            continue;
                        } else if (blocks[i].type == 5) {
                            if (this == player) {
                                checkpoint = true;
                            }
                            continue;    
                        }   
                    return i;
                }
            }
        }
        return -1;
    }
    
    void drawPlayer() {
        //Width of character is size
        int size = width;
        int halfSize = size / 2;
    
        strokeWeight(2);
        stroke(0,0,0,255);
    
        //Legs
        line(x-halfSize, y, x, y-size);
        line(x+halfSize, y, x, y-size);
    
        //Body
        line(x, y-size,x,y-(2*size));
    
        //arms
        line(x-halfSize, y-size, x, y-(2*size));
        line(x+halfSize, y-size, x, y-(2*size));
    
        //head
        ellipse(x, y - (2*size) - (halfSize/1.8), halfSize, halfSize);
    }
    
    void drawBlock() {
        
        if (!checkpoint && (type==6 || type==7)) { return; }
        
        strokeWeight(3);
        stroke(60,20,0,255);
        fill(200,100,0,255);
        
        if (type==2 || type == 7) {
            fill(100,50,0,255);
        }
        rect(x-width/2,y-height,width,height);
        
        if (type==2 || 7) {
            strokeWeight(5);
            line(x,y-height/2,x,y-height/2);    
        } 
        fill(0,0,0,0);
        
    }
    
    void drawFlag() {
        strokeWeight(2);
        stroke(0,0,0,255);
        fill(255,0,0,255);
        if (type==5) { fill(255,255,0,255); }
        
        int top = y - height;
        int mid = (top + y) / 2;
        int midmid = (mid + top) / 2;
        int right = x + width;
        int left = x - width;
        
        line(right, y, right, top);
        triangle(right, top, left, midmid, right, mid);
        
        fill(0,0,0,0);
        
    }   
    
    boolean isCollidingWith(GameObject other) {
      return !(other.left() >= this.right() || 
           other.right() <= this.left() || 
           other.top() >= this.bottom() ||
           other.bottom() <= this.top());
    }
    
    void drawCollisionBox() {
        stroke(0,0,0,255);
        rect(left(),bottom(),right()-left(), top()-bottom());    
    } 
    
    int left() { return x + offsetX; };
    int right() { return x + width + offsetX;}
    //Note, "top" is the highest point, but drawn as the bottom
    int top() { return y + offsetY; }
    int bottom() { return y+height + offsetY; }       
}