> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://cisc1600.sketchpad.cc/sp/pad/view/ro.sZvERqFZ2g5/rev.76
 * 
 * authors: 
 *   Michael Mandel

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



/* @pjs preload="/static/uploaded_resources/p.20231/donkey.png"; */

/**********************************************************
  Karen C. Aragon
  Project 2: Processing
  CIS 1600 Professor Meyers
  
  Title: Crazy Donkey
  Description: The user is a donkey and is able
               to move around the screen. Depending on
               what letter is pressed shapes will shoot out
               like lasers from the donkey's eyes.
  
  Due: 11/15/10
**************************************************************/

/**************************** VARIABLES ***********************/
// for triangles in setup screen
PFont title, start;
int center1x, center1y, a1x, a1y, b1x, b1y, c1x, c1y;
int center2x, center2y, a2x, a2y, b2x, b2y, c2x, c2y;
int center3x, center3y, a3x, a3y, b3x, b3y, c3x, c3y;
int triangle_scale = height;
boolean scale_up = false;

String proceed;
Star stars[];
int STARS = 300;
Donkey donkey;
Triangles triangles;
Rectangle rectangle;
Ellipses ellipses;

/******************************* SETUP *************************/
void setup() {
  size(800,500);
  noStroke();
  smooth();
  // title = loadFont("GillSans-UltraBold-48.vlw");
  // start =  loadFont("Consolas-Bold-48.vlw"); //start button on startscreen
  title = createFont("GillSans-UltraBold", 48);
  start = createFont("Consolas-Bold", 48);

  stars = new Star[STARS];
      for ( int i =0; i < STARS; i++) {
          stars[i] = new Star( random( width ), random( height ));
      }

  PImage dimg = loadImage("/static/uploaded_resources/p.20231/donkey.png");
  donkey = new Donkey( 100, height/2, 3, dimg);
  
  triangles = new Triangles(10,10);
  triangles.active = false;
 
  rectangle = new Rectangle(10,10);
  rectangle.active = false;
  
  ellipses = new Ellipses(10,10);
  ellipses.active = false;

  proceed = "start_screen";
  
  frameRate(35);
}

/******************************* DRAW **************************/
void draw() {
   if(proceed=="start_screen"){
       startscreen();

    //if play button is press then instructions page show up 
    if(mousePressed && mouseX> 300  && mouseX < 450 && mouseY > 200 && mouseY < 300){
      instructions();
      proceed = "play";
     }
  }//end startscreen
  
  if(proceed =="play") {
    if(mousePressed && mouseX >300 && mouseX <450 && mouseY >300 && mouseY < 450){
     background(0);
     proceed = "shoot";
    }
  } //end play
    
  if(proceed == "shoot"){
   background(0);
   
   starfield(); 
   donkey.draw();
   triangles.draw();
   rectangle.draw();
   ellipses.draw();
  
    //controls motion of the donkey
    if ( keyPressed == true && key == CODED ) {
       if ( keyCode == UP ) {
         donkey.up();
       } 
       else if ( keyCode == DOWN ) {
         donkey.down();
       }
       else if( keyCode == LEFT ){
         donkey.left();
       }
       else if( keyCode == RIGHT ){
         donkey.right();
       }
    }  
  }//end shoot
}
 
/***************************  FUNCTIONS  ************************/

void startscreen() {
  background(255);
  textFont(title, 15);
  fill(#33CCFF);
  text("WELCOME TO",width/4, height/4);
  textFont(title, 30);
  fill(0);
  text("/**CRAZZZY DONKEY**/",width/4, height/3);
  textFont(start, 13);
  text("(PRESS THE SQUARE TO PLAY)",width/3 + 40, height/2 - 50);
  
  fill(#33CCFF);
  rect(360, 220, 55, 55);
  
  //begin triangles
  fill(random(250),random(250),random(250));
  center1x= width/4;
  center1y= height - 100;
  a1x= center1x;
  b1x = center1x - triangle_scale;
  c1x = center1x + triangle_scale;
  a1y = center1y - triangle_scale;
  b1y = center1y + triangle_scale;
  c1y = b1y;
  triangle(a1x,a1y,b1x,b1y,c1x,c1y);
  center1x = center1x + width/4;

  center2x= width/2;
  center2y= height - 100;
  a2x= center2x;
  b2x = center2x - triangle_scale;
  c2x = center2x + triangle_scale;
  a2y = center2y - triangle_scale;
  b2y = center2y + triangle_scale;
  c2y = b2y;
  triangle(a2x,a2y,b2x,b2y,c2x,c2y);
  
  center3x= width/2 + width/4;
  center3y= height - 100;
  a3x= center3x;
  b3x = center3x - triangle_scale;
  c3x = center3x + triangle_scale;
  a3y = center3y - triangle_scale;
  b3y = center3y + triangle_scale;
  c3y = b3y;
  triangle(a3x,a3y,b3x,b3y,c3x,c3y);
  
  if(scale_up)
      triangle_scale = triangle_scale +2;
  else
     triangle_scale = triangle_scale -2;
     
  if(triangle_scale < 0)
      scale_up = true;
 
  if(triangle_scale > 100) 
      scale_up = false;
} // end startscreen

void instructions() {
  background(0);
  textAlign(LEFT);
  textFont(title, 15);
  fill(#33CCFF);
  text("INSTRUCTIONS:",width/4, height/4);
  fill(#CCCCCC);
  textFont(start, 13);
  text("Let your donkey go crazy in space! Shoot lazers out of its eyes!",width/4, 140);
  text("Use Arrow Keys to navigate the donkey though space", width/4, 180 );
  text("Press T for Triangles", width/4, 200 );
  text("Press R for Rectangles.",width/4, 220 );
  text("Press E for Ellipses.",width/4, 240 );
  text("Press C for clear the screen.",width/4, 260 );
  text("PRESS THE SQUARE TO PLAY!",width/4, 300 );
  fill(#33CCFF);
  rect(360, 320, 55, 55);

} // end instructions

void starfield() {
  strokeWeight(3);
  for ( int i =0; i < STARS; i++) {
     stroke(random(255), random(255), random(255));
       point( stars[i].x, stars[i].y);
     stars[i].x -= 6;
        if (stars[i].x < 0) { 
      stars[i] = new Star(width, random( height ));
    }
  }
} //end starfield

void keyPressed() {
  if ( key == 't' || key == 'T') {

    triangles.active = true;
  }
  if ( key == 'r' || key == 'R') {
  
    rectangle.active = true;
  }
  
  if ( key == 'e' || key == 'E') {
    ellipses.active = true;
  }
  
  if(key == 'c'){
     triangles.active = false;
     rectangle.active = false;
     ellipses.active = false;
  }
}


/***************************  CLASSES  ************************/

 class Star {
  float x, y;
  Star( float x, float y) {
    this.x = x;
    this.y = y;

  }
} // end star

class Donkey {
  PImage img;
  int speed;
  int x;
  int y;
  
 Donkey( int x, int y,int speed, PImage img ) {
    this.speed = speed;
    this.x = x;
    this.y = y;
    this.img = img;
  }
  
  void draw() {

    pushMatrix();
            popMatrix();
    translate ( x, y );
    image( img, -img.width/2, -img.height/2);
  
  }
  
   void up() {
    y -= speed;
    if ( y < img.height/2 ) { 
        y = img.height/2;
     }
  }
  
  void down() {
    y += speed;
    if ( y > height - img.height/2 ) { 
    y = height - img.height/2; 
    }
  }
  
  void left() {
    x -= speed;
    if ( x < width - img.width/2 ) { 
    x = img.width/2; 
    }
  }
  
  void right() {
    x += speed;
    if ( x > width - img.height/2 ) { 
    y = height - img.height/2; 
    }
  }
} //end donkey

class Triangles {
  int x;
  int y;
  boolean active;
  
  Triangles(int x, int y) {
    this.x = x;
    this.y = y;
    this.active = true;
  }
  
  void draw() {
    if (active) {
          for(int i= 0; i < height; i++) {
          fill(random(255), random(255));
          float a3x, b3x, c3x, a3y, b3y, c3y;
          float center3x= (random(width));
          float center3y= (random(height/2));
          a3x= center3x;
          b3x = center3x + triangle_scale;
          c3x = center3x + triangle_scale;
          a3y = center3y/2 - triangle_scale;
          b3y = center3y/2 - triangle_scale;
          c3y = b3y/2;
          triangle(a3x,a3y,b3x,b3y,c3x,c3y);
          }
   }
  }
} //end triangles

class Rectangle {

  int x;
  int y;
  boolean active;
  
  Rectangle(int x, int y) {
    this.x = x;
    this.y = y;
    this.active = true;
  }
  
  void draw() {
    if (active) {
          for(int i= 0; i < width; i++) {
          fill(random(255), random(255));
          rect(random(width),random(height/2),50, 50);
          }
   }
  }
} //end rectangle

class Ellipses {
  int x;
  int y;
  boolean active;
  
  Ellipses(int x, int y) {
    this.x = x;
    this.y = y;
    this.active = true;
  }
  
  void draw() {
    if (active) {
          for(int i= 0; i < width; i++) {
          fill(random(255));
          ellipse(random(width),random(height),20, 20);
          }
    }
  }
} //end ellipses