how do I make an impact detector?

edited August 2017 in Questions about Code

so far, i've searched up many ways to make an impact detector, but none of them work. code is down here. how to make a code run when a bullet hits a zombie.

      ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
      PVector me, speed,zombiespeed,Bullet;
      float maxSpeed = 2.5;
      float zombiex;
      float zombiey;
      zombie zombie1;
      zombie zombie2;
      void setup() { 
      rectMode(CENTER);
      size(600, 600);
      me = new PVector();
      speed = new PVector();
      zombiespeed = new PVector();
      Bullet = new PVector();
      noCursor();
      noStroke();
      me.x = 300;
      me.y = 300;
    }
    void draw(){
      background(255);
      //walls
      if (me.x < 0){me.x = 0;}
      if (me.x > width){me.x = width;}
      if (me.y < 0){me.y = 0;}
      if (me.y > height){me.y = height;}
      //zombie movement
      if (zombiex> me.x){zombiex-= random(1,2.5);}
      if (zombiey< me.y){zombiey += random(1,2.5);}
      if (zombiex< me.x){zombiex += random(1,2.5);}
      if (zombiey > me.y){zombiey -= random(1,2.5);}
      //player
      me.add(speed);
      fill(255, 0, 0);
      ellipse(me.x, me.y, 20, 20);
      //zombie
      fill(0,150,50);
      zombie1 = new zombie();
      zombie2 = new zombie();

  //mouse
  PVector mouse = new PVector(mouseX, mouseY);
  fill(0);
  ellipse(mouse.x, mouse.y, 5, 5);
  //shoot
  if (frameCount%20==0 && mousePressed) {PVector dir = PVector.sub(mouse,me);
  dir.normalize();
  dir.mult(9);
  Bullet b = new Bullet(me, dir);
  bullets.add(b); }
  //bullet movement
  for (Bullet b : bullets) {b.bullup();
  b.bulldis();}
  }
  //bullet class
  class Bullet extends PVector { PVector vel; 
  Bullet(PVector loc, PVector vel) { super(loc.x, loc.y);
  this.vel = vel; }
  void bullup() {add(vel);}
  void bulldis() {fill(0, 150, 55);
  ellipse(x, y, 5, 5);}
  }
  class zombie{PVector pos;
zombie(PVector pos,PVector zom){super (zom.x,zom.y);
  this.pos = pos;}
    void zomup(){add(pos);}
  void zomdis(){fill 0,150,50)
  rect(x,y,25,25);}
  }
}
  //move
  void keyPressed() {
  if (key == 'w'){ speed.y = -maxSpeed; }
  if (key == 's'){ speed.y =  maxSpeed; }
  if (key == 'a'){ speed.x = -maxSpeed; }
  if (key == 'd'){ speed.x =  maxSpeed; }
  }
  //stop
  void keyReleased() {
  if (key == 'w' || key == 's'){ speed.y = 0; }
  if (key == 'a' || key == 'd'){ speed.x = 0; }
  }
Tagged:
«1

Answers

  • Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format it. Make sure there is an empty line above and below your code.

    Kf

  • edited August 2017

    Please read the collision detection faq. Circle / circle collision specifically.

  • Answer ✓

    Basically check each bullet in a for loop against each zombie

    This gives you a nested for loop- all bullets are checked against all zombies

    For collision use if(dist(......

    See reference

  • edited August 2017

    thanks christir, but I get don't know how to remove the zombie if it's hit.

  • In theory have in the class a boolean isAlive

    Set it to false on collision

    Display Zombie only when it's alive

  • edited August 2017

    The thing is that I don't know what the bullet's x and y position is, this program is not 100% made by me

  • and I don't know how to add multiple zombies, sorry if I seem like i'm really bad at processing

  • edited August 2017

    EDIT removed -- response to wrong post.

  • edited August 2017

    my current code is here

    void setup() { 
      rectMode(CENTER);
      ellipseMode(CENTER);
      size(600, 600);
      me = new PVector();
      speed = new PVector();
      Bullet = new PVector();
      zombie = new PVector();
      bulloc = new PVector();
      noCursor();
      noStroke();
      me.x = 300;
      me.y = 300;
      zombie1 = new zombie(width,height,20);
    zombie2 = new zombie(101,101,20);
    
      }
         //bullet class
      class Bullet extends PVector {
    
        PVector vel; 
      Bullet(PVector bulloc, PVector vel) { 
        super(bulloc.x,bulloc.y);
      this.vel = vel; 
      if (dist(zombie,bulloc) < 100){
      canmove = true;
    text("you hit the zombie",200,100);
    }
    }
      void bullup() {add(vel);}
      void bulldis() {
    
        fill(0, 150, 55);
      ellipse(x,y,5, 5);
    }
    //zombie class
      }
    
        class zombie{
          zombie(float zombiex1 ,float zombiey1,float zomsize1){
        zombie.x = zombiex1;
        zombie.y = zombiey1;
        zomsize = zomsize1;
      }
      void zomdis(){fill (0,150,50);
    
      rect(zombie.x,zombie.y,zomsize,zomsize);}
      }
    
      boolean isalive = true,canmove = true;
      ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
      PVector me, speed,Bullet , bulloc;
      PVector zombie;
      float maxSpeed = 2.5;
      float zomsize;
      float zombiespeed = random(1,2);
      zombie zombie1;
      zombie zombie2;
    
    void draw(){
        background(255);
    
    
    if (dist(me.x,me.y,zombie.x,zombie.y) < 20){
      canmove = false;
    text("you lose",300,100);
    }
    
      //walls
      if (me.x < 0){me.x = 0;}
      if (me.x > width){me.x = width;}
      if (me.y < 0){me.y = 0;}
      if (me.y > height){me.y = height;}
      //zombie movement
      if (zombie.x> me.x){zombie.x-= (zombiespeed);}
      if (zombie.y< me.y){zombie.y += (zombiespeed);}
      if (zombie.x< me.x){zombie.x += (zombiespeed);}
      if (zombie.y > me.y){zombie.y -= (zombiespeed);}
      //player
      me.add(speed);
      fill(255, 0, 0);
      ellipse(me.x, me.y, 20, 20);
      //zombie
      fill(0,150,50);
      if (isalive){
      zombie1.zomdis();
      zombie2.zomdis();
      }
      //mouse
      PVector mouse = new PVector(mouseX, mouseY);
      fill(0);
      ellipse(mouse.x, mouse.y, 5, 5);
      //shoot
      if (frameCount%20==0 && mousePressed) {PVector dir = PVector.sub(mouse,me);
      dir.normalize();
      dir.mult(9);
      Bullet b = new Bullet(me, dir);
      bullets.add(b); }
      //bullet movement
      for (Bullet b : bullets) {b.bullup();
      b.bulldis();}
      }
    
    
    
    
      //move
      void keyPressed() {
        if (canmove){
      if (key == 'w'){ speed.y = -maxSpeed; }
      if (key == 's'){ speed.y =  maxSpeed; }
      if (key == 'a'){ speed.x = -maxSpeed; }
      if (key == 'd'){ speed.x =  maxSpeed; }
        }
      }
      //stop
      void keyReleased() {
        if (canmove){
      if (key == 'w' || key == 's'){ speed.y = 0; }
      if (key == 'a' || key == 'd'){ speed.x = 0; }
        }
      }
    
  • It works, but I have to be a certain distance to be able to "hit" him, and I still can't find a good solution to make the zombie dissapear

  • isAlive must be inside the class because it can be different for both zombies -- remove it from outside the class

    In the class don't draw the zombie if it's not alive

    Use ctrl-t in processing to auto-format, you get better indents

  • You wrote certain distance to hit him --- what do you mean? The bullet is checked via dist() against the zombie? Where? I think you forgot that.

    Read my first post again.

    I don't understand lines 8,9, 25

    You need to check every bullet against every zombie in line 101 above

  • I think you are checking the distance in the constructor- this is wrong because this is at the start of the path the bullet is flying on.

    Instead check it throughout the flight - line 101 above.

  • thank you, I am making an improved version

  • how do you check it throuout the flight?

  • Answer ✓

    Hello !

    start by using ctrl-t in processing.

    Better Auto-format


    Woe, you haven't implemented your class Zombie fully.

    Since you have line 8 zombie = new PVector();.

    You are using this PVector too often. This leads to the situation that your 2 zombies appear always in the same spot.

    Look at these lines from your constructor

    zombie.x = zombiex1;
    zombie.y = zombiey1;
    

    here instead setting variables inside your class, you are setting a global variable PVector zombie. This leads to the situation that your 2 zombies appear always in the same spot.

    Also look at your method

    void zomdis(){
        fill (0,150,50);
        rect(zombie.x,zombie.y,zomsize,zomsize);}
    }
    

    Here you are using PVector zombie. This leads to the situation that your 2 zombies appear always in the same spot.

    Bad.

    (similar to PVector zombie you still had PVector Bullet as global variables. You need to do house cleaning in your code more often to get rid of such artifacts of older versions)


    Also note that what you do with your zombie in draw you only do for one of them. To do this for both, move the code lines into the class Zombie (new methods in the class) and call them twice for each zombie - similar as you do for bullets.


    Minimal improved, but still a lot to do for you:

    // all global variables here 
    
    // 1. bullets
    ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
    
    // 2. The Player me
    boolean canmove = true;
    PVector me, speed;
    float maxSpeed = 2.5;
    
    // 3. The zombies 
    Zombie zombie1;
    Zombie zombie2;
    
    // -----------------------------------------------------------
    // Two core functions setup and draw 
    
    void setup() { 
      rectMode(CENTER);
      ellipseMode(CENTER);
      size(600, 600);
      me = new PVector();
      speed = new PVector();
      // Bullet = new PVector();
      // zombie = new PVector();
      // bulloc = new PVector();
      noCursor();
      noStroke();
      me.x = 300;
      me.y = 300;
      zombie1 = new Zombie(width, height, 20);
      zombie2 = new Zombie(101, 101, 20);
    } // end of setup
    
    void draw() {
      background(255);
    
      if (dist(me.x, me.y, zombie1.x, zombie1.y) < 20) {
        canmove = false;
        text("you lose", 300, 100);
      }
    
      //walls
      if (me.x < 0) {
        me.x = 0;
      }
      if (me.x > width) {
        me.x = width;
      }
      if (me.y < 0) {
        me.y = 0;
      }
      if (me.y > height) {
        me.y = height;
      }
    
      //zombie movement
      if (zombie1.x> me.x) {
        zombie1.x-= (zombie1.zombiespeed);
      }
      if (zombie1.y< me.y) {
        zombie1.y += (zombie1.zombiespeed);
      }
      if (zombie1.x< me.x) {
        zombie1.x += (zombie1.zombiespeed);
      }
      if (zombie1.y > me.y) {
        zombie1.y -= (zombie1.zombiespeed);
      }
    
      //player
      me.add(speed);
      fill(255, 0, 0);
      ellipse(me.x, me.y, 20, 20);
    
      //zombie
      fill(0, 150, 50);
      if (zombie1.isalive) {//???????????????????????????
        zombie1.zomdis();
        zombie2.zomdis();
      }
    
      //mouse
      PVector mouse = new PVector(mouseX, mouseY);
      fill(0);
      ellipse(mouse.x, mouse.y, 5, 5);
    
      //shoot
      if (frameCount%20==0 && mousePressed) {
        PVector dir = PVector.sub(mouse, me);
        dir.normalize();
        dir.mult(9);
        Bullet b = new Bullet(me, dir);
        bullets.add(b);
      }
    
      //bullet movement
      for (Bullet b : bullets) {
        b.bullup();
        b.bulldis();
      }
    }//end of draw
    
    // -------------------------------------------------------------
    // Input functions 
    
    //move
    void keyPressed() {
      if (canmove) {
        if (key == 'w') { 
          speed.y = -maxSpeed;
        }
        if (key == 's') { 
          speed.y =  maxSpeed;
        }
        if (key == 'a') { 
          speed.x = -maxSpeed;
        }
        if (key == 'd') { 
          speed.x =  maxSpeed;
        }
      }
    }// end of function 
    
    //stop
    void keyReleased() {
      if (canmove) {
        if (key == 'w' || key == 's') { 
          speed.y = 0;
        }
        if (key == 'a' || key == 'd') { 
          speed.x = 0;
        }
      }
    }// end of function 
    
    // =============================================================
    
    //bullet class
    class Bullet extends PVector {
    
      PVector vel; 
    
      // constructor
      Bullet(PVector bulloc, PVector vel) { 
        // constructor
        super(bulloc.x, bulloc.y);
        this.vel = vel; 
        // if (dist(zombie, bulloc) < 100) {
        //canmove = true;
        // text("you hit the zombie", 200, 100);
        // }
      }// constructor
    
      void bullup() {
        add(vel);
      }
    
      void bulldis() {
        fill(0, 150, 55);
        ellipse(x, y, 5, 5);
      }
    }// end of class 
    
    // =============================================================
    
    //zombie class
    class Zombie {
    
      float x, y; 
      float zomsize;
      boolean isalive = true; 
      float zombiespeed = random(1, 2);
    
      // constructor
      Zombie(float zombiex1, float zombiey1, 
        float zomsize1) {
        // constructor 
        x = zombiex1;
        y = zombiey1;
        zomsize = zomsize1;
      }// constructor
    
      void zomdis() {
        // display 
        fill (0, 150, 50);
        rect(x, y, zomsize, zomsize);
      }
      //
    } // end of class 
    //
    

    But you will learn it quick, I am sure.

    Best, Chrisir ;-)

  • edited August 2017 Answer ✓

    you wrote

    how do you check it throuout the flight?

    Basically check each bullet in a for loop against each zombie

    This gives you a nested for loop-all bullets are checked against all zombies

    For collision use if(dist(......

    See reference

    ....
    ....
    //bullet movement
      for (Bullet b : bullets) {
        b.bullup();
        b.bulldis();
        if(dist(b.x,b.y,zombie1.x,zombie1.y) < 33) {
             zombie1.isAlive=false; 
        }
      }
    }//end of draw
    
  • thank you!

  • edited August 2017

    I made an improved version of the game with your help

  • You should move the zombies in an ArrayList too

  • Can you show your entire code...?

  • Btw as fas as I see you don't remove bullets from the ArrayList- you should do so when they hit a zombie or when they leave the screen

    Otherwise your list will be very long and slow things down

  • edited August 2017

    this is my current code, but I don't know how to make it so that the zombies die when they get hit, I try the dist function, it works, but I can't really get the bullet's location, when I put it in for(Bullet b: bullets)" I can't get the zombie's location help please

    ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
    ArrayList <Zombie> zombies = new ArrayList <Zombie> ();
    boolean mealive = true,canmove = true;
    PVector me, speed;
    float maxSpeed = 2.5,zombieSpeed = 0.9,score = 0, pluscore = 0.9;
    void setup() {
      rectMode(CENTER);
      ellipseMode(CENTER);
      size(1000,700);
      me = new PVector();
      speed = new PVector();
      noCursor();
      noStroke();
      me.x = 500;
      me.y = 350;} 
      void border(){
      fill(80,40,40);
      rect(500,50,1000,100);
      rect(50,350,100,1000);
      rect(950,350,100,1000);
      rect(500,650,1000,100);}
    void draw() {
      background(80,180,80); 
        border();
          text(score,450,350);
      for (Bullet b : bullets) {
        b.bullup();
        b.bulldis();}
           for (Zombie c: zombies){c.zomdis(); }
      if (!mealive){
        text("you lose", 500, 300);
    canmove = false;}
      pluscore = zombieSpeed;
      fill(100);
      textSize(30);
      if (me.x < 100) { me.x = 100;}
      if (me.x > width - 100) {me.x = width - 100;}
      if (me.y < 100) {me.y = 100;}
      if (me.y > height -100) {me.y = height -100;}
      me.add(speed);
      fill(255);
      ellipse(me.x, me.y, 20, 20);
      PVector mouse = new PVector(mouseX, mouseY);
      fill(255);
      ellipse(mouse.x, mouse.y, 10, 10); 
      if (frameCount %150 == 0){
       Zombie c = new Zombie(random(width),random(height),20);
       zombies.add(c); }
      if (frameCount%15==0 && mousePressed && mealive) {
        PVector dir = PVector.sub(mouse, me);
        dir.normalize();
        dir.mult(9);
        Bullet b = new Bullet(me, dir);
        bullets.add(b); }}
    class Bullet extends PVector { 
      PVector vel; 
      Bullet(PVector bulloc, PVector vel) { 
        super(bulloc.x, bulloc.y);
        this.vel = vel; }
      void bullup() {add(vel); }
      void bulldis() {
        fill(255);
        ellipse(x, y, 5, 5);}}
    class Zombie{ 
      float x, y; 
      float zomsize;
      boolean isalive = true; 
      float zombiespeed;
      Zombie(float zombiex1, float zombiey1,float zomsize1) {
        x = zombiex1;
        y = zombiey1;
        zomsize = zomsize1; } 
      void zomdis() {
        zombiespeed = zombieSpeed;
      if (dist(me.x, me.y, x, y ) < 20) {mealive = false;}
      if (x> me.x) {x-= (zombiespeed);}
      if (y< me.y) {y += ( zombiespeed);}
      if ( x<  me.x) {x += ( zombiespeed); }
      if ( y > me.y) {y -= ( zombiespeed);}
        fill (0,100,30);
        rect(x, y, zomsize, zomsize);}}
      void keyPressed() {
      if (canmove) {
        if (key == 'w') { speed.y = -maxSpeed;}
        if (key == 's') { speed.y =  maxSpeed;}
        if (key == 'a') { speed.x = -maxSpeed;}
        if (key == 'd') { speed.x =  maxSpeed;}}} 
    void keyReleased() {
        if (key == 'w' || key == 's') { speed.y = 0;}
        if (key == 'a' || key == 'd') { speed.x = 0;}}
    
  • Why?

    In draw you loop over the bullets

    Here just insert

    if( dist(b.x,b.y, zombie1.x, zombie1.y) < 33) zombie1.isalive=false;

    in zomdis display it only when isalive is true

    Show your attempt

    Entire code

    Thank you

  • the problem is I have multiple zombies, your code is what I used to do, but now I want more than a few zombies,so I have a few problems

  • Answer ✓

    I see

    Sorry, I overlooked that

    As I said:

    Basically check each bullet in a for loop against each zombie

    This gives you a nested for loop- all bullets are checked against all zombies

    For collision use if(dist(......

    See reference

    As I said:

    Use ctrl-t in processing to auto-format, you get better indents

    If you would have done this you would have seen that this:

    for (Bullet b : bullets) {
        b.bullup();
        b.bulldis();}
           for (Zombie c: zombies){c.zomdis(); }
    

    has a wrong bracket.

    Did you spot it? No, because you haven't used ctrl-t ....

    Here is the code with ctrl.t

    for (Bullet b : bullets) {
      b.bullup();
      b.bulldis();
      for (Zombie c : zombies) {
           if( dist(b.x,b.y, c.x, c.y) < 33) 
                c.isalive=false;
      }
    }
    
    // ----------------  
    for (Zombie c : zombies) {
      c.zomdis();
    }
    
  • Answer ✓

    Bullets also should have a boolean isAlive

    To remove bullets and zombies where isAlive is false from the ArrayLists you need to loop backward through arraylists

  • edited August 2017

    I fixed spme problems,although there still are more, here are a few of them:

    1. when a zombie gets killed, I cant really remove it, it stops getting displayed, but its location is still there, i made it so that it sets itself to -1, but if you hit -1 with your bullets, you still get score

    2. problem with health, I think that 1 health is not enough, especially because i'm making more than one type of zombie,the code looks like it would work, but it doesn't. thank you for helping!

      ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
      ArrayList <Zombie> zombies = new ArrayList <Zombie> ();
      boolean mealive = true, canmove = true;
      PVector me, speed, bul;
      float maxSpeed = 2.5, score = 0, pluscore = 0.9, zombiespeed = 1;
      void setup() {
        rectMode(CENTER);
        ellipseMode(CENTER);
        size(1000, 700);
        me = new PVector();
        speed = new PVector();
        bul = new PVector();
        noCursor();
        noStroke();
        me.x = 500;
        me.y = 350;
      } 
      void border() {
        fill(80, 40, 40);
        rect(500, 50, 1000, 100);
        rect(50, 350, 100, 1000);
        rect(950, 350, 100, 1000);
        rect(500, 650, 1000, 100);
      }
      void draw() {
        background(80, 180, 80); 
        border();
        text(score, 450, 350);
        for (Bullet b : bullets) {
          b.bullup();
          b.bulldis();
          for (Zombie c : zombies) {
            if ( dist(b.x, b.y, c.x, c.y) < 20) {
              c.health -= 1;
              if (c.health == 0);{
                c.isalive = false;
                        c.x = -1;
              c.y = -1;
                      score += pluscore;
              zombiespeed += 0.01;
              }
            }
          }
        }
        for (Zombie c : zombies) {
          c.zomdis();
        }
        if (!mealive) {
          text("you lose", 500, 300);
          canmove = false;
        }
      
        fill(100);
        textSize(30);
        if (me.x < 100) { 
          me.x = 100;
        }
        if (me.x > width - 100) {
          me.x = width - 100;
        }
        if (me.y < 100) {
          me.y = 100;
        }
        if (me.y > height -100) {
          me.y = height -100;
        }
        me.add(speed);
        fill(255);
        ellipse(me.x, me.y, 20, 20);
        PVector mouse = new PVector(mouseX, mouseY);
        fill(255);
        ellipse(mouse.x, mouse.y, 10, 10); 
        if (frameCount %200/pluscore == 0) {
          Zombie c = new Zombie(random(width), random(100), 20);
          zombies.add(c);
        }
        if (frameCount%15==0 && mousePressed && mealive) {
          PVector dir = PVector.sub(mouse, me);
          dir.normalize();
          dir.mult(9);
          Bullet b = new Bullet(me, dir);
          bullets.add(b);
        }
      }
      class Bullet extends PVector { 
        PVector vel; 
        Bullet(PVector bulloc, PVector vel) { 
          super(bulloc.x, bulloc.y);
          this.vel = vel;
        }
        void bullup() {
          add(vel);
        }
        void bulldis() {
          fill(255);
          ellipse(x, y, 5, 5);
        }
      }
      class rzombie {
        float x, y, zomsize;
        boolean isalive;
        void rzomdis() {
          if (isalive) {
            pluscore = zombiespeed;
            if (dist(me.x, me.y, x, y ) < 20) {
              mealive = false;
            }
            if (x> me.x) {
              x-= (zombiespeed *1.25);
            }
            if (y< me.y) {
              y += ( zombiespeed *1.25);
            }
            if ( x<  me.x) {
              x += ( zombiespeed*1.25);
            }
            if ( y > me.y) {
              y -= ( zombiespeed*1.25);
            }
            fill (0, 100, 30);
            rect(x, y, zomsize, zomsize);
          }
        }
      }
      class Zombie { 
        float x, y; 
        float zomsize, health = 2;
        boolean isalive = true; 
        Zombie(float zombiex1, float zombiey1, float zomsize1) {
          x = zombiex1;
          y = zombiey1;
          zomsize = zomsize1;
        } 
        void zomdis() {
          if (health <= 0){
            isalive = false;}
          if (isalive) {
            pluscore = zombiespeed;
            if (dist(me.x, me.y, x, y ) < 20) {
              mealive = false;
            }
            if (x> me.x) {
              x-= (zombiespeed);
            }
            if (y< me.y) {
              y += ( zombiespeed);
            }
            if ( x<  me.x) {
              x += ( zombiespeed);
            }
            if ( y > me.y) {
              y -= ( zombiespeed);
            }
            fill (0, 100, 30);
            rect(x, y, zomsize, zomsize);
          }
        }
      }
      void keyPressed() {
        if (canmove) {
          if (key == 'w') { 
            speed.y = -maxSpeed;
          }
          if (key == 's') { 
            speed.y =  maxSpeed;
          }
          if (key == 'a') { 
            speed.x = -maxSpeed;
          }
          if (key == 'd') { 
            speed.x =  maxSpeed;
          }
        }
      } 
      void keyReleased() {
        if (key == 'w' || key == 's') { 
          speed.y = 0;
        }
        if (key == 'a' || key == 'd') { 
          speed.x = 0;
        }
      }
      
  • Check this:

    https://processing.org/reference/ArrayList.html

    Now in draw(), replace:

     for (Zombie c : zombies) {
        c.zomdis();
      }
    

    with this:

      for (int i = zombies.size() - 1; i >= 0; i--) {
        Zombie zz = zombies.get(i);
        if (zz.isalive==false) {
          zombies.remove(i);
        } else {
          zz.zomdis();
        }
      }
    

    Also, isAlive is easier to read than isalive.

    Kf

  • Also, isAlive is easier to read than isalive.

    Alternatively, simply alive is OK too! :P

  • what is the class rzombie?

  • rzombie is a red zombie, a new type of zombie i'm going to add later

  • Maybe not a good idea to start a new class

    Can't you add color and other properties to the old class/ make different types of zombies there?

  • how do you add health?

  • and the zombies have different abilities, so I have to add a new class

  • edited August 2017 Answer ✓

    how do you add health?

    you mean for the zombies?

    You did that already.

    health = 2; etc. etc. .....

    ah, I see.

    This is your mistake:

            c.health -= 1;
            if (c.health == 0)   ; // !!!!!!!!!!!!!!!!!!!!!!!
            {
              c.isalive = false;
    

    No ; is allowed directly after if(...).

    The ; ends a command and so it says "if the if-clause applies, execute everything between if(...) and the ; " - which is nothing in this case. Bad.

    Delete the ; and it will read: "if the if-clause applies, execute everything after theif(...) in the {.....} block" - much better.

    and the zombies have different abilities, so I have to add a new class

    maybe.

    It's ok.

    Permission granted. ;-)

    Remark

    Your code still is hard to read because you use no empty lines to separate sections.

    I use e.g. //----- between sections of code (mostly multiple functions) and //====== before and after a class.

    Also empty lines between functions and between methods within a class and between sections within a function.

    Here is my version:

    ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
    ArrayList <Zombie> zombies = new ArrayList <Zombie> ();
    
    // Player 
    boolean mealive = true, canmove = true;
    PVector me, speed; // I killed bul here - !!!!!!!!!!!!!!!!!!!!!!!!!
    
    // other 
    float maxSpeed = 2.5, score = 0, pluscore = 0.9, zombiespeed = 1;
    
    // --------------------------------------------------
    // Two core functions 
    
    void setup() {
      size(1000, 700);
    
      rectMode(CENTER);
      ellipseMode(CENTER);
    
      me = new PVector();
      speed = new PVector();
      // bul = new PVector(); // killed this line  !!!!!!!!!!!!!!!!!!!
      noCursor();
      noStroke();
      me.x = 500;
      me.y = 350;
    } // setup  
    
    void draw() {
    
      background(80, 180, 80);
    
      border();
      text(score, 450, 350);
    
      // display bullets 
      for (Bullet b : bullets) {
        b.bullup();
        b.bulldis();
        // Did we hit a zombie c ? 
        for (Zombie c : zombies) {
          if ( dist(b.x, b.y, c.x, c.y) < 20) {
            c.health -= 1;
            if (c.health == 0) {
              c.isalive = false;
              c.x = -100000;
              c.y = -100000;
              score += pluscore;
              zombiespeed += 0.01;
            }//if
          }//if
        }//for
      }//for
    
      for (Zombie c : zombies) {
        c.zomdis();
      }
    
      if (!mealive) {
        text("you lose", 500, 300);
        canmove = false;
      }
    
      fill(100);
      textSize(30);
    
      if (me.x < 100) { 
        me.x = 100;
      }
      if (me.x > width - 100) {
        me.x = width - 100;
      }
      if (me.y < 100) {
        me.y = 100;
      }
      if (me.y > height -100) {
        me.y = height -100;
      }
      me.add(speed);
    
      fill(255);
      ellipse(me.x, me.y, 20, 20);
    
      PVector mouse = new PVector(mouseX, mouseY);
      fill(255, 0, 0);
      ellipse(mouse.x, mouse.y, 10, 10); 
    
      if (frameCount %200/pluscore == 0) {
        Zombie c = new Zombie(random(width), random(100), 20);
        zombies.add(c);
      }
    
      if (frameCount%15==0 && mousePressed && mealive) {
        PVector dir = PVector.sub(mouse, me);
        dir.normalize();
        dir.mult(9);
        Bullet b = new Bullet(me, dir);
        bullets.add(b);
      }
    } // draw 
    
    // --------------------------------------------------------
    // Input functions 
    
    void keyPressed() {
      if (canmove) {
        if (key == 'w') { 
          speed.y = -maxSpeed;
        }
        if (key == 's') { 
          speed.y =  maxSpeed;
        }
        if (key == 'a') { 
          speed.x = -maxSpeed;
        }
        if (key == 'd') { 
          speed.x =  maxSpeed;
        }
      }
    } //func 
    
    void keyReleased() {
      if (key == 'w' || key == 's') { 
        speed.y = 0;
      }
      if (key == 'a' || key == 'd') { 
        speed.x = 0;
      }
    } //func 
    
    // --------------------------------------------------------
    // other functions 
    
    void border() {
      fill(80, 40, 40);
      rect(500, 50, 1000, 100);
      rect(50, 350, 100, 1000);
      rect(950, 350, 100, 1000);
      rect(500, 650, 1000, 100);
    }//func 
    
    // ========================================================
    // all classes, nothing comes after it  
    
    class Bullet extends PVector { 
    
      PVector vel;
    
      // constr 
      Bullet(PVector bulloc, PVector vel) { 
        super(bulloc.x, bulloc.y);
        this.vel = vel;
      }  // constr 
    
      void bullup() {
        add(vel);
      }
    
      void bulldis() {
        fill(255);
        ellipse(x, y, 5, 5);
      }
      //
    }//class
    
    // ========================================================
    
    class rzombie {
    
      float x, y, zomsize;
      boolean isalive;
    
      // constr 
      void rzomdis() {
        if (isalive) {
          pluscore = zombiespeed;
          if (dist(me.x, me.y, x, y ) < 20) {
            mealive = false;
          }
          if (x> me.x) {
            x-= (zombiespeed *1.25);
          }
          if (y< me.y) {
            y += ( zombiespeed *1.25);
          }
          if ( x<  me.x) {
            x += ( zombiespeed*1.25);
          }
          if ( y > me.y) {
            y -= ( zombiespeed*1.25);
          }
          fill (0, 100, 30);
          rect(x, y, zomsize, zomsize);
        }//if
      }  // constr 
      //
    } // class rzombie
    
    // ========================================================
    
    class Zombie {
    
      float x, y; 
    
      float zomsize, health = 2;
    
      boolean isalive = true;
    
      // constr 
      Zombie(float zombiex1, float zombiey1, 
        float zomsize1) {
        x = zombiex1;
        y = zombiey1;
        zomsize = zomsize1;
      }// constr
    
      void zomdis() {
        if (health <= 0) {
          isalive = false;
        }
        if (isalive) {
          pluscore = zombiespeed;
          if (dist(me.x, me.y, x, y ) < 20) {
            mealive = false;
          }
          if (x> me.x) {
            x-= (zombiespeed);
          }
          if (y< me.y) {
            y += ( zombiespeed);
          }
          if ( x<  me.x) {
            x += ( zombiespeed);
          }
          if ( y > me.y) {
            y -= ( zombiespeed);
          }
          fill (0, 100, 30);
          rect(x, y, zomsize, zomsize);
        }
      }
      //
    }//class 
    // end of sketch ========================================================
    

    much better, uh?

    Your draw() is still too long.

    You could make sub-functions here like

    • bulletManager()

    • zombieManager()

    • playerManager()

    Best, Chrisir ;-)

  • thank you

  • you can use remove

  • that was not actually my problem

  • the problem was that the bullet kept touching the zombie and it gets checked twice, and the zombie loses 2 health. the solution is to remove the bullet whenever it gets used

  • Well done !

  • edited August 2017

    I have another problem, I want the red zombie to be able to place fire, but it says that add(fire) does not exist

        `PImage fireim;`
              for (int i = rzombies.size() - 1; i >= 0; i--) {
                rzombie zz = rzombies.get(i);
                if (zz.isalive==false) {
                  rzombies.remove(i);
                } else {
                  if (frameCount %130 == 0 && zz.isalive){
                    fire d = new fire(zz.x,zz.y);
                    fire.add(d);
                  }
                  zz.rzomdis();
                }
              }
    
            class fire{
              fire(float firex,float firey){
                x = firex;
                y = firey;
              }
              float x,y;
              float duration = 10;
              void fdis(){
                fireim = loadImage("fire.jpg");
                image(fireim,x,y);
              }
            }
    
  • edited August 2017

    I don't know why it keeps happening

  •     for (fire d: fires) {
        if (d.isalive==false) {
        } else {
          d.fdis();
        }
      }
    
  • never mind syntax error

  • fixed it it's add fires not add fire

  • i'm ending this discussion it's way too long

  • Some discussions continue on the next page even....

  • Fire is like bullets but with fire images?

Sign In or Register to comment.