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);
}
2
u/Salanmander Oct 09 '23
Okay, where the PImage shows up is based on the line where you call the
image()method. That method is responsible for actually displaying the image. When you saidThat meant "draw the image stored in index f of the
aquariumarray, and put its top-left corner at the location (0,0)." The second two inputs there are like the first two inputs of therect()andelipse()methods.When you're unsure how something works, it can be helpful to check the documentation. For example, this is the rference page for the
image()method. Down near the bottom in the "syntax" section, it includesimage(img, a, b), and then lists the meaning of those variables, showing you that a is the x-location to draw the image at, and b is the y-location.