I have to have 2 animations, with a background and I can't seem to get it to work.
in
Programming Questions
•
2 years ago
I was trying to get the background to form while the ball is bouncing and I just cant seem to get it right. Here my code so far. Please help if you can. I am an extreme beginner at processing.
import processing.opengl.*;
float xpos;
float ypos;
int ballWth ;
int ballHgt ;
float xspeed ;
float yspeed ;
PImage bg;
void setup(){
size(800, 600, OPENGL);
smooth();
background(0);
strokeWeight(10);
for(int i = 0; i < width; i++) {
float r = random(255);
float x = random(0, width);
stroke(r, 100);
line(i, 0, x, height);
}
save("background.png");
bg = loadImage("background.png");
noStroke();
frameRate(200);
smooth();
xpos = width/2.0;
ypos = height/2.0;
xspeed = .3+random(2.5);
yspeed = .3+random(2.5);
ballWth = 80;
ballHgt = 80;
}
void draw(){
smooth();
background(0);
strokeWeight(10);
for(int i = 0; i < width; i++) {
float r = random(255);
float x = random(0, width);
stroke(r, 100);
line(i, 0, x, height);
}
// Animation
background(123);
fill(0, 162, 142);
ellipse(xpos, ypos, ballWth, ballHgt);
xpos += xspeed;
ypos += yspeed;
if (xpos >= width || xpos <=0){
xspeed *= -1;
}
if (ypos >= height || ypos <= 0){
yspeed *=-1;
}
}
[Moderator's note: removed useless information from the title. Also removed duplicate thread.]
[Moderator's note: removed useless information from the title. Also removed duplicate thread.]
1