Ryan
Ryan

Reputation: 45

Moving 2D Array For Space Invaders Game

So the title pretty much explains it all, recently I have learned about 2D arrays but I am a little confused on how to make the 2D array move correctly in this Space Invaders game I am creating.

Right now I have Aliens moving left to right (and vice versa), however, they don't all move down at the same time, they move down column by column. Anyone know where to edit the code?

Here is my code for the Aliens:

class Aliens {

  int x = 100, y = 75, deltaX = 1;

  Aliens (int x, int y) {
    this.x = x;
    this.y = y;
  }

  void drawAlien() {
    fill(255);
    rect(x, y, 25, 25);
  }

  void moveAlien() {
    x = x + deltaX;
    if (x >= width) {
      y = y + 20;
      deltaX = - deltaX;
    } else if (x <=0) {
      y = y + 20;
      deltaX = - deltaX;
    }
  }

  void updateAlien() {
    drawAlien();
    moveAlien();
  }
}

and my main class:

import ddf.minim.*;

//Global Variables
PImage splash;
PFont roboto;
Defender player;
Aliens[][] alienArray = new Aliens[15][3];
Missile missile;
int gameMode = 0;
int score = 0;

void setup() {
  size(1000, 750);
  rectMode(CENTER);
  textAlign(CENTER, CENTER);
  splash = loadImage("Splash.png");
  player = new Defender();
  for (int row = 0; row < 15; row++) {
    for (int column = 0; column < 3; column++) {
      alienArray[row][column] = new Aliens((row + 1) * 50, (column + 1) * 50);
    }
  }
  roboto = createFont("Roboto-Regular.ttf", 32);
  textFont(roboto);
}

void draw() {
  if (gameMode == 0) {
    background(0);
    textSize(75);
    text("Space Invaders", width/2, height/8);
    textSize(25);
    text("Created by Ryan Simms", width/2, height/4);
    textSize(45);
    text("Press SPACE to Begin", width/2, height - 100);
    image(splash, width/2-125, height/4 + 75);
  } else if (gameMode == 1) {
    background(0);
    score();
    player.updateDefender();
    for (int row = 0; row < 10; row ++) {
      for (int column = 0; column < 3; column++) {
        alienArray[row][column].updateAlien();
      }
    }
    if (missile != null) {
      missile.updateMissile();
    }
    if (keyPressed) {
      if (key == ' ') {
        if (missile == null) {
          missile = new Missile(player.x);
        }
      }
    }
    if (missile != null) {
      if (missile.y <= 0) {
        missile = null;
      }
    }
  }
}

void score() {
  textSize(20);
  text("Score: " + score, 40, 15);
}

void keyPressed() {
  if (key == ' ') {
    gameMode = 1;
  }
}

Upvotes: 1

Views: 1162

Answers (1)

G_H
G_H

Reputation: 11999

The logic flaw is in the moveAlien() method. You move the x position of an alien by a certain delta. If it is detected that the alien has passed some screen boundary (the x >= width and x <=0 checks), you invert the delta to reverse the movement of the alien, and also update the y position to have it move down.

Since the aliens are oriented in vertical columns, one column will always reach such a boundary while the others haven't yet. So that column will go down, and also begin reverting its movement. The other columns will then "catch up" later. So not only do they move down per column, the columns will also end up moving through one another.

You'll have to implement some logic to have your aliens move as a block, by detecting when the rightmost remaining alien has reached the right boundary, or the leftmost remaining alien has reached the left boundary, and then doing an update on all aliens to move down.

The idea of having each alien have its own state, putting the logic for controlling it in the alien class and calling that is actually good object-oriented design. It just so happens that for Space Invaders, the aliens all affect one another because they move in a block. As a result, your design results in them having too much individuality. Perhaps you can add some class that maintains state for the invader array as a whole (such as the leftmost and rightmost column still having an alien, direction of the entire block), control that in your movement loop and have it update the alien's location in turn.

Upvotes: 1

Related Questions