I recently started learning processing and I have been trying to write a code in which a ball is bouncing off the four corners of a grid.
Can someone plz help me with this?
float x = 150;
float y = 150;
void setup() {
size(800, 800);
}
void draw() {
background(0, 0, 255);
stroke(255);
//VERTICAL LINES
for (float x = 150; x < 700; x = x + 50) {
line(x, 150, x, 650);
}
//HORIZONTAL LINES
for (float y = 150; y < 700; y = y + 50) {
line(150, y, 650, y);
}
//Ball
noStroke();
ellipseMode(CENTER);
ellipse(x, y, 20, 20);
x = x + 5;
if (x > 650) {
x = 650;
y = y + 5;
}
if (y > 650) {
x = x - 10;
}
Yes you are correct. I made a grid and then on that grid right now the ball starts moving from x and y axis 150 towards x axis 650 and then from 650 it bounces and starts moving down and then once it reaches 650 on y axis, here the ball slightly goes off the grid and starts moving towards the left, after that the ball stops.
I have looked at all the tutorials on youtube but I am not being able to figure the movement. The movement which I am trying to achieve is right, down, left and up and this keeps going on in a loop.
Thank you so much for the link, this is the movement which I am trying to achieve but the code looks a bit complicated. I am still at the beginners level. Would you know if there is a basic code to achieve this movement?
Okay, let me go through the link which you sent. I will try to fix this by myself first and still if I don’t get the movement right, I will get back to you.
Hi, thank you for your reply. Its been few weeks that I started learning Processing, I am gonna go through the code which you sent and try to work on it again. I have been looking at tutorials on youtube and still trying to figure a better platform to learn.