New Hope Academy Coding

This blog will be a way to continue our class discussion about coding challenges in Processing.
As a reminder: Dr. Daniel Shiffman is the guru for Processing programming. He has several books, the two most important ones are: “Learning Processing” and “The Nature of Code.” “Learning Processing” is a very good introductory text while “The Nature of Code” is more advanced.

Dr. Shiffman also has a large collection of tutorial videos on processing that cover all the introductory and advanced topics. He is very good and easy to listen to. I recommend you consult them frequently.

A word about Processing. It is a Java based program that is primarily aimed at producing graphics and even art. The principles needed to use Processing are the same as the principles used in other programming languages. So learning Processing will be an excellent start to learning other languages. Processing is free and may be used on different platforms.

On our last meeting we discussed the following code:

//A program to have a button to stop the bouncing ball when the mouse is over the button
// Jim Lauridson, 2017

float xTar=50; // x for the target ball
float yTar=100; // y for the target ball
float xVel= random(4,6); // the x velocity, set as a random number at the beginning 
float xTarVel=xVel; // establishes two variables for the x velocity
float yVel= random(7,9); // the y velocity, set as a random number at the beginning 
float yTarVel=yVel; // establishes two variables for the y velocity
// color variables for the target ball
int rTar=255;
int gTar=255;
int bTar=255;

// to indices that are needed to allow smooth stopping of the ball
int cx = 1;
int cy = 1 ;

// color variables for the button
int rButton= 0;
int gButton = 0;
int bButton=0;

void setup()
{
 size(800,800); 
}

//The draw loop which repeats continuously
void draw()
{
 background(200,200,200);
 
 // the Stop Action button
 textSize(20);
 fill(rButton,gButton, bButton);
 rect(95,80, 120,25); // button
 fill(255, 255,0);
 text("Stop Action", 100,100);
 fill(rTar, gTar, bTar,100);
 
 // the ball target
 ellipse(xTar,yTar, 25,25);
 if (mouseX>95 && mouseX < 215 && mouseY> 80 && mouseY < 105) // checks for mouse position
 {
 // if above is TRUE then velocity becomes zero.
 xTarVel = 0;
 yTarVel = 0;
 rButton = 180;
 }
 else
 {
 xTarVel= xVel * cx;
 yTarVel = yVel * cy;
 rButton = 0;
 }
 xTar +=xTarVel;
 yTar += yTarVel;
 
 if (xTar > width || xTar < 0) // bounce conditional for x
 { 
 cx=cx* -1;
 gTar= int(random(150,200));
 bTar=int (random(120,255));
 rTar=int(random(180,255));
 }
 if (yTar > height || yTar < 0) // bounce conditional for y
 {
 cy=cy * -1;
 //yTarVel= yTarVel * -1;
 rTar=0;
 gTar=int(random(0,255));
 bTar=int(random(0,255));
 }
 
 
}

The next step is to change the “Stop Action” button to a circle and then stop action any time the mouse is over the circle. The key command to make this easy is dist.  See what you can do.  If you want leave the code in a comment.

We will discuss this next class.

March 16, 2017