**DontTouchTheBottomG1EI.pde

// This Processing program creates a little animation machine
// with controls that you can customize.
// First, we need to “declare” a few variables of 3 different types.
int bDia, cDia, mvX, mvY;
float cX, cY, clickX, clickY;
color cCol;

void setup() {
size(201, 400); // Create a program window 201 pixels wide and 400 pixels high.
stroke(127); // Set the color for the outlines of our shapes.
bDia = 20; // Set the diameter of our circular control buttons.
cDia = 20; // Set the diameter of the “action circle” we will control.
cX = 100; // Set the starting horizontal position of the action circle.
cY = 300; // Set the starting vertical position of the action circle.
cCol = color(255, 255, 255); // Set the starting color of the action circle.
mvX = 0; // Set the horizontal movement of the action circle.
mvY = 0; // Set the vertical movement of the action circle.
}

void draw() {
// These first 4 lines let you use math coordinates instead of computer coordinates.
scale(1, -1);
translate(0, -height);
clickX = mouseX;
clickY = height – mouseY;

fill(0); // Color the screen white.
rect(0, 200, 200, 200); // Draw the square “action screen”.

ellipse(100, 60, bDia, bDia); // Draw the bottom button.
ellipse(100, 100, bDia, bDia); // Draw the middle button.
ellipse(100, 140, bDia, bDia); // Draw the top button.
ellipse(60, 100, bDia, bDia); // Draw the left button.
ellipse(140, 100, bDia, bDia); // Draw the right button.

fill(cCol); // Set the color for the action circle.
if(cY + cDia/2 > 399) {
cY = 399 – cDia/2;
mvY = -(mvY);
}

if(cX + cDia/2 > 199) {
cX = 199 – cDia/2;
mvX = -(mvX);
}
else if(cX – cDia/2 <1) {
cX = 1 + cDia/2;
mvX = -(mvX);
}
if (cX + cDia/2 > 199){
cX = 199 – cDia/2;
}
else if (cX – cDia/2 < 1) {
cX = 1 + cDia/2;
}

cX = cX + mvX;

if (cY + cDia/2 > 399){
cY = 399 – cDia/2;
}
else if (cY – cDia/2 < 201) {
cY = 201 + cDia/2;
}
cY = cY + mvY;

// The 2 “print lines” below will display at the bottom of this window.
// They will let us check the coordinates of the program window.
println(“X =”, clickX);
println(“Y =”, clickY);

ellipse(cX, cY, cDia, cDia); // Draw the action circle.
}

void mousePressed() {
//Check if the user clicked the middle button.
if (dist(clickX, clickY, 100, 100) < bDia/2) {
cCol = color(123, 170, 69);
// If so, turn the action circle red.
}

//Check if the user clicked the top button.
else if (dist(clickX, clickY, 100, 140) < bDia/2) {
mvY = 10;
//If so, make the action circle move upwards.
}

//Check if the user clicked the bottom button.
else if (dist(clickX, clickY, 100, 60) < bDia/2) {
mvY = -20;
//If so, make the action circle move downwards.
}

//Check if the user clicked the right button.
else if (dist(clickX, clickY, 140, 100) < bDia/2) {
mvX = 10;
//If so, make the action circle move to the right.
}

//Check if the user clicked the bottom button.
else if (dist(clickX, clickY, 60, 100) < bDia/2) {
mvX = -10;
//If so, make the action circle move downwards.
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s