Hello, I’m really new to Processing and programming in general so I’m having some hard time to realize my project. I’m kinda lost right now and I’d really appreciate some help.
So, I’m having this code, in which I want to achieve two goals:
a) Make the ball move on the grid at random directions, kinda like following directions from a GPS like:
right, right, up, right, down, left, left, but for each direction I have set some rules:
If right is chosen, then move from current X to new X by moving 1 by 1 pixels. When the ball reaches the new X position then it should follow a new direction order.
To sum up the problem, the ball is disappearing off the screen and I can’t find the error in the code.
b) I want the speed of Movement Class to be different than the rest of the sketch. In simple words I want the ball to move 1 by 1 pixel but do this movement fast. However I want the random parts of the code like random color of the grid, random space between grains and random stroke weight of the grains to be slower.
Sorry for the lame explanation… Any help would be really appreciated. Thanks in advance and have a nice day!
int space = 50; //space for grid,
int radius = 20; //radius of ball
Grid grid1;
Movement movement1;
void setup() {
size(601, 601, P2D);
frameRate(30);
grid1 = new Grid();
movement1 = new Movement(space, 1); //speed/amount of step, delay time
}
void draw() {
background(0);
grid1.display();
movement1.display();
movement1.move();
//grains
for (int i = 0; i < width; i = i+int(random(2, 100))) {
for (int j = 0; j < height; j = j+int(random(2, 100))) {
strokeWeight(random(1, 2.5));
stroke(255);
point(i, j);
}
}
}
class Movement {
int stepsize; //stepsize
int s; // value for switching cases
int d; // delay time
int x, y; // position
//constractor
Movement(int tempStSz, int tempD) {
stepsize = tempStSz;
d = tempD;
x = width / 2;
y = height / 2;
}
void move() {
boolean b = true; // a way for infinite loop
while (b == true) {
s = int(random(4))+1; // 1,2,3,4 translates to up, right, down, left
delay(d); // delays/stops the loop for t amount of time
switch(s) {
case 1:
println("up");
for (int i = 0; i < stepsize; i++) {
y += i;
}
if (y > height) {
y *= -1;
}
break;
case 2:
println("right");
for (int i = 0; i < stepsize; i++) {
x += i;
}
if (x > width) {
x *= -1;
}
break;
case 3:
println("down");
for (int i = 0; i < stepsize; i++) {
y -= i;
}
if (y < 0) {
y *= -1;
}
break;
case 4:
println("left");
for (int i = 0; i < stepsize; i++) {
x -= i;
}
if (x < 0) {
x *= -1;
}
break;
}
return; // return values of x and y to the ellipse
}
}
void display() {
noStroke();
ellipseMode(CENTER);
fill(0, int(random(190)), 255);
ellipse(x, y, radius*2, radius*2);
}
}
class Grid {
int x, y; // location
//constractor
Grid() {
x = 0;
y = 0;
}
void display() {
for (int x = 0; x < width; x += space) {
for (int y = 0; y < height; y += space) {
strokeWeight(1);
stroke(0, int(random(255)), 0);
line(0, y, width, y);
line(x, 0, x, height);
}
}
}
}