My most recent assignment was one where I had to make a square and have it so that when the user (square) reaches the edge of the window, the user (square) will appear on the other side (left <-> right, top <-> bottom). I created a very interesting square that is constantly changing colors in the middle, but has a very thick yellow stroke weight. I did this just for fun not because it was part of the assignment. In order to do this, I first created two int's. This two int's (squarex & squarey) declare the size of the square which is 400x400. In order to get the square to move, I had to use keyPressed. I made it so that when keyCode UP was pressed the square increased by 60, when keyCode DOWN was pressed the square decreased by 60 and I continued this for the LEFT and RIGHT keys. This allowed my square to move left, right, up and down by 60 when certain keys were pressed. This is what those codes looked like:
void keyPressed(){
if(keyCode == UP){
squarey -= 60;
}
else if(keyCode == DOWN){
squarey += 60;
}
else if(keyCode == LEFT){
squarex -= 60;
}
else if(keyCode == RIGHT){
squarex += 60;
}
Then I had to make it so that when the square reached the far left of the screen it appeared on the right, when it reached the far right on the screen it appeared on the left and so on. In order to do this, I used if statements. My if statements basically said if squarex goes beyond the width make squarex equal 0. Meaning make squarex reappear at coordinate (0,?). Also, if squarey goes beyond the height make squarey equal 0. Meaning make squarey reappear at coordinate (?,0). I basically repeated this four times simply changing where the square. So if squarex is less than 0 make squarex=width. This is what those codes look like:
// (Too far right)
if(squarex > width){
squarex=0;
}
// (Too far left)
if(squarex < 0){
squarex=width ;
}
//(Too far down)
if( squarey > height){
squarey=0;
}
//(Too far up)
if( squarey < 0){
squarey=height;
}
}
Altogether these codes made it so that when the user reached the edge of the window, the square will appear on the other side (left <-> right, top <-> bottom).
“Yes, we have a dress code. You have to dress.”
– Scott McNealy, co-founder of Sun Microsystems