Game - shoot action dillema

Hello, having a difficult time arranging some code for a simple platform game...

What I have so far -

if shoot, then image(shootBullet), move bullet in direction depending on characters resting state.

What I'm trying to do -

if bullet>width || bullet<0, delete bullet. if bulletX == enemyX, delete enemy. shoot multiple bullets??? Lost here...

Problems I'm running into...

Have tried adapting the OpenProcessing SpaceInvaders concept of ArrayList for shooting but no avail... (http://www.openprocessing.org/sketch/57710) So I set my image(shootBullet) as a PImage for-loop array (like for my character and enemy), now I have confusions about PImage and arrays, not being able to .get or .delete the image(shootBullet). Can I somehow get the bullet to act in an array so I can shoot multiple bullets? Any help appreciated!!

CODE FOR GAMEPLAY

Player player;
Bullet bullet;
Enemy enemy;

//ArrayList bullets = new ArrayList();
void setup() {
  size(500, 360);
  frameRate(12); 
  player = new Player(); //initialize Player
  bullet = new Bullet();
  enemy = new Enemy();
  // Loads all of the images before the game starts so that
  // loading images during run-time doesnt slow the game down
  player.loadImages();
  bullet.loadImages();
  enemy.loadImages();
}

void draw() {
//println("bullet.bulletX ="+bullet.bulletX+"         enemy.enemyX ="+enemy.enemyX);
  println(bullet.canShoot);
  background(200);
  player.draw();
  bullet.draw();
  enemy.draw();
  bullet.startTimer();
  bullet.shootSignal();
  player.checkKeys();
  player.checkState();
  //  player.update();
}

CODE FOR PLAYER CLASS

class Player
{
  float playerX = 60; //X-coordinates of player
  float playerY = 280; //Y-coordinates of player
  String dirState = "chill"; //states: chill,left,right
  String chillState = "right"; //states: left,right
  String jumpState = "none"; //states:none,jumping,falling
  int maxImagesWalkR = 9; //Total # of images loaded
  int imageIndexWalkR = 0; //Initial image loaded
  int maxImagesWalkL = 9;
  int imageIndexWalkL = 0; 
  int direction = 1;
  PImage[] DemocratWalkR = new PImage[maxImagesWalkR];
  PImage[] DemocratWalkL = new PImage[maxImagesWalkL];

  void loadImages() { //Load all images for the sequences
    // Load all images running right
    for (int i = 0; i < DemocratWalkR.length; i++) {
      DemocratWalkR[i] = loadImage("WomanR_Walk" + i + ".gif");
    } 
    // Load all images running left
    for (int j = 0; j < DemocratWalkL.length; j++) {
      DemocratWalkL[j] = loadImage("WomanL_Walk" + j + ".gif");
    }
  }

  void draw() {  
    if (dirState == "chill") {
      if (chillState == "right") {
        image(DemocratWalkR[imageIndexWalkR], playerX, playerY);
      }
      if (chillState == "left") {
        image(DemocratWalkL[imageIndexWalkL], playerX, playerY);
      }
    }    
    if (dirState == "right") {
      image(DemocratWalkR[imageIndexWalkR], playerX, playerY);
    } else if (dirState == "left") {
      image(DemocratWalkL[imageIndexWalkL], playerX, playerY);
    }

    if (jumpState == "jump" || jumpState == "falling")
    { 
      if (chillState == "right") {
        background(220);
        image(DemocratWalkR[1], playerX, playerY);
      } else if (chillState == "left") {
        background(220);
        image(DemocratWalkL[1], playerX, playerY);
      }
      dirState="chill";
    }
  }


  void runRight()
  {
    dirState = "right";
    if (imageIndexWalkR <= 8) {
      imageIndexWalkR += 1;
      playerX+=10;
      if (playerX > width) {
        playerX = -0;
      }
    }
    if (imageIndexWalkR > 8) {
      imageIndexWalkR = 0;
    }
  }

  void runLeft()
  {
    dirState = "left";
    if (imageIndexWalkL <= 8) {
      imageIndexWalkL += 1;
      playerX-=10;
      if (playerX < -70) {
        playerX = width;
      }
    }
    if (imageIndexWalkL > 8) {
      imageIndexWalkL =0 ;
    }
  }

  void chill() {
    if (chillState == "chill") {
      playerX -=0;
    }
  }
  void jumping() {
    if (jumpState == "jump") {
      playerY -= 10;
      if (playerY <= 220) { 
        jumpState = "falling";
      }
    }
  }

  void falling() {
    if (jumpState == "falling") {
      playerY += 10; 
      if (playerY >= 280) {
        jumpState = "none";
      }
    }
  }

  void checkState() {
    if (dirState == "right") {
      runRight();
      chillState = "right";
    } 
    if (dirState == "left") {
      runLeft();
      chillState = "left";
    }        
    if (jumpState == "jump") {
      jumping();
    } else if (jumpState == "falling") {
      falling();
    }
    if (bullet.shootState == "shoot") {
      bullet.shooting();
    }
    dirState = "chill";
    chill();
  }


  void checkKeys() 
  {
    if (keyPressed && keyCode == RIGHT)player.dirState = "right";
    if (keyPressed && keyCode == LEFT)player.dirState ="left";
    if (keyPressed && keyCode == UP && player.jumpState == "none") 
    {
      player.jumpState = "jump";
    }

    if (keyPressed && keyCode == DOWN && bullet.canShoot) {
      println("SHOOTING...*FIRE* " );
      bullet.bulletX= player.playerX;
      bullet.shootState = "shoot";
      bullet.startTimer();
      bullet.shootdelay ++;
    } else {
      //    bullet.bulletX = playerX;
    }
  }
}

CODE FOR ENEMY CLASS

class Enemy
{

  ArrayList enemies = new ArrayList();
  float enemyX = 400; //X-coordinates of player
  float enemyY = 280; //Y-coordinates of player
  int maxImagesEnemyR = 9; //Total # of images loaded
  int imageIndexEnemyR = 0; //Initial image loaded
  int maxImagesEnemyL = 9;
  int imageIndexEnemyL = 0; 
  //  int direction = 1;
  int pixelsize = 4;
  int gridsize  = pixelsize * 7 + 5;

  PImage[] EnemyWalkR = new PImage[maxImagesEnemyR];
  PImage[] EnemyWalkL = new PImage[maxImagesEnemyL];

  void loadImages() { //Load all images for the sequences
    // Load all images running right
    for (int i = 0; i < EnemyWalkR.length; i++) {
      EnemyWalkR[i] = loadImage("EnemyR_Walk" + i + ".gif");
    } 
    // Load all images running left
    for (int j = 0; j < EnemyWalkL.length; j++) {
      EnemyWalkL[j] = loadImage("EnemyL_Walk" + j + ".gif");
    }
  }

  void setup() {
    createEnemies();
  }

  void draw() {
    for (int i = 0; i < EnemyWalkL.length; i++) {
      //      Enemy enemy = (Enemy) Enemy.get(i);
      // if (outside() == true) {
      //   break;
      // }
      if (alive() == false) {
        //      EnemyWalkL.remove(imageIndexEnemyL);
       // background(100);
        break;
      } else if (alive() == true) {
        createEnemies();
        break;
      }
    }
  }

  void createEnemies() {

    image(EnemyWalkL[imageIndexEnemyL], enemyX, enemyY);
    {
      if (imageIndexEnemyL <= 8) {
        imageIndexEnemyL += 1;
        enemyX-=20;
        if (enemyX < -70) {
          enemyX = width;
        }
      }
      if (imageIndexEnemyL > 8) {
        imageIndexEnemyL =0 ;
      }
    }
    //  for (int i = 0; i < width/gridsize/2; i++) {
    //    for (int j = 0; j <= 5; j++) {
    //     image(EnemyWalkL[imageIndexEnemyL], i*gridsize, j*gridsize);
    // EnemyWalkL.add(new Enemy(i*gridsize, j*gridsize));
  }
  //}
  //}

  boolean alive() {
    // for (int i = 0; i < bullet.shootBullet.length; i++) {
    //Bullet bullet = bullet.shootBullet.get(imageIndexEnemyL);
    // bullet.shootBullet[i] = loadImage("FireballBullet" + i + ".gif");
    //if (bullet.bulletX == enemyX  ) {
//    if (bullet.bulletX == enemyX) {
      // bullet.bulletState = "false";
      //bullet.shootBullet.remove(imageIndexEnemyL);
      return true;

  }
  boolean outside() {
    if (enemyX < 0 || enemyX > width) {
      createEnemies();
      return true;
    } else {
      return false;
    }
  }
}

CODE FOR BULLET CLASS

class Bullet
{
  float bulletX = player.playerX;                                                // bullet coordinate X = player coordinate X
  float bulletY = player.playerY;
  String bulletState = "false"; 
  String shootState = "none"; 
  boolean canShoot = true;
  int shootdelay = 0;
  int maxImagesBullet = 4;                                                       //# of images in bullet array
  int imageIndexBullet = 0;                                                       //Starting image for bullet array
  int savedTime;
  int totalTime = 3000;
  PImage[] shootBullet = new PImage[maxImagesBullet];           //Setting array for action shootBullet
  //ArrayList shootBullet = new ArrayList();                                //From SpaceInvaders example...
  //ArrayList<Bullet> shootBullet = new ArrayList<Bullet>();     //couldn't adapt, left marked out.
  void loadImages()                                                                     // Load bullet images
  { 
    for (int i = 0; i < shootBullet.length; i++) 
    {
      shootBullet[i] = loadImage("FireballBullet" + i + ".gif");         // Images inside sketchfolder
    }
  }
  void startTimer()
  {
    int passedTime = millis() - savedTime;                                  //state timer
    if (passedTime > totalTime) {                                                //If timer expires
      println("Ready to Fire... Press DOWN");
      background(255, 100, 0);                                                   // Color a new background
      shootState = "none";                                                           //stop shooting
      bulletX = player.playerX;                                                     //resets bullet coordinate to players coordinate
      savedTime = millis();                                                           // Save the current time, restart the timer
      canShoot = true;                                                                  //able to shoot 
    }
  }
  void draw()
  {
    if (shootState == "shoot")
    { 
      if (player.chillState == "right" || player.dirState == "right") {
        image(shootBullet[imageIndexBullet], bulletX, bulletY);     //initialize shootBullet image
        bulletX +=10;                                                                   //Moves bullet right!
      }
      if (player.chillState == "left" || player.dirState == "left") {
        image(shootBullet[imageIndexBullet], bulletX, bulletY);     //initialize shootBullet image
        bulletX -=10;                                                                   //Moves bullet left!
      }
      if (bulletX >=width || bulletX <= 0) {                                  // if bullet is out of screen area...
        shootState = "none";                                                         //stop shooting
      bulletX = player.playerX;                                                     //resets bullet coordinate to players coordinate
      }
    }
  }
  void shooting() {
    if (imageIndexBullet <= 3) {                                                   // play bullet animation
      imageIndexBullet += 1;
    }
    if (imageIndexBullet > 3) {                                                     //reset bullet animation 
      imageIndexBullet = 0;
    }
    canShoot = false;                                                                  //not able to shoot
  }
}

Is there a way to post my sketchbook folder, or the other classes in full?

Thank you! - the sheep

Tagged:
Sign In or Register to comment.