Different Hole Score Question

Due to other projects and not a lot of time left, my golf game for processing has been adjusting to fit time loss. My new idea for now is a golf skee ball. I have the code to throw the golf ball, but I’m trying to figure out the code for the holes, each hole on the screen represents Par,Bogey,Birdie etc. Which will be tracked by adding a score.So when the ball comes in to contact or radius of certain hole it will be counted towards your score.
I know classes will be best for this but they are not my friend and they don’t work when I use them. ( I know it’s something I’m doing wrong that doesn’t make it work).

///MAIN FILE

// Declare global variables here.
PImage golf;
Menu clubTypeMenu;
Menu ironsMenu;
Menu currentMenu;
Menu woodsMenu;
Menu wedgesMenu;
///BALL
int numstroke = 0;
PVector ball;
PVector velocity;
float force;
float loft = radians ( 80 );
boolean landed = true;
int size = 10;
int xspeed = 0;
int yspeed = 0;
int wherex = 20;
int wherey = 10;
int buttonMinX = 150;
int buttonMinY = 400;
int buttonMaxX = 300;
int buttonMaxY = 475;

// Function setup() will be called exactly once,
// when the program starts up.
void setup() {
  size ( 700, 700 );
  golf=loadImage("golf1.png");
  ball = new PVector();
  velocity = new PVector();
  //Hole
  clubTypeMenu = new Menu ( width-100, 0, width, height );
  clubTypeMenu.addItem ( "Ready Shot" );
  clubTypeMenu.addItem ( "Driver" );
  clubTypeMenu.addItem ( "Putter" );
  clubTypeMenu.addItem ( "Irons" );
  clubTypeMenu.addItem ( "Woods" );
  clubTypeMenu.addItem ( "Wedges" );
  ironsMenu = new Menu ( width-100, 0, width, height );
  ironsMenu.addItem ( "Bag" );
  for ( int i=1; i<10; i++ ) {
    ironsMenu.addItem ( str ( i ) + " Iron" );
  }
  currentMenu = clubTypeMenu;

  woodsMenu = new Menu ( width-100, 0, width, height );
  woodsMenu.addItem ( "Bag" );
  for ( int i=1; i<6; i++ ) {
    woodsMenu.addItem ( str ( i ) + "Wood" );
  }
  woodsMenu.addItem ("Hybrid");
  currentMenu = clubTypeMenu;

  wedgesMenu = new Menu ( width-100, 0, width, height );
  wedgesMenu.addItem ( "Bag" );
  for ( int i=1; i<5; i++ ) {
    wedgesMenu.addItem ( str ( i ) + " Wedges" );
  }
  currentMenu = clubTypeMenu;
}



// Function draw() will be called repeatedly,
// once per frame, until noloop() is called
// or the program exits.
void draw() {
  pushMatrix();
  scale(.7);
  image (golf, 0, 0);
  popMatrix();
  fill(255);
  textSize(20);
  text("Stroke:"+ numstroke, 5, 20);
  textSize(15);
  fill ( #499834 );
  currentMenu.show();
  noStroke();
  //swingPower=mouseX/2;
  noStroke();
  float diam = max ( 3, 500/ball.z );
  ellipse ( ball.x, ball.y, diam, diam );
  if ( !mousePressed ) {
    if ( !landed ) {
      ball.add(velocity);
      velocity.y += 0.5;
      if ( ball.y > height-15 ) {
        float distance = ball.z;
        velocity.set ( 0, 0 );
        println ( "Ball goes " + distance );
        landed = true;
      }
    }
  }
}

void mousePressed() {
  if ( currentMenu.inside ( mouseX, mouseY ) ) {
    int n = currentMenu.whichItem( mouseX, mouseY );
    if ( n > 0 ) {
      println ( currentMenu.items.get(n) );
    }
    if ( n == 0 ) {
      currentMenu = clubTypeMenu;
    }
    if ( n == 3 ) {
      currentMenu = ironsMenu;
    }
    if ( n == 4 ) {
      currentMenu = woodsMenu;
    }
    if ( n == 5 ) {
      currentMenu = wedgesMenu;
    }
  }
  {
    ball.set ( width/2, height - 15, 10 );
    velocity.set ( 6, 4 );
  }
}

void mouseDragged() {
  //ball.set ( mouseX, mouseY );
  velocity.set ( mouseX-pmouseX, mouseY-pmouseY );
  velocity.mult ( 0.8 );
  force = velocity.mag();
  println ( force );
  if ( force > 5 ) {
    velocity.mult ( 5 / force );
  }
}

void mouseReleased() {
  PVector club = PVector.fromAngle ( -loft );
  club.mult ( force );
  velocity.x *= 0.1;
  velocity.y = club.y+2;
  velocity.z = club.x+2;
  ball.z = 0;
  landed = false;
  numstroke +=1;
}



void keyPressed() {
  numstroke = 0;
}


//MENU CODE

class Menu {
  PVector minPt;
  PVector maxPt;
  ArrayList<String> items;
  int spacing = 40;

  Menu ( int minX, int minY, int maxX, int maxY ) {
    minPt = new PVector ( minX, minY );
    maxPt = new PVector ( maxX, maxY );
    items = new ArrayList<String>();
  }

  void show() {
    int item = whichItem ( mouseX, mouseY );
    rectMode ( CORNERS );
    rect ( minPt.x, minPt.y, maxPt.x, maxPt.y );
    fill ( 255 );
    int nItems = items.size();
    textAlign ( LEFT, CENTER );
    for ( int i=0; i<nItems; i++ ) {
      if ( i == item ) {
        fill ( 255, 70 );
        rect ( minPt.x, minPt.y + spacing*i, maxPt.x, minPt.y + spacing*(i+1) );
      }
      float itemY = minPt.y + spacing * (i+0.5);
      text ( items.get(i), minPt.x+10, itemY );
    }
  }

  void addItem ( String i ) {
    items.add ( i );
  }

  boolean inside ( int mX, int mY ) {
    if ( mX > minPt.x && mX < maxPt.x
      && mY > minPt.y && mY < maxPt.y )
    {
      return true;
    } else {
      return false;
    }
  }

  int whichItem ( int mX, int mY ) {
    if ( !inside ( mX, mY ) ) {
      return -1;
    } else {
      int result = int((mY-minPt.y)/spacing);
      if ( result < items.size() ) {
        return result;
      } else {
        return -1;
      }
    }
  }
}

2 Likes

Presumably, your ball has a position. You can check that position to see if it is in one of the holes. Oh, and you should have a class that represents a hole.

class Hole{
  float x, y, points;
  Hole(float _points){
    points = _points;
  }
  boolean check(ballX, ballY){
    if( ball in hole ){
      // Move the ball elsewhere - it's gone!
      player_score += points;
    }
  }
}
1 Like