like a pool game

edited October 2015 in How To...

anyone knows how to make a simple program for a pool game . ( i only need 1 ball and 1 hole here).
The ball will connect with a line and a mouse. the mouse will decide the direction for the ball to move to the hole.

Tagged:

Answers

  • Answer ✓
    PVector ball;
    PVector hole;
    PVector vell;
    boolean shot_line;
    
    void setup() {
      size(600, 400);
      reset();
      smooth();
    }
    
    void reset() {
      ball = new PVector( random(width), random(height), 0);
      hole = new PVector( random(width), random(height), 0);
      vell = new PVector( 0, 0, 0);
      shot_line = true;
    }
    
    void draw() {
      background(0, 128, 0);
      ball.x+=vell.x;
      ball.y+=vell.y;
      if(shot_line){
        stroke(255, 255, 0);
        line(mouseX, mouseY, ball.x, ball.y);
      }
      stroke(0, 64, 0);
      fill(0);
      ellipse(hole.x, hole.y, 30, 30);
      stroke(0);
      fill(255);
      ellipse(ball.x, ball.y, 20, 20);
      if (dist(ball.x, ball.y, hole.x, hole.y)<15) {
        println("POINT!");
        reset();
      } else if (ball.x<0||ball.x>width||ball.y<0||ball.y>height) {
        reset();
      }
    }
    
    void mousePressed() {
      shot_line = false;
      vell.x = 0.4 * (ball.x-mouseX);
      vell.y = 0.4 * (ball.y-mouseY);
    }
    

    That should be more than enough to get you started.

  • what about hit the ball and make it move to the hole. it will bound if it touches the edge

  • Answer ✓

    TfGuy44 gave you a great starting point. Try looking thru the examples and write some code. Look here: https://processing.org/examples/bounce.html

  • Maybe I wasn't clear... We've helped you enough. You're going to have to at least try to add the other features you want yourself. Post the code of your attempt for more help.

  • actually, the code you wrote is quite advance, i just take a look at it and have some sort of idea to make my game. thanks for sharing tho.

  • edited October 2015
     /* i want to make the ball toward the direction of the mouse. if the mouse 
         is  pressed then shot the ball to the hole. can you guys see this program 
         and fixed. 
         note: there will be no vector or new command and there can be only 
         void set up and draw . 
    */  
    
        float ball_locationX = 250;
        float ball_locationY = 250;
        float deltaX;
        float deltaY;
        float ballradius = 25;
        float t = 20;
        float targetX = random(100+ballradius/2,500-ballradius/2);
        float targetY = random(100+ballradius/2,500-ballradius/2);
    
        void setup()
        {
        size (500, 500);
    
        }
    
        void draw()
        {
          background (245767);
          fill (120);
        rect (100,100,300,300);
    
          if(mousePressed == true)
          {
          stroke(255);
          line (ball_locationX, ball_locationY, mouseX, mouseY);
          }
    
          deltaX = (ball_locationX - mouseX)/66;
          deltaY = (ball_locationY - mouseY)/66;
    
          noStroke();
    
          fill (0);
          ellipse (targetX, targetY, ballradius,ballradius);
          fill (255);
          stroke (#A6A7A6);
          ellipse (ball_locationX, ball_locationY, ballradius, ballradius);
          stroke (#A6A7A6);
          noFill();
          translate(ball_locationX,ball_locationY);
    
          ball_locationX = ball_locationX + deltaX;
          ball_locationY = ball_locationY + deltaY;
    
          if (ball_locationX > width-100-ballradius/2 ||ball_locationX< 100+ballradius/2)
    
            deltaX = -deltaX;
            deltaX = deltaX * 0.6;
            if (ball_locationX < 100+ballradius/2)
            {
            ball_locationX = 101+ballradius/2;
          }
            else
            {ball_locationX = width-101-ballradius/2;
          }
    
          if (ball_locationY > width-100-ballradius/2 || ball_locationY < 100+ballradius/2)
          {
            deltaY = -deltaY;
            deltaY = deltaY * 0.6;
            if (ball_locationY < 100+ballradius/2){
              ball_locationY = 101+ballradius/2;
          }
            else{
           ball_locationY = width-101-ballradius/2;
            }
    
        deltaX = deltaX * 0.9999;
        deltaY = deltaY * 0.9999;
    
          if (sqrt(sq(ball_locationX-targetX)+sq(ball_locationY-targetY))-5 < ballradius/2)
          {ball_locationX= targetX;
          ball_locationY = targetY;
          //deltaX = 0;
          //deltaY = 0;
          targetX = random(100+ballradius/2,500-ballradius/2);
          targetY = random(100+ballradius/2,500-ballradius/2);
          }
          }
        }
    
Sign In or Register to comment.