> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://cisc1600.sketchpad.cc/sp/pad/view/ro.ed9GbHLJD2Y/rev.4282
 * 
 * authors: 
 *   Danny Gong

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



/*a Processing sketch of a basic game where a player's goal is to reach the bottom right square*/

int circlesize=20;                //player sprite radius
int circlex=15, circley=15;       //initial position of player sprite
color c1=color(0,255,255);        //color of rectangular obstructions (cyan)
color c2=color(0,200,0);          //color of start and end squares (green)
color c3=color(255,0,0);          //color of triangles (red)
color c4=color(255,0,255);        //color of player sprite (magenta)

Box box;                          //object: horizontal moving box
Boxvert boxvert;                  //object: vertical moving box

void setup() 
{  
    background=0;
    size(400, 300);
    noStroke();
    smooth();
    box = new Box();
    boxvert = new Boxvert;
} 

void draw() 
{ 
    background(0);
    
    //draw static rectangular obstructions
    fill(c1);
    rect(0,120,250,20);
    rect(0,200,40,20);
    rect(280,200,120,20);
    rect(100,30,20,60);
    rect(185,120,20,50);
    rect(70,220,180,20);
    rect(230,0,20,90);   
    rect(165,30,20,100);
    rect(230,220,20,50);
    rect(350,250,20,50);
    
    //draw triangular, secondary obstructions
    fill(c3);
    triangle(110, 5, 100, 20, 120, 20);
    triangle(265, 122, 255, 137, 275, 137);
    triangle(290, 122, 280, 137, 300, 137);
    triangle(315, 122, 305, 137, 325, 137);
    triangle(15, 151, 5, 166, 25, 166);
    triangle(15, 171, 5, 186, 25, 186);
    triangle(265, 222, 255, 237, 275, 237);
    triangle(80, 245, 70, 260, 90, 260);
    triangle(140, 270, 130, 285, 150, 285);
    
    //draw starting zone
    fill(c2);
    rect(0,0,30,30);
    
    //draw target (finish) zone
    fill(c2);
    rect(370,270,30,30);
    
    //draw player sprite
    fill(c4);
    ellipse(circlex,circley,circlesize,circlesize);
    
    //functions to return sprite to start position if it runs into triangle
    if (circlex>=100 &&circlex<120 && circley>=0 && circley<25)
    {
        circlex=15;
        circley=15;
    }
    if (circlex>=255 && circlex<325 && circley>=122 && circley<137)
    {
        circlex=15
        circley=15;
    }
    if (circlex>=5 && circlex<25 && circley>=151 && circley<186)
    {
        circlex=15;
        circley=15;
    }
    if (circlex>=255 && circlex<275 && circley>=222 && circley<237)
    {
        circlex=15;
        circley=15;
    }
    if (circlex>=70 && circlex<90 && circley>=245 && circley<260)
    {
        circlex=15;
        circley=15;
    }
    if (circlex>=130 && circlex<150 && circley>=270 && circley<285)
    {
        circlex=15;
        circley=15;
    }
    
    //functions to draw and animate horizontal moving box
    box.draw();
    box.update();
    
    //functions to draw and animate vertical moving box
    boxvert.draw();
    boxvert.update();
}

class Box
{
    color c=color(255,204,0);
    int x=40, y=200, vx=5;
    
        void draw()
        {
            fill(c);
            rect(x, y, 80, 20);
        }
    
        void update()
        {
            x+=vx;
            if (x>=width-200 || x<40)
                vx=-vx;       
        }
}

class Boxvert
{
    color c=color(255,0,0);
    int x=330, y=20, vy=3;
    
        void draw()
        {
            fill(c)
            rect(x, y, 20, 80);
        }
        
        void update()
        {
            y+=vy;
            if (y>=height-180 || y<0)
                vy=-vy;
        }
}

//functions to move player sprite via arrow keys 
void keyPressed()
{
    if (key==CODED && keyCode==UP)
        circley-=10;
    else if (key==CODED && keyCode==DOWN)
        circley+=10;
    else if (key==CODED && keyCode==LEFT)
        circlex-=10;
    else if (key==CODED && keyCode==RIGHT)
        circlex+=10;
}