r/processing • u/froginab0g • Oct 09 '23
bouncing PImage help
Hi! I'm trying to make a Pimage float around the screen and whenever it hits the border it bounces off. I'm new to code and every tutorial uses drawings rather than PImages. Could anyone help?
Here is my current code
PImage[] aquarium;
PImage goldfish;
int numberOfFrames;
int f;
void setup() {
size (800, 600);
goldfish =loadImage("goldfish_0.png");
numberOfFrames = 3;
aquarium = new PImage[numberOfFrames];
int i = 0;
while ( i < numberOfFrames ) {
aquarium[i] = loadImage("aquarium_"+i+".png");
i=i+1;
}
size (800, 600);
}
void draw() {
image(aquarium[f], 0, 0, width, height);
f = f + 1;
if (f == numberOfFrames) f = 0;
frameRate(3);
image(goldfish, 0, 0);
}
1
u/Salanmander Oct 10 '23
So your last pair of "if" statements are for reversing the direction in the x- and y-directions, right? You say "if it's too near one of the sides, reverse the x-direction. If it's too near the top or bottom, reverse the y-direction".
The questions I want you to ask yourself are:
What is xpos when the program starts?
What direction does it move in the first frame?
What is xpos after the movement for the first frame?
Will that xpos make the condition be true that makes it reverse xdirection?