Target shooting game.

Hello, I have a project where we have to make a target shooting game. however, I can't figure out how to work the dist function to figure out if the two circles are touching.

Heres the main class where I am having issues. Thanks in advance for any help

[code below]

Tagged:

Answers

  • edited January 2018

    Sorry, it formatted weirdly.

    class Ball {
    
      PVector pos;
      PVector velocity;
      PVector acceleration;
      PImage ballImage, targets;
      Boolean alive;
      float rs = random(2,10);
      //float rx=  random(75,1068);
    //float ry= random(100,550);
    
    
      Ball(PVector v_) {
        pos = new PVector(70, 520);
        velocity = v_;
        acceleration = new PVector(0, 0.2);
        alive= true;
    
        ballImage= loadImage("Cannonball.png");
    
    
      }
    
    
       void targetCollision(){
       //noLoop();
       for (int t=0; t<21;t++){
     image(targets,rx, ry);
     if(dist(pos.x, pos.y,rx ,ry )<5){
     hitsCounter++;
       text("Hit", width/2, height/2);
     }
       }
    
     }
    
      void move() {
        pos = pos.add(velocity);
        velocity = velocity.add(acceleration);
      }
      void display() {
        pushMatrix();
        translate(pos.x, pos.y);
        image(ballImage, 0, 0);
        popMatrix();
      }
    
      void smoke(){
      ellipseMode(CENTER);
      for(int i=0; i<20; i++){
        fill(random(20,220));
    
    
    
    }
    
    }
    
      void checkGroundCollision() {
        if (pos.y>546) {
          smoke();
          alive =false;
        }
      }
      Boolean isAlive() {
        return alive;
      }
    }
    
  • Answer ✓

    Best is to hit ctrl-t in processing first before copy paste into the forum (https://forum.processing.org).

    You can edit your post (click the small gear and the 'Edit').

    How to format Code:

    empty line before and after the code section
    
    select (highlight) entire code with the mouse
    
    hit ctrl-o OR click the small C in the command bar
    
  • Thanks for that help

  • please in processing hit ctrl-t to get auto indents in processing

    can you post the entire code so that we can run it?

  • AND DO IT HERE

    don't start a new thread when this one has nothing in it.

  • 5 might be too small here

    dist(pos.x, pos.y,rx ,ry )<5){
    
  • whenever I try to paste my code using ctrl-o or the c I get

  • edited January 2018
    Game currentGame;
    
    void setup() {
      size(1068, 600);
      currentGame = new Game();
      frameRate(60);
    }
    
    void draw() {
      currentGame.play();
      quickInput();
      tops();
      currentGame.counts();
      //ball.target();
    }
    
    void keyPressed() {
      if (key ==' ') {
        currentGame.createShot();
      }
    }
    
    void quickInput() {
      if (keyPressed) {
        if (keyCode == UP) {
          if (currentGame.cannonPower >= 5) {
            currentGame.cannonPower++;
          } else if (currentGame.cannonPower <= 5) {
            currentGame.cannonPower=5;
          } else {
            currentGame.cannonPower=50;
          }
        } else if (keyCode == DOWN) {
          if (currentGame.cannonPower >= 5) {
            currentGame.cannonPower--;
          } else if (currentGame.cannonPower <= 5) {
            currentGame.cannonPower=5;
          } else {
            currentGame.cannonPower=50;
          }
        } else if (keyCode == LEFT) {
          currentGame.increseAngle(true);
        } else if (keyCode == RIGHT) {
          currentGame.increseAngle(false);
        }
      }
    }
    
    class Ball {
    
      PVector pos;
      PVector velocity;
      PVector acceleration;
      PImage ballImage, targets;
      Boolean alive;
      float rs = random(2, 10);
      //float rx=  random(75,1068);
      //float ry= random(100,550);
    
    
      Ball(PVector v_) {
        pos = new PVector(70, 520);
        velocity = v_;
        acceleration = new PVector(0, 0.2);
        alive= true;
    
        ballImage= loadImage("Cannonball.png");
      }
    
    
      void targetCollision() {
        //noLoop();
        for (int t=0; t<21; t++) {
          image(targets, rx, ry);
          if (dist(pos.x, pos.y, rx, ry )<5) {
            hitsCounter++;
            text("Hit", width/2, height/2);
          }
        }
      }
    
      void move() {
        pos = pos.add(velocity);
        velocity = velocity.add(acceleration);
      }
    
      void display() {
        pushMatrix();
        translate(pos.x, pos.y);
        image(ballImage, 0, 0);
        popMatrix();
      }
    
      void smoke() {
        ellipseMode(CENTER);
        for (int i=0; i<20; i++) {
          fill(random(20, 220));
        }
      }
    
      void checkGroundCollision() {
        if (pos.y>546) {
          smoke();
          alive =false;
        }
      }
    
      Boolean isAlive() {
        return alive;
      }
    }
    
    float rx=  random(75, 1068);
    float ry= random(110, 550);
    int hitsCounter= 1;
    
    class Game {
      ArrayList<Ball> shots = new ArrayList <Ball>();
      PImage backImage, base, barrel, backImage2, targets;
      float cannonAngle = 0;
      float cannonPower=11;
      float shoots;
      PImage ballCounter1, ballCounter2, ballCounter3, ballCounter4, ballCounter5, ballCounter6, ballCounter7, ballCounter8, ballCounter9, ballCounter10;
      PImage ballCounter11, ballCounter12, ballCounter13, ballCounter14, ballCounter15, ballCounter16, ballCounter17, ballCounter18, ballCounter19, ballCounter20;
      PImage hitCount, hitCount1, hitCount2, hitCount3, hitCount4, hitCount5, hitCount6, hitCount7, hitCount8, hitCount9, hitCount10;
      PImage hitCount11, hitCount12, hitCount13, hitCount14, hitCount15, hitCount16, hitCount17, hitCount18, hitCount19, hitCount20;
      Game() {
        imageMode(CENTER);
        loadImages();
        shoots=0;
      }
    
      void play() {
        image(backImage, width/2, height/2);
        target();
        for (int i=0; i<shots.size(); i++) {
          if (shoots==20) {
            last();
    
            break;
          }
          Ball b= shots.get(i);
          b.move();
          b.display();
          b.checkGroundCollision();
          if (b.alive==false) {
            shots.remove(i);
            i--;
          }
        }
    
        displayCannon();
      }
    
      void createShot() {
        shots.add( new Ball(new PVector(cannonPower*cos(radians(cannonAngle)), cannonPower*sin (radians(cannonAngle))*-1)));
        shoots = shoots+1;
      }
    
      void target() {
    
        image(targets, rx, ry);
      }
    
      void counts() {
        if (shoots==0) {
          image(ballCounter1, width/2, 60);
        } else if (shoots== 1) {
          image(ballCounter2, width/2, 60);
        } else if (shoots==2) {
          image(ballCounter3, width/2, 60);
        } else if (shoots==3) {
          image(ballCounter4, width/2, 60);
        } else if (shoots==4) {
          image(ballCounter5, width/2, 60);
        } else if (shoots==5) {
          image(ballCounter6, width/2, 60);
        } else if (shoots==6) {
          image(ballCounter7, width/2, 60);
        } else if (shoots==7) {
          image(ballCounter8, width/2, 60);
        } else if (shoots==8) {
          image(ballCounter9, width/2, 60);
        } else if (shoots==9) {
          image(ballCounter10, width/2, 60);
        } else if (shoots==10) {
          image(ballCounter11, width/2, 60);
        } else if (shoots==11) {
          image(ballCounter12, width/2, 60);
        } else if (shoots==12) {
          image(ballCounter13, width/2, 60);
        } else if (shoots==13) {
          image(ballCounter14, width/2, 60);
        } else if (shoots==14) {
          image(ballCounter15, width/2, 60);
        } else if (shoots==15) {
          image(ballCounter16, width/2, 60);
        } else if (shoots==16) {
          image(ballCounter17, width/2, 60);
        } else if (shoots==17) {
          image(ballCounter18, width/2, 60);
        } else if (shoots==18) {
          image(ballCounter19, width/2, 60);
        } else if (shoots==19) {
          image(ballCounter20, width/2, 60);
        }
      }
    
      void hitsCounters() {
        if (hitsCounter==0) {
          image(hitCount, 900, 60);
        } else if (hitsCounter==1) {
          image(hitCount, 900, 60);
        } else if (hitsCounter==2) {
          image(hitCount2, 900, 60);
        } else if (hitsCounter==3) {
          image(hitCount3, 900, 60);
        } else if (hitsCounter==4) {
          image(hitCount4, 900, 60);
        } else if (hitsCounter==5) {
          image(hitCount5, 900, 60);
        } else if (hitsCounter==6) {
          image(hitCount6, 900, 60);
        } else if (hitsCounter==7) {
          image(hitCount7, 900, 60);
        } else if (hitsCounter==8) {
          image(hitCount8, 900, 60);
        } else if (hitsCounter==9) {
          image(hitCount9, 900, 60);
        } else if (hitsCounter==10) {
          image(hitCount10, 900, 60);
        } else if (hitsCounter==11) {
          image(hitCount11, 900, 60);
        } else if (hitsCounter==12) {
          image(hitCount12, 900, 60);
        } else if (hitsCounter==13) {
          image(hitCount13, 900, 60);
        } else if (hitsCounter==14) {
          image(hitCount14, 900, 60);
        } else if (hitsCounter==15) {
          image(hitCount15, 900, 60);
        } else if (hitsCounter==16) {
          image(hitCount16, 900, 60);
        } else if (hitsCounter==17) {
          image(hitCount17, 900, 60);
        } else if (hitsCounter==18) {
          image(hitCount18, 900, 60);
        } else if (hitsCounter==19) {
          image(hitCount19, 900, 60);
        } else if (hitsCounter==20) {
          image(hitCount20, 900, 60);
        }
      }
    
      void displayCannon() {
        pushMatrix();
        translate(73, 525);
        rotate(radians(cannonAngle*-1));
        image(barrel, 0, 0);
        popMatrix();
        image(base, 73, 525);
      }
    
      void increseAngle(Boolean increase) {
        if (increase && cannonAngle <90) cannonAngle+=2;
        else if (increase ==false && cannonAngle >0)cannonAngle-=2;
      }
    
      void last() {
        image(backImage2, width/2, height/2);
        fill(255);
        textSize(32);
        text("still working on what will be said here but good job!", 100, height/2);
      }
    
      void loadImages() {
        backImage = loadImage("background.png");
        base=loadImage("base.png");
        barrel= loadImage("barrel.png");
        backImage2= loadImage("backgroundReport.png");
        targets= loadImage("target.png");
        ballCounter1 = loadImage("shots20.png");
        ballCounter2 = loadImage("shots19.png");
        ballCounter3 = loadImage("shots18.png");
        ballCounter4 = loadImage("shots17.png");
        ballCounter5 = loadImage("shots16.png");
        ballCounter6 = loadImage("shots15.png");
        ballCounter7 = loadImage("shots14.png");
        ballCounter8 = loadImage("shots13.png");
        ballCounter9 = loadImage("shots12.png");
        ballCounter10 = loadImage("shots11.png");
        ballCounter11 = loadImage("shots10.png");
        ballCounter12 = loadImage("shots9.png");
        ballCounter13 = loadImage("shots8.png");
        ballCounter14 = loadImage("shots7.png");
        ballCounter15 = loadImage("shots6.png");
        ballCounter16 = loadImage("shots5.png");
        ballCounter17 = loadImage("shots4.png"); 
        ballCounter18 = loadImage("shots3.png");
        ballCounter19 = loadImage("shots2.png");
        ballCounter20 = loadImage("shots1.png");
        hitCount=loadImage("target0.png");
        hitCount1= loadImage("target1.png");
        hitCount2= loadImage("target2.png");
        hitCount3= loadImage("target3.png");
        hitCount4= loadImage("target4.png");
        hitCount5= loadImage("target5.png");
        hitCount6= loadImage("target6.png");
        hitCount7= loadImage("target7.png");
        hitCount8= loadImage("target8.png");
        hitCount9= loadImage("target9.png");
        hitCount10= loadImage("target10.png");
        hitCount11= loadImage("target11.png");
        hitCount12= loadImage("target12.png");
        hitCount13= loadImage("target13.png");
        hitCount14= loadImage("target14.png");
        hitCount15= loadImage("target15.png");
        hitCount16= loadImage("target16.png");
        hitCount17= loadImage("target17.png");
        hitCount18= loadImage("target18.png");
        hitCount19= loadImage("target19.png");
        hitCount20= loadImage("target20.png");
      }
    }
    
    void tops() {
      fill(0);
      rect(0, 30, currentGame.cannonPower+70, 50);
    }
    
  • you solved it?

  • No, that's all of the code that we need to use to make the project work. I just can't figure out how to tell when the cannonball and target collided

  • Please format it better

  • I am unsure how to

  • edit post, highlight code, press ctrl-o. That's all it needs.

  • Then read the Common Questions about arrays because hitcount20 etc is ridiculous.

  • Okay, thanks. there's the formatted one

  • Line 73 has a loop. But nothing inside the loop changes so you're just doing the exact same test 21 times.

  • That's the part where i was trying to test if the cannonball touched the target

  • Please use loops. Please. :((

Sign In or Register to comment.