HORIZONTAL SHOOTING GAME BULL OPERATION METHOD

Asked 2 years ago, Updated 2 years ago, 105 views

I'm making a simple horizontal shooting game with java, but I can't fire the bullet properly.Originally, I want to fire one bullet each time I click, but the bullet doesn't work properly from the second click.We have already completed the operation of our own aircraft and the movement of the enemy aircraft.

I think the reason is probably the part surrounded by ////, but I don't know how to solve it.
I would appreciate it if you could let me know.

How the bullet works
How the bullet works

Current source code

MyCharacter mc;//Self-Equipment
Enemy enemy[] = new Enemy[10]; // Array containing enemies
Bullet Bullet [] = new Bullet [10]; // Array containing bullets
boolean bulletJudge;// Determine if a bullet is fired
int bulletCount; // Number of bullets being fired

void setup() {
    size (800,800);
    mc = new MyCharacter();
    for(inti=0;i<10;i++){
      enemy[i] = new Enemy();
    }
    for(inti=0;i<10;i++){
      bullet[i] = new Bullet();
    }
    BulletJudge=false;
    BulletCount = 0;
}
void draw() {
    background(255);
    mc.display(); // View My Unit
    mc.update(); // Update coordinates to display your device
    for(inti=0;i<10;i++){  
      enemy[i].display(); // Show Enemy
      enemy[i].update();// Update coordinates to display enemies
    }
/////////////////////////////////////////////////////////////
    if(mousePressed){
      BulletJudge=true;// Determined that a bullet is being fired
      bulletCount++; // Count the number of bullets being fired
      if(bulletCount>9) {// Limit the number of bullets that can be displayed on the screen simultaneously to 10
        BulletCount = 0;
      }
    }
    if(bulletJudge) {// If a bullet is fired
      for (inti=0;i<bulletCount;i++) {//Change parameters for each bullet being fired
        if(!bullet[i].isDead) {// If the shot is in the screen
          US>bullet[i].display();//View bullets
          US>bullet[i].update();// Update coordinates to display bullets
        }
      }
    }
//////////////////////////////////////////////////////////
}
class MyCharacter {//Self Class
     int size; // Diameter of own machine
     PVector loc; // Location vector of its own machine
     PVectorop; // Vector when operating your own machine
     MyCharacter(){
       size = 50;
       loc = new PVector (size/2, height/2);
     }

     void display() {//Methods for Displaying My Unit
       fill(0);
       circuit(loc.x, loc.y, size);     
     }

     void update() { // Method for updating coordinates of one's machine
       op = new PVector(0, mouseY-loc.y); // Store the vector when moving the mouse in op (x coordinates are fixed)
       loc.add(op); // Move your machine to the vector above
     }
 }

class Bullet {// bullet class
    PVector loc;// bullet position vector
    PVector speed; // bullet velocity vector
    boolean isDead;// Determine if the bullet coordinates are in-screen
    US>Bullet(){
     loc = new PVector(mc.size, mc.loc.y);
     speed = new PVector(20,0);
     isDead=false;
    }

    void display() {// method for displaying bullets
      stroke(0);
      line(this.loc.x, mc.loc.y, this.loc.x+20, mc.loc.y);
    }
    void update() {// Method for updating bullet coordinates
      This.loc.add(speed); // Change bullet position vector (move bullet)
      if(this.loc.x>width) {// If the coordinates of the bullet are off-screen
        isDead = true; // The bullet is determined to be dead
        this.loc = new PVector (mc.size, mc.loc.y); // Initialize bullet position vector
      }
    }
}

class Enemy {//Enemy class
      float size;// enemy plane diameter
      PVector loc;// enemy position vector
      float sp;// enemy plane speed
      Enemy(){
        size = 20;
        loc = new PVector(width+size/2, random(size/2, height-size/2));
        sp=random(5,10); // Randomly determines the speed of the enemy plane
      }

      void display() { // method for displaying enemy planes
        fill(0);
        circuit(loc.x, loc.y, size);
      }
      void update() {// Method of updating enemy plane coordinates (move enemy plane)
        loc.x-=sp;// Change the x coordinates of the enemy plane 
        if(loc.x+size/2<0) {// Enemy is off screen
          loc = new PVector (width+size/2, random(size/2, height-size/2)); // Initialize enemy plane coordinates
        }
      } 
}

java processing

2022-09-30 19:44

1 Answers

The bug has been bugged since the first click.

mousePressed is "true all the time the mouse button is pressed", not "true only for a moment when the mouse button is pressed".Also, the draw function is called over and over again at a very fast period compared to human motion.For this reason, all 10 shots were fired in an instant with the current implementation.For example, if you put the println(bulletCount) into the draw function and looked at the log, you would have noticed something was wrong.

Also, bulletJudge does not return to false forever once it is true, so the bullet that was shot once will move forever. After a while clicking bulletCount, it will shift and appear blinking.


2022-09-30 19:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.