I want to make a sketch of a ball that moves throughout the screen. I will start by making a ball that moves once through the screen with the below code:
//**********************
// Below two values are variables that I will use later
float xPosition = 0;
float yPosition ;
void setup(){
size(450,350);
smooth();
noStroke();
yPosition = height/2;
}
void draw(){
background(0);
fill(0,20,220);
ellipse(xPosition,yPosition,50,50);
// The below line says that the value of xPosition will grow by 2 every frame
xPosition += 2;
}
//**********************
Copy and paste the above code in the Processing program and make sure you understand what's happening.
Float is one kind of data type. A float value can hold REAL numbers, meaning positive or negative numbers with decimal point values. Examples of float values are 1.4, -4255.22, 19.14125462, 0.
Other data types are INTs (whole number integers), Strings (text), etc.
xPosition += 2;
If you want to increment a value that a variable is holding, there are some shorthand ways to do it.
For example, if you want a number to get larger by one you could write the longhand or the shorthand ways:
i = i + 1;
is the same as
i++;
The above two lines are synonymous.
i = i + 3;
is the same as
i += 3;
i = i * 1.5;
is the same as
i *= 1.5;
i = i / 1.1;
is the same as
i /= 1.1;
This means that in our program, if you want the ball to move across the screen SLOWER, then lower the value 2 to a smaller number such as .75
xPosition += .75;
This will make the ball move slower. |