#Adding enemy fire to my space shooter game

61 messages · Page 1 of 1 (latest)

fringe parrot
#

I am attempting to add a method that has my enemies shoot at my space ship. I have everything working but I am having trouble coming up with a way to control when the enemy fires.

#

If you can offer any input here is my repo on github.

#

I just uploaded my changes.

#

Everything is working with a test button, I am just figuring out how to control when the enemy fires back.

#
 boolean enemy_fire() {
    float c_time = millis();
    float d_time = c_time - last_burst;
    boolean state = false;
    if (last_burst == 0 || d_time >= enemy_fire_interval) {
      state = !state;
      last_burst = c_time;
    }
    println("led state is.........." + state);
    return state;
  }```
#
void add_E_bullet() {
  int lastIndex = enemy_list.size() - 1;
  for (int i = lastIndex; i >= 0; i--) {
    if (enemy_list.get(i).enemy_fire() == true) {
      float enemy_x = enemy_list.get(i).x;
      float enemy_y = enemy_list.get(i).y;
      e_bullet = new Bullet()
        .setPosition(enemy_x, enemy_y)
        .setDimension(9, 9)
        .setSpeed(-15, 0)
        .setColor(e_bullet_color);
      e_bullet_list.add(e_bullet);
      //println("e_bullet_list size.........." +e_bullet_list.size());
    }
  }
}```
#

I do get a bullet, but only one. I can see a change to true but it doesn't produce any bullets. How can I make it stay on for just a bit longer? Any input is welcome.

#

@bright yoke Any input? Sorry to bug you again.

bright yoke
#

Don't worry! One moment while I download everything

fringe parrot
#

Nested if statements? outer for on and inner for off?

bright yoke
#

What does the state variable mean

fringe parrot
#

It is the state of the enemy fire. On = true Off = false.

#

I want a short burst of fire every n milliseconds.

#

I do get false with a true value for like 1 frame on the interval.

bright yoke
#

Something very weird is happening. last_burst is being updated, but state is not. I add some print statements to check this: ```processing
boolean enemy_fire() {
float c_time = millis();
float d_time = c_time - last_burst;
println("Millis:", c_time, "Last burst:", last_burst, "D Time:", d_time);

boolean state = false;
if (last_burst == 0 || d_time >= enemy_fire_interval) {
state = !state;
last_burst = c_time;
println("HERE");
}
println("led state is.........." + state);
return state;
}

And this is the kind of output I would get: ```
Millis: 13765.0 Last burst: 13257.0 D Time: 508.0
led state is..........false
Millis: 13765.0 Last burst: 13257.0 D Time: 508.0
led state is..........false
Millis: 13765.0 Last burst: 13257.0 D Time: 508.0
led state is..........false
...
#

If last_burst was being changed, I would expect to see "HERE" somewhere in the output, but I never did

#

At first, I thought last_burst was being changed somewhere else in the code, but I couldn't find any other place it was updated

fringe parrot
#

If I watche the print statement in my testjava float last_burst = 0; float enemy_fire_interval = 1000; void setup() { size(200, 200); background(0); } void draw() { enemy_fire(); } boolean enemy_fire() { float c_time = millis(); float d_time = c_time - last_burst; boolean state = false; if (last_burst == 0 || d_time >= enemy_fire_interval) { state = !state; last_burst = c_time; } println("led state is.........." + state); return state; }

#

I see false with a blip of true, so I thought I was good. When I drop it in it doesnt work right.

#

I was hoping for about 5 bullets or so in a burst.

#

The test does act like I expect.

bright yoke
#

I'm doing some tests

fringe parrot
#

I'm fooling around too.🤓

#

Fun stuff. I love working on a problem.

#

Been thinking on it most of the day today

#

I knew the real work was going to be taking out the test button.

#

Interesting. Making your changes to my test method works.```java
Millis: 18317.0 Last burst: 17333.0 D Time: 984.0
led state is..........false
Millis: 18335.0 Last burst: 17333.0 D Time: 1002.0
HERE
led state is..........true
Millis: 18350.0 Last burst: 18335.0 D Time: 15.0
led state is..........false

#

I got this when I put your print statements in.```java
Millis: 3051.0 Last burst: 2058.0 D Time: 993.0
led state is..........false
Millis: 3068.0 Last burst: 2058.0 D Time: 1010.0
HERE
led state is..........true
Millis: 3068.0 Last burst: 3068.0 D Time: 0.0
led state is..........false
Millis: 3084.0 Last burst: 3068.0 D Time: 16.0
led state is..........false

#

How to make it stay true for just a few more milliseconds?

#

I wonder if I shorten the interval

#

Nope. As expected it just goes true more often, not for longer duration. Maybe a duration variable?

#

I also just learned from you that you can use commas in a println statement. LOL

bright yoke
fringe parrot
#

I think I have brain lock. I have been at it for about 6 hours. lol

bright yoke
#

ok so I didn't exactly solve the problem, but I managed to do what you wanted to, in a sperate sketch. Here's a basic sketch that does what you want.
BulletTest:

int BURST_INTERVAL = 1000;
int FIRE_INTERVAL = 75;

ArrayList<Enemy> enemies;
ArrayList<Bullet> bullets;

void setup() {
  size(600, 600);
  
  enemies = new ArrayList<>();
  bullets = new ArrayList<>();
  
  enemies.add( new Enemy(width/2 - 25, height/2 - 25) );
}

void draw() {
  background(125);
  
  for (Enemy enemy : enemies) {
    enemy.display();
    enemy.fire();
  }
  
  for (int i = bullets.size() - 1; i >= 0; i--) {
    Bullet bullet = bullets.get(i);
    bullet.display();
    bullet.update();
    
    if (bullet.x < 0)
      bullets.remove(i);
  }
}

void mouseClicked() {
  enemies.add( new Enemy(mouseX, mouseY) );
}

Bullet:


class Bullet {
  float x, y;
  
  float speed;
  
  Bullet(float x, float y) {
    this.x = x;
    this.y =y;
    
    speed = 5;
  }
  
  void display() {
    fill(255);
    stroke(0);
    ellipse(x, y, 10, 10);
  }
  
  void update() {
    x -= speed;
  }
}

Enemy


class Enemy {
  float x, y;
  
  int remBullets;
  int lastBurst, lastFire;
  
  Enemy(float x, float y) {
    this.x = x;
    this.y =y;
    
    remBullets = 5;
    lastBurst = 0;
    lastFire = 0;
  }
  
  void display() {
    stroke(0);
    fill(200, 0, 0);
    rect(x, y, 50, 50);
  }
  
  void fire() {
    int cTime = millis();
    
    if (cTime - lastBurst < BURST_INTERVAL) {
      if (cTime - lastFire > FIRE_INTERVAL) {
        if (remBullets > 0) {
          lastFire = cTime;
          
          bullets.add( new Bullet(x, y + 25) );
          remBullets -= 1;
        }
      }
    }
    else {
      lastBurst = cTime;
      remBullets = 5;
    }
  }
}
fringe parrot
#

Ahhhh, nested if statements. I was on the right track above. ```java
if (cTime - lastBurst < BURST_INTERVAL) {
if (cTime - lastFire > FIRE_INTERVAL) {
if (remBullets > 0) {
lastFire = cTime;

      bullets.add( new Bullet(x, y + 25) );
      remBullets -= 1;
    }
  }
}
else {
  lastBurst = cTime;
  remBullets = 5;
}```
fringe parrot
#

This is what Im looking for too. The bullets stay if the enemy is shot so the ship will need to avoid them.

#

Once again. Thank you very much for the input.

bright yoke
#

Another thing I noticed about your code, you have the last_burst outside of any class. It's in the Enemy tab, but not the Enemy class. I suggest giving each enemy their own last_burst variable, like I did.

bright yoke
fringe parrot
#

Just gotta work on a little longer of a burst.

bright yoke
#

Here's a simpler version of the fire() method I wrote ```processing
void fire() {
int cTime = millis();

if (cTime - lastBurst < BURST_INTERVAL) {
if (cTime - lastFire > FIRE_INTERVAL && remBullets > 0) {
lastFire = cTime;

  bullets.add( new Bullet(x, y + 25) );
  remBullets -= 1;
}

}
else {
lastBurst = cTime;
remBullets = 5;
}
}

(The only difference is that I eliminated that third if-statement by conjoining it to the other one)
fringe parrot
#

I am so happy with my progress.

bright yoke
#

You might notice that with my code it actually starts with a burst of 10 rather than 5. You can fix that by changing lastBurst = -BURST_INTERVAL; in my constructor so that it waits the right amount of time

fringe parrot
#

I need to start thinking about an explosion after an enemy is shot. A particle burst at the location of the enemy that was shot.

#

I have to make/eat supper. I will be working on this later and tomorrow. Soooo much fun!!!!

bright yoke
#

Explosions are cool

fringe parrot
#

I like the burst of 5-10 bullets.

bright yoke
fringe parrot
#

I am settling on 5.

bright yoke
#

cool

fringe parrot
#

I need to feed myself and my family LOL. I will make my updates and take it from there. I consider you my friend. I am sure I will talk to you soon.

bright yoke
#

Alrighty, see you later! 👋

fringe parrot
#

@bright yoke Updates are finished and uploaded to my repo if you want to check it out. Thanks again. Very good lesson in nested if statements.

#

It works perfectly.