// 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;

// What do the variables mean? – There are more reminders next to
// the code in the next section named: THIS SETS UP THE GAME
// bDia is the button diameter
// cDia is the action circle diameter
// mvX is the direction on the x axis Horizontal
// mvY is the direction on the Y axis Vertical

//THIS SETS UP THE GAME
void setup() {
size(200, 400); // Create a program window 200 pixels wide and 400 pixels high.
stroke(0); // Set the color for the outlines of our shapes.
bDia = 30; // Set the diameter of our circular control buttons.
cDia = 30; // 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.
}

// Loop for the screen
void draw() {
// First 4 lines let you use math coordinates instead of computer coordinates.
// (0,0) is at the top left corner of the screen in computers.
scale(1, -1); // What does this mean?
translate(0, -height); // What does this mean?
clickX = mouseX; // What does this mean?
clickY = height – mouseY; // What does this mean?

fill(143, 141, 141); // This colors the action screen grey (RGB to change color)
rect(0, 200, 200, 200); // This draws the action screen

// FYI ellipse means circle
ellipse(100, 60, bDia, bDia); // Draw the top button.
ellipse(100, 100, bDia, bDia); // Draw the middle button.
ellipse(100, 140, bDia, bDia); // Draw the bottom 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 (cX + cDia/2 > 399){
cX = 199 – cDia/2;
mvX= -(mvX); //Makes the a.c. “bounce”
}

//If the action circle hits the bottom of the screen, it should bounce back down
else if (cX – cDia/2 < 201){
cX = 0 + cDia/2;
mvX= -(mvX); //Makes the a.c. “bounce”
}
cX = cX + mvX; // Set the horizontal position (x-axis) of the action circle.
// (cX) whatever it was last time + (mvX) movement vari

// THIS IS SO IT DOES NOT GET STUCK AT THE TOP OF THE SCREEN
// If the action circle hits the top of the screen, it should bounce back down.
if (cY + cDia/2 > 399){
cY = 399 – cDia/2;
mvY= -(mvY); //Makes the a.c. “bounce”
}

//If the action circle hits the bottom of the screen, it should bounce back down
else if (cY – cDia/2 < 201){
cY = 201 + cDia/2;
mvY= -(mvY); //Makes the a.c. “bounce”
}

cY = cY + mvY; // Set the vertical position of the action circle.
// 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.
}

// IF THE MOUSE PRESSES ONE OF THE BUTTONS
void mousePressed() {
//Check if the user clicked the middle button.
if (dist(clickX, clickY, 100, 100) < bDia/2) {
cCol = color(123, 60, 46);
// If so, the a.c. should turn a color.
}

// When playing the game, you need to press the different buttons
// before it hits the edge of the screen

//Check if the user clicked the top button.
else if (dist(clickX, clickY, 100, 140) < bDia/2) {
mvY = 1;
mvX = 0;
//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 = -1;
mvX = 0;
//If so, make the action circle move downwards.
}

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

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

}

Advertisements