Mouse Press on Image to Add Extra Lives

edited May 2017 in Library Questions

I have a scoreboard and life displayer in my program. I have a random object flying around the screen. I want the user to be able to press on the flying object and they will earn an extra life each time they click on it. What is the code I would need for this?

Answers

  • edited May 2017
    import processing.sound.*; //Import background music
    SoundFile file;
    
    //Arrays
    DuckLine[] ducks = new DuckLine[0];
    DuckLineLeft[] ducks2 = new DuckLineLeft[0];
    Particle[] particles = new Particle[0]; //particles that appear when duck is shot //#creative
    
    //Ints
    int score = 0;
    int lives=5;
    boolean GameOver = false;
    
    //Images
    PImage duck; //bottom green ducks (originial duck images from Duck Hunt)
    PImage BG; //origninal Nintendo Duck Hunt background
    PImage ducksmall; //top purple ducks (originial duck images from Duck Hunt)
    PImage MouseCursor; //use originial Duck Hunt gun that came with game
    PImage bulletbill; //bulletbill is "ammo"! Click on him to earn an extra life
    
    //Position for bulletbill
    int siz = 30;
    int posX = 5; //initial position
    int posY = 100;
    int velX = 4; 
    int velY = 6;
    int radius = 25;
    
    void setup() {
      size(1020, 720);
      bulletbill = loadImage("bulletbill.png"); //load the image in the setup section
      smooth();
    
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "backgroundmusic.mp3"); //#creative
      file.play();
    
      MouseCursor = loadImage("MouseCursor.png"); //create gun cursor
      //#creative
    
      //Images
      BG = loadImage ("BG.png");
      duck = loadImage ("duck.png");
      ducksmall = loadImage ("ducksmall.png");
    }
    
    void draw() {
    
      {
        if (mouseX < 100) {
          cursor(MouseCursor, 0, 0);
        } else {
          cursor(MouseCursor);
        }
      }
    
      if (!GameOver) {  
        background(BG);
    
        //Particles
        while (particles.length>maxParticles) {
          particles = (Particle[]) subset(particles, 1);
        }
        for (int i=0; i<particles.length; i++) {  
          particles[i].update();
        }
    
        //Inset Bulletbill and move him around screen randomly
        posX = posX + velX; //these ones must be within the brackets but the initial variables must be at the top
        posY = posY + velY;
    
        //creates X and Y borders
        if (posX > width ||posX < 0) {
          velX = velX * -1;
        }
        if (posY > height ||posY < 0) {
          velY = velY * -1;
        }
        //point the bouncing elements
        //ellipse(posX, posY, 30, 30);
        image(bulletbill, posX-50, posY-22);
    
        //Add a new duck 
        if (frameCount % 40 == 0) {
          DuckLine duck = new DuckLine();
          ducks = (DuckLine[]) append(ducks, duck);
        }
    
        if (frameCount%40==0) {
          DuckLineLeft duck2 = new DuckLineLeft();
          ducks2 = (DuckLineLeft[]) append(ducks2, duck2);
        }
        //Remove Duck after 30 have been created
        if (ducks.length>30) {
          ducks = (DuckLine[]) subset(ducks, 1);
          ducks2 = (DuckLineLeft[]) subset(ducks2, 1);
        }
        //Move Duck
        for (int i = 0; i<ducks.length; i++) {
          ducks[i].update();
          ducks2[i].update();
        }
      } else {
        fill(255);
        textSize(26);
        text("GAME OVER", width/2-100, height/2); 
        text("Press any key to play again", width/2-100, height/2+100); 
        frameCount=0;
        if (keyPressed == true) {
          reset();
        }
      }
      //Check if duck has left the stage
      for (int i = 0; i<ducks.length; i++) {
        DuckLine duck = ducks[i];
        if ((duck.x<-20)&&(duck.x>-23)) {
          lives--;
        }
      }
    
      //Check if duck has left the stage
      for (int i = 0; i<ducks.length; i++) {
        DuckLineLeft duck2 = ducks2[i];
        if ((duck2.x>width+duck2.w) && (duck2.x<width+duck2.w+3)) {
          lives--;
        }
      }
    
      //println(mouseX + "." +  mouseY);
      // Game Over   
      if (lives<0) {
        GameOver =true; 
        lives=0;
      } 
    
      image(BG, 1020, 720);  //Background image size
    
      //Score 
      textSize(26);
      fill(255);
      stroke(0);
      rect(440, 559, 150, 80);
      fill(0);
      text("Score:" + score, 457, 569+20);
      text("Lives:" + lives, 457, 600+20);
    } // End of Draw
    
    void mousePressed() {
      {
        file = new SoundFile(this, "gunshot.mp3"); //Play gunshot sound when mouse pressed
        file.play();
        //#creative
      }
    
      for (int i = 0; i<ducks.length; i++) {
        DuckLine duck = ducks[i];
        if ((mouseX>duck.x) && (mouseX<duck.x + duck.w) && (mouseY>duck.y) && (mouseY<duck.y+duck.h)) {
          duck.alive = false;
          score++;
          for (int ii = 0; ii<30; ii++) {
            Particle p = new Particle(mouseX, mouseY);
            p.yVel = random(-2, 2);
            p.xVel = random(-5, 5);
            p.gravity = 0.1;
            p.shrink = 0.94;
            particles = (Particle[]) append(particles, p );
          }
        }
      }
      for (int i = 0; i<ducks2.length; i++) {
        DuckLineLeft duck2 = ducks2[i];
        if ((mouseX>duck2.x) && (mouseX<duck2.x + duck2.w) && (mouseY>duck2.y) && (mouseY<duck2.y+duck2.h)) {
          duck2.alive = false;
          score++;
          for (int ii = 0; ii<30; ii++) {
            Particle p = new Particle(mouseX, mouseY);
            p.yVel = random(-2, 2);
            p.xVel = random(-5, 5);
            p.gravity = 0.1;
            p.shrink = 0.94;
            particles = (Particle[]) append(particles, p );
          }
        }
      }
    }
    
    float maxParticles = 50; //Particles that appear when duck is shot
    class Particle {
    
      float x;
      float y;
      float xVel;
      float yVel;
      float particleSize;
      float opacity;
      float gravity;
      float fadeSpeed;
      float shrink;
    
      Particle(float xpos, float ypos) {
        x = xpos;
        y = ypos;
        xVel = random(-5, 5);
        yVel = random(-5, 5);
        particleSize = random(1, 5);
        opacity = 255;
        gravity = 0;
        fadeSpeed = 1;
        shrink =2;
      }
    
      void update() { //Update game
        x+=xVel;
        y+=yVel;
        opacity*=fadeSpeed;
        yVel += gravity;
        particleSize*=shrink;
    
        //noStroke();
        fill(255, 0, 0, opacity); //Blood red particles when killed 
        ellipse(x, y, particleSize, particleSize);
      }
    }
    
    void reset() { //Reset game
      lives =5;
      score=0;
      GameOver = false;
      ducks = (DuckLine[]) new DuckLine[0];
      ducks2 = (DuckLineLeft[]) new DuckLineLeft[0];
    }
    
    //CLASS DUCKLINE
    class DuckLine { //Purple duck line coming in at right
      float x;
      float y;
      float w;
      float h;
      float velX;
      float velY;
      float speed;
      boolean alive;
      DuckLine() {
        y = 200;
        w = 50;
        x = width+30;
        h = 50;
        velX = -2;
        velY = 0;
        alive = true;
        speed=-1;
      } 
      void update() {
        x+=velX;
        y+=velY*speed;     
        if (alive) {
          fill(255, 0, 0);
          stroke(255);
        } else {
          fill(0, 0, 0); 
          noStroke();
          velY-=1;
          velX=0;
          x=-1000;
        }
        image(ducksmall, x-50, y-20);
      }
    }
    
    CLASS DUCKLINE LEFT
    class DuckLineLeft { //Green duckline coming in at left
      float x;
      float y;
      float w;
      float h;
      float velX;
      float velY;
      boolean alive;
      DuckLineLeft() {
        y = 400;
        w = 80;
        x = 0-w-50;
        h = 80;
        velX = -2;
        velY = 0;
        alive = true;
      } 
      void update() {
        x-=velX;
        y+=velY;     
        if (alive) {
          fill(255, 0, 0);
          stroke(255);
        } else {
          fill(0, 0, 0); 
          noStroke();
          velY+=1;
          velX=0;
          x=-1000;
        }
    
        image(duck, x-50, y-40);
     }
    }
    
  • Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

  • You want an if check that is a "collision detection", based on the shape and location of your object compared to the location of the mouse when clicked.

    http://www.jeffreythompson.org/collision-detection/table_of_contents.php

    On click, IF a collision is detected THEN lives++

    http://www.jeffreythompson.org/collision-detection/point-rect.php

    For example, check out Point / Rectangle collision examples if your target hit box is rectangular.

  • edited May 2017

    Thank you but I'm still unsure how to correctly type the collision code to detect my image "bulletbill.png" that moves randomly across the screen. Can you show me the code for that and where to put it? I'm very new to coding.

Sign In or Register to comment.