• fullscreen
  • Ball.pde
  • Pong.pde
  • class Ball{
      
      Rectangle r;
      Point v;//Bewegungsvector
      
      int RADIUS=10;
      
      public Ball(int x, int y, int vx, int vy){
        r=new Rectangle(x,y,RADIUS,RADIUS);
        v=new Point(vx,vy);
      }
      public void draw(){
        r.x+=v.x;
        r.y+=v.y;
        rect(r.x,r.y,r.width,r.height);
        if (r.x < 0 || r.x > width-RADIUS) v.x=-v.x;
        if (r.y < 0-RADIUS || r.y > height) balls.remove(this);
      }
      public void test(Rectangle brett){
        if (r.intersects(brett) && (
              (v.y<0 && r.y-brett.y>0) || 
              (v.y>0 &&r.y-brett.y<0)) 
            ) v.y=-v.y;
      }
    }
    
    import java.awt.*;
    
    Rectangle brett1;
    Rectangle brett2;
    ArrayList balls;
    
    int breite=100;
    int hoehe=10;
    
    void setup(){
      size(500,500);
      balls=new ArrayList();
      createBall();
      noStroke();
    }
    void draw(){
      background(0);
      brett1=new Rectangle(mouseX-breite/2,height-hoehe,breite,hoehe);
      brett2=new Rectangle(mouseX-breite/2,hoehe,breite,hoehe);
      brett(brett1);
      brett(brett2);
    
      for (int x=0;x<balls.size();x++){
        Ball ball=(Ball) balls.get(x);
        ball.draw();
        ball.test(brett1);
        ball.test(brett2);
      }
    }
    
    void brett(Rectangle r){
      rect(r.x,r.y,r.width,r.height);
    }
    void mouseClicked() {
      createBall();
    }
    void createBall(){
       balls.add( new Ball(width/2,height/2,1,2) );
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Dieter Meiller

    Pong

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    Educational Collision Detection, OOP

    You need to login/register to comment.