Using 2D array of images movement?

Answers

  • The code snippet you've provided is troubling. You load the same image for each alien! Instead, you should load the image just once, and use it multiple times.

    As for making your aliens move, you're probably going to want to make each Alien be it's own object (that is, write a class Alien). The movement direction can be global and shared between all aliens, and they can change direction (and move down) when any one of them bumps into the edge.

    Post a working example of your game code for more help. Something that we can copy, paste, and run. The snippet you've shown us so far can't be executed as-is.

  • edited December 2015

    I have a total of 4 tabs This is my current code i'll link the images

    first tab:

    PImage bg;
    PImage ship_move;
    PImage ship;
    int shipWidth = 400;
    int shipHeight = 550;
    int laserY = 550;
    int laserX = 400;
    boolean laserVis = false;
    PImage laser;
    boolean [] controls;
    
    
    void setup() {
      size(800, 600);
      bg = loadImage("sky.png");
      ship_move = loadImage("ship_move.png");
      ship = loadImage("ship.png");
      laser = loadImage("laser.png");
    
      controls = new boolean[3];//This sets whether the controls array
      controls[0]=false;//'a' is pressed this allow for other keys to be pressed at the same time
      controls[1]=false;//'d' " "
      controls[2]=false;//' ' " "
    }
    
     void draw() {
      background(bg);
      frameRate(10);
      twoDarrays();
    
      laserDraw();
      shipDraw();
      shipOnScreen();
    
      if (controls[0]){ //when the array is false it will allow for multiple keyPresses
      shipWidth = shipWidth - 10;
      }
      if (controls[1]){
      shipWidth = shipWidth + 10;
      }
    
    }
    
    void keyPressed() {
      shipMove();
    
      if (key == ' ' && !laserVis) {
        laserX = shipWidth+10;
        laserY = shipHeight;
        laserVis = true;
        controls[2]=true;//when this is true it will allow to do multiple keypresses at the sametime
    
    
      }
    }
    
    void keyReleased(){
    shipMoveRelease();
    }
    

    second tab:

    void twoDarrays() {
    
      PImage[] [] aliens  = new PImage [20][5];
      PImage army;
      //c = column
      for (int c=0; c<15; c++) {
        for (int i=0; i<aliens[c].length; i++) {
          army = aliens[c][i] = loadImage("enermyShip.png") ;
            for (int m=0; m<c%2; m++) {
            image (army, 85+(c*45), (85+(i*45)));
    
          }
        }
      }
    
    }
    

    third tab:

    void laserDraw() {
      if (laserVis){ 
      fill(255, 0, 0);
      noStroke();
      //rect(laserX, laserY, 3, 20);
      laserY = laserY - 20;
      image(laser, laserX, laserY);
    
      }
      if(laserY <= -20){
    
      laserVis = false;
      }
    }
    

    fourth tab:

    void shipDraw() {
      image(ship, shipWidth, 550);
    }
    
    void shipMove() {
      if (key == 'a' || keyCode == LEFT) { 
    
        controls[0]=true;
    
      }
      if (key == 'd' || keyCode == RIGHT) {
    
        controls[1]=true;
      }
    }
    void shipMoveRelease(){
      if (key =='a'){
      controls[0]=false;
      }
      if (key =='d'){
      controls[1]=false;
      }
      if (key == ' '){
      controls[2]=false;
      }
    }
    
    void shipOnScreen() {
      if (shipWidth<=20) {
        shipWidth = 20; 
      }
      if (shipWidth>=750) {
        shipWidth = 750;
      }
    
    }
    

    enermyShip

    laser

    ship

    sky

  • Have you made any attempts at only loading the enemy ship once, or using classes?

  • @blugamma do not delete your comments once a reply has been made. Thus is against forum etiquette.

  • I have a total of 4 tabs This is my current code i'll link the images

    first tab:

    PImage bg;
    PImage ship_move;
    PImage ship;
    int shipWidth = 400;
    int shipHeight = 550;
    int laserY = 550;
    int laserX = 400;
    boolean laserVis = false;
    PImage laser;
    boolean [] controls;
    
    
    void setup() {
      size(800, 600);
      bg = loadImage("sky.png");
      ship_move = loadImage("ship_move.png");
      ship = loadImage("ship.png");
      laser = loadImage("laser.png");
    
      controls = new boolean[3];//This sets whether the controls array
      controls[0]=false;//'a' is pressed this allow for other keys to be pressed at the same time
      controls[1]=false;//'d' " "
      controls[2]=false;//' ' " "
    }
    
     void draw() {
      background(bg);
      frameRate(10);
      twoDarrays();
    
      laserDraw();
      shipDraw();
      shipOnScreen();
    
      if (controls[0]){ //when the array is false it will allow for multiple keyPresses
      shipWidth = shipWidth - 10;
      }
      if (controls[1]){
      shipWidth = shipWidth + 10;
      }
    
    }
    
    void keyPressed() {
      shipMove();
    
      if (key == ' ' && !laserVis) {
        laserX = shipWidth+10;
        laserY = shipHeight;
        laserVis = true;
        controls[2]=true;//when this is true it will allow to do multiple keypresses at the sametime
    
    
      }
    }
    
    void keyReleased(){
    shipMoveRelease();
    }
    

    second tab:

    void twoDarrays() {
    
      PImage[] [] aliens  = new PImage [20][5];
      PImage army;
      //c = column
      for (int c=0; c<15; c++) {
        for (int i=0; i<aliens[c].length; i++) {
          army = aliens[c][i] = loadImage("enermyShip.png") ;
            for (int m=0; m<c%2; m++) {
            image (army, 85+(c*45), (85+(i*45)));
    
          }
        }
      }
    
    }
    

    third tab:

    void laserDraw() {
      if (laserVis){ 
      fill(255, 0, 0);
      noStroke();
      //rect(laserX, laserY, 3, 20);
      laserY = laserY - 20;
      image(laser, laserX, laserY);
    
      }
      if(laserY <= -20){
    
      laserVis = false;
      }
    }
    

    fourth tab:

    void shipDraw() {
      image(ship, shipWidth, 550);
    }
    
    void shipMove() {
      if (key == 'a' || keyCode == LEFT) { 
    
        controls[0]=true;
    
      }
      if (key == 'd' || keyCode == RIGHT) {
    
        controls[1]=true;
      }
    }
    void shipMoveRelease(){
      if (key =='a'){
      controls[0]=false;
      }
      if (key =='d'){
      controls[1]=false;
      }
      if (key == ' '){
      controls[2]=false;
      }
    }
    
    void shipOnScreen() {
      if (shipWidth<=20) {
        shipWidth = 20; 
      }
      if (shipWidth>=750) {
        shipWidth = 750;
      }
    
    }
    

    enermyShip

    laser

    ship

    sky

This discussion has been closed.