# Processing SpaceInvaders-Game

**URL:** https://discourse.processing.org/t/processing-spaceinvaders-game/6126
**Category:** Coding Questions
**Created:** [November 30, 2018, 8:10pm UTC](https://discourse.processing.org/t/processing-spaceinvaders-game/6126 "2018-11-30T20:10:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ProcessingHelp](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@ProcessingHelp](https://discourse.processing.org/u/ProcessingHelp)
#### Post date: [November 30, 2018, 8:10pm UTC](https://discourse.processing.org/t/processing-spaceinvaders-game/6126/1 "2018-11-30T20:10:41Z")

</div>

Delete this post.

```auto

```

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [December 2, 2018, 8:40pm UTC](https://discourse.processing.org/t/processing-spaceinvaders-game/6126/2 "2018-12-02T20:40:54Z")

</div>

Hi,

Please format your code before posting. You can still edit your previous post to format the code.

Before answering some of your questions, there are some typos in your code.

- When you declare an arrayList, you want to declare the type of the object you put in it. So you should write this: `ArrayList<Bullet> bullets = new ArrayList<Bullet();`
- In the last for loop of your draw, it should be `i--`
- There are some `z2` and `r2` that does not exist

Now to get to your question, let’s focus on why the bullet disappear. When you hit space, a new bullet is created.

```auto
void keyPressed() {
  if (key ==' ') {
    Bullet bullet = new Bullet(ship.x, height);
    bullets.add(bullet);
  }
  ...
}

```

As you can see, the bullet is added to the `bullets` arrayList.  
Now, how do they appear on screen? Well for that, we need to look at the `draw()` function:

```auto
void draw() {
  ...

  for (Bullet b : bullets) {
    b.display();
    ...
  }

   ...
}

```

As you can see, we are looping through all the bullets that are in the `bullets` arrayList and for each one of them you are calling their `display()` function

Now, if you want to get rid of one, the only thing you need to do is to remove them from the array because if it is not in the array anymore, you won’t be displaying it anymore. That’s what the following piece of code is doing:

```auto
void draw() {
  ...
  
  for (int i = bullets.size() - 1; i >= 0; i--) {
    Bullet b = bullets.get(i);
    if (b.toDelete) {
      bullets.remove(i); // Here we remove the bullet from the list
    }
  }
}

```

It would work the same for your invaders. The only problem right now is that you are using a fixed sized array to store your invaders `Invader[] invaders = new Invader[6];`

What you want to do is to store them in an `arrayList` so you can add and remove invaders easily.

Creating multiple levels is another story and as people uses to say on this forum, divide to conquer! Try to implement that part and then you can move on to the next step.

---

<div class="post-metadata">

### Author: ![ProcessingHelp](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@ProcessingHelp](https://discourse.processing.org/u/ProcessingHelp)
#### Post date: [December 7, 2018, 2:53pm UTC](https://discourse.processing.org/t/processing-spaceinvaders-game/6126/3 "2018-12-07T14:53:38Z")

</div>

Thank very much that you took your time to explain this to me it really helps me a lot. 😃
