may sound basic to you---but im a beginner, any help would be appreciated
in
Programming Questions
•
2 years ago
Write a dynamic processing application for a screen size of 400 X 400 that moves a square at a 45degree angle continually from (200,0) to (0,200) to (200,400) to (400,200) and then backto (200,0). The square should change to a random color every time it hits an edge.
----
add-on : Any key press should change the direction of rotation of the square, anti clockwise to clockwise and vice-versa .
----
add-on : Any key press should change the direction of rotation of the square, anti clockwise to clockwise and vice-versa .
^^^^^question
//my code so
int x = 200;
int y = 0;
int speed = 5;
int state = 0;
float r = random (10);
float g = random (10);
float b = random (70);
void setup() {
size(400,400);
}
void draw() {
frameRate (30);
background(255);
stroke(0);
rect (x,y,30,30);
//Move to left center
if(state == 0) {
x -= speed ;
y += speed;
if(x <= 0 || y >= 200) {
x = 0;
y = 200;
fill(r,g,b);
state ++;
}
}
//Move to bottom center
else if(state == 1) {
x += speed ;
y += speed;
if(x >= 200 || y >= 370) {
x = 200;
y = 370;
fill(r,g,b);
state ++;
}
}
//Move to right side center
else if(state == 2) {
x += speed ;
y -= speed;
if(x >=380 || y <= 200) {
x = 380;
y = 200;
fill(r,g,b);
state ++;
}
}
//move back to top center
else if(state == 3) {
x -= speed ;
y -= speed;
if(x <= 200 || y <= 0) {
x = 200;
y = 0;
fill(r,g,b);
state = 0;
}
}
}
//i do not know how to make it change direction if a key is pressed , any help?
//thanks in advance
2