Bouncing off walls question
in
Programming Questions
•
11 months ago
Greetings again;
Another question for another sketch.
This sketch is just a draft where the user can click to create a rectangle that then bounces around the screen. However, not only is my rectangle changing sizes but it is only bouncing off the top and left walls. Any advice?
Another question for another sketch.
This sketch is just a draft where the user can click to create a rectangle that then bounces around the screen. However, not only is my rectangle changing sizes but it is only bouncing off the top and left walls. Any advice?
- Star[] stars = {
};
void setup() {
size(600, 600);
background(255);
}
void draw() {
background(255);
for (int i = 0; i < stars.length; i++) {
stars[i].move();
stars[i].display();
}
}
void mouseClicked() {
Star newStar = new Star(mouseX, mouseY);
stars = (Star[]) append (stars, newStar);
} - class Star {
float x = 0;
float y = 0;
float speed = 1;
int xdir = -1;
int ydir = 1;
int w = 10;
int h = 10;
Star(int x, int y) {
this.x = x;
this.y = y;
this.speed = int(random(2, 10));
}
void move() {
x += (xdir * speed);
if (x < 0 || x > width ) {
xdir = xdir * -1;
x =+ (xdir * speed);
}
w=50*xdir;
y+= (ydir * speed);
if (y < 0|| y > height){
ydir = ydir * -1;
y =+ (ydir * speed);
}
h=50*ydir;
}
void display() {
rect(x, y, x+50, y+50);
}
}
Thanks
1