Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ball ball;
- ArrayList <wall> walls = new ArrayList <wall>();
- void setup() {
- size(500, 700);
- ball = new ball();
- walls.add(new wall(100, 500, 200, 30));
- walls.add(new wall(200, 300, 200, 30));
- }
- void draw() {
- background(255);
- for (int i = 0; i < walls.size(); i++) {
- wall dwall = walls.get(i);
- dwall.drawWall();
- dwall.ballCollision();
- }
- ball.drawBall();
- }
- class ball {
- float x, y, Dx, Dy;
- int r;
- float alt;
- float maxAlt;
- float speed;
- float maxSpeed;
- boolean goingUp;
- ball() {
- speed = 1;
- maxSpeed = 30;
- maxAlt = 500;
- goingUp = false;
- alt = 0;
- x = 300;
- y = 50;
- r = 50;
- Dx = 0;
- Dy = maxSpeed;
- }
- void drawBall() {
- noStroke();
- fill(100, 50, 255);
- ellipse(x, y, r, r);
- moveBall();
- }
- void moveBall() {
- if (mouseX < width / 2 && mousePressed) {
- Dx = -3;
- } else if (mouseX > width / 2 && mousePressed) {
- Dx = 3;
- } else {
- Dx = 0;
- }
- x = x + Dx;
- if (speed < maxSpeed) {
- speed += .1;
- }
- y += speed; // this is the same as y = y + speed
- }
- }
- class wall {
- float x, y, w, h;
- color wallColor;
- wall(int _x, int _y, int _w, int _h) {
- wallColor = color(0, 0, 0);
- x = _x;
- y = _y;
- w = _w;
- h = _h;
- }
- void drawWall() {
- fill(wallColor);
- rect(x, y, w, h);
- }
- void ballCollision() {
- if ( ball.x + ball.r / 2 > x &&
- ball.x - ball.r / 2 < x + w &&
- ball.y + ball.r / 2 > y &&
- ball.y - ball.r / 2 < y + h) {
- ball.speed *= -1; // when it hits the wall, make speed reverse
- ball.y = y - ball.r / 2; // keeps it from getting "stuck" in the paddle
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment