How to make a shape "die"

edited December 2013 in Questions about Code

Hi. I am relatively new to programming, and I thought it would be fun to make a "shooter" kind of game. I am stuck on trying to make the enemy shape die... It would be awesome if I could get the current enemy circle to disappear and have another one spawn... Here is my code!

.....

ellipse(enemyX, enemyY, 50, 50); // enemy circle

....

if(mousePressed) {                                                                  
    line(playerA, playerB, mouseX, mouseY);  // bullet animation
    if(mouseX == enemyX && mouseY == enemyY) {  // checking accuracy
        *unknown code*
    }
}

.......

Tagged:

Answers

  • if(mousePressed) {
    line(playerA, playerB, mouseX, mouseY); bullet animation
    if(dist(mouseX,mouseY,enemyX,enemyY)<25) {
      enemyX = random(width);
      enemyY = random(height);
    }
    
  • Thanks @TfGuy44, I never thought about just changing his position to make him "die" and "respawn", do you know a way where i could make it so if the player kills one enemy, two spawn instead of just the one?

  • This is one of my older examples - the concept is the same. In short, define your enemy object as it's own class, and have an ArrayList of them. When one gets removed from the ArrayList, two more get added.

    class FallingBall {
      float X, Y, dY, R;
      color C;
      boolean removeMe;
      FallingBall(float _X, float _Y, float _dY, float _R){
        X=_X;
        Y=_Y;
        dY=_dY;
        R=_R;
        C = color(random(128,255),random(128,255),random(128,255));
        removeMe = false;
      } 
      void simulate(){
        Y+=dY;
        if( Y > height + 30 ){ 
          Y = -30;
          shrink = true;
          mouseC = C;
        }
        if( !removeMe && (2*dist(mouseX, mouseY, X, Y) < R + mouseR )){
          removeMe = true;
          if( fallers.size() < MAX_FALLERS ){
            float R1 = random(30,50);
            float R2 = random(30,50);
            fallers.add( new FallingBall( random(0+R1,width-R1), -30, dY+.05, R1) );
            fallers.add( new FallingBall( random(0+R2,width-R2), -30, 1, R2) );
          }
        }
        if( shrink ){ R--; }
        if( R <= 0 ){ removeMe = true; }
      }
      void render(){
        fill(mousePressed?color(random(255),random(255),random(255)):C);
        ellipse(X,Y,R,R);
      }
      boolean alive(){ 
        return !removeMe; 
      }
    }
    
    int MAX_FALLERS = 500;
    boolean shrink = false;
    ArrayList fallers;
    float mouseR = 25;
    color mouseC = color(random(128,255),random(128,255),random(128,255));
    
    void setup(){
      size(400,400);
      smooth();
      ellipseMode(CENTER);
      noStroke();
      fallers = new ArrayList();
      fallers.add( new FallingBall( width/2.0, -30, 1, 50) );
    }
    void draw(){
      simulate();
      render();
    }
    void simulate(){
      for( int i=fallers.size()-1;i>=0;i--){
        ((FallingBall) fallers.get(i)).simulate();
      }
      if( shrink && fallers.size() == 0 ){
        shrink = false;
        fallers.add( new FallingBall( width/2.0, -30, 1, 50) );
      }
    }
    void render(){
      background(mousePressed?color(random(255),random(255),random(255)):0);
      for( int i=fallers.size()-1;i>=0;i--){
        FallingBall temp = (FallingBall) fallers.get(i);
        if( temp.alive() ){
          temp.render();
        } 
        else {
          fallers.remove(i);
        }
      }
      fill(mousePressed?color(random(255),random(255),random(255)):mouseC);
      ellipse(mouseX, mouseY, mouseR, mouseR);
    }
    
  • Oh, and press mouse for RAVE PARTY MODE.

  • Hey cole,

    Definitely check out TfGuy44's code and take a look at the Object Oriented Programming tutorial to learn about classes and objects. If you want to make a shooter-type program you'll probably want to have different enemy types, which would require classes and objects.

    If you want to draw multiple enemies, the best way that I know of is to run a for loop in your draw() function. This way each of your enemy objects will be drawn every frame.

           Enemy[] enemies;
            int i;
    
            void setup() {
              enemies = new Enemy[5];
              for(i = 0; i < enemies.length; i++){
                enemies[i] = new Enemy(10*i,10*i);
              }
            }
    
            void draw() {
              background(255);
              stroke(0);
              fill(255,0,0);
              for(i = 0; i < enemies.length; i++) {
              enemies[i].display();
              }
            }
    
            class Enemy {
              int x;
              int y;
    
              Enemy(int x_, int y_) {
                x = x_;
                y = y_;
              }
    
              void display() {
                rect(x,y,5,5);
              }
            }
    

    You can use a similar for loop in the draw() function to move each enemy, and you can use a for loop in the mouseClicked() function to check each enemy to see if it's been clicked. I would also suggest using some coordinates-checking system that lets you hit the enemy without clicking exactly on its origin, like something below:

    for(k = 0; k < myThings.length; k++){
        if(myThings[k].xloc-actualMouseX<=(myThings[k].size/2)
        & actualMouseX-myThings[k].xloc<=(myThings[k].size/2)
        & myThings[k].yloc-actualMouseY<=(myThings[k].size/2)
        & actualMouseY-myThings[k].yloc<=(myThings[k].size/2))
        {
         myThings[k].bulletHit;
        }
    

    Cheers!

Sign In or Register to comment.