Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Stack 'Em: Building Blocks! 
//By Rachel Keilhofer
//Click the mouse to draw a block
//Press a key to draw a circle
//Create whatever you like!

Block newBlock;
Circle newCircle;

color bordercolor = color(100,0,255);
color purple = color(200,0,255);
color yellow = color(255, 255, 0);
color blue = color(0,0,255);
color red = color(255,0,0);
color green = color(0,255,0);
color fillColor;
color[] colorArray;
int gravity = 2;
int randomArray;

void setup(){
  //set display size to 800x600
  size(800,600);
  //set background to sky blue
  background(200,255,255);
  
  //Establish the values in the color array
  colorArray = new color[5];
  colorArray[0] = purple;
  colorArray[1] = yellow;
  colorArray[2] = blue;
  colorArray[3] = red;
  colorArray[4] = green;
  
  //Draw clouds in background
  noStroke();
 fill(255);
 ellipse(50,150,100,25);
 ellipse(100,300,150,50);
 ellipse(400,200,150,40);
 ellipse(700,300,125,50);
 ellipse(600,100,150,25);
  
}


void draw(){
 
}

//Draw the block object when the mouse is pressed
void mousePressed(){
  
  randomArray = (int)random(colorArray.length);
  fillColor = colorArray[randomArray];
  newBlock = new Block(fillColor, bordercolor, gravity, 50, 50);
  newBlock.drop();
  newBlock.display();
  
  
}

//Draw the circle object when the mouse is pressed
void keyPressed(){
 
  randomArray = (int)random(colorArray.length);
  fillColor = colorArray[randomArray];
  newCircle = new Circle(fillColor, bordercolor, gravity, 50, 50);
  newCircle.drop();
  newCircle.display();
  
}
//class
class Block{
  //variables
  color c0;
  color c1;
  int gravity;
  int w;
  int h;
  float xpos;
  float ypos;
  
  //Constructor, establish 'temp' variables
  Block(color culor1, color culor2, int grav, int blockWidth, int blockHeight){
    
   //Tie 'temp' variables to original variables with values
   c0=culor1; //color(200,0,255);
   c1=culor2; //color(100,0,255);
   gravity= grav;//=2;
   w = blockWidth; //50;
   h = blockHeight;
   xpos= mouseX;
   ypos= mouseY;
  }
  
  //Display the block at mouse location
  void display(){
   rectMode(CENTER);
   fill(c0);
   stroke(c1);
   strokeWeight(4);
   rect(xpos,ypos, w, h);
   
  }
  
  //Make blocks move - unsuccessful
  void drop(){
    PVector location = new PVector(xpos,ypos);
    PVector velocity = new PVector(0,gravity);
    location.add(velocity);
  }
}
//class
class Circle{
  //establish variables
  color c0;
  color c1;
  int gravity;
  int w;
  int h;
  float xpos;
  float ypos;
  
  //Constructor, establish 'temp' variables
  Circle(color culor1, color culor2, int grav, int blockWidth, int blockHeight){
    
   //Tie 'temp' variables to original variables with values
   c0=culor1; //color(200,0,255);
   c1=culor2; //color(100,0,255);
   gravity= grav;//=2;
   w = blockWidth; //50;
   h = blockHeight;
   xpos=mouseX;
   ypos=mouseY;
  }
  
  //Draw the circle at mouse location
  void display(){
   ellipseMode(CENTER);
   fill(c0);
   stroke(c1);
   strokeWeight(4);
   ellipse(xpos,ypos, w, h);
  }
  
  //Make circle move - unsuccessful
  void drop(){
   PVector location = new PVector(xpos,ypos);
   PVector velocity = new PVector(0,gravity);
   location.add(velocity);
  }
}