How can I create bullets for my game?

edited March 2014 in Using Processing

Hi there, hope your well.

Im a beginner to processing and I need to create bullets for a space game using classes. How would I go about doing this?

ArrayList bullets;

void setup() {

}

void draw() {

bullets = new ArrayList();

}

class Bullet() {

}

Thanks

Answers

  • This is a pretty broad question. What have you tried? What exactly are you confused about?

    I have an object oriented tutorial in Processing here: http://staticvoidgames.com/tutorials/objects/index.jsp

  • edited March 2014

    .

  • Did you take a look at the tutorial I posted? It shows you exactly how to create an array of Objects at a point. What specifically are you confused about?

  • Yeah is very helpful, trying to understand how I can create bullets instead of a ball using your example.

  • Would you give me a short example on how to create a bullet class?

  • edited March 2014

    That Bullet class is very complex, b/c it's meant to shoot in any direction. Hence it uses trigonometry w/ PVector.

    Now, how many directions your Ship needs to shoot to? :-/
    Take notice that a Bullet's initial coordinates are the same as the sprite which fired it! >-)

  • Well the idea is to make it shoot in all directions. Will it best to show you my code or send it to? sorry to bother you

  • If it's all directions, that's what the online example is about. Dunno how to make 1 otherwise! :-&

  • You should try something out, and post it here as an SSCCE, and we'll be glad to help you refine your approach. Especially if this is homework (but even if it's not homework), it's really important for you to understand the concepts involved, not just the syntax.

  • Hey, I managed to get my bullets working but I need it to come out from my ship Image. Instead of MouseX, MouseY.

    void keyPressed() {

    if (keyCode == ' ') {
    
    
      Bullet temp = new Bullet(mouseX, mouseY);
      bullets.add(temp);
    }    
    

    I have tried this:

      Bullet temp = new Bullet(myShip.ship.x, myShip.ship.Y);
    

    //But I get an error "cannot be resolved or is not a field"

  • Like I said, you need to take a step back and really understand what you're doing.

    Is myShip an Object of some kind? Does that Object contain a ship variable? Is the ship variable itself a variable? Does it contain both an x and a Y variable (notice the mixed case on those)?

  • edited March 2014

    I got it working, thanks for your help!

  • That doesn't really answer the questions. Does whatever Object myShip is contain a ship variable? Is that variable itself an Object? Does that object contain an x and a Y variable?

    Object oriented programming can be confusing, which is why I think you should take a step back and try to answer these questions instead of just trying different syntax and hoping for the best.

  • Answer ✓

    this is the idea

    hit space or mouse to fire

        /**
         * Multiple Bullets (v2.5)
         * by Ammon.Owed (2013/Jan)
         *
    
         from http://forum.processing.org/one/topic/shoot-multiple-bullets 
    
         **/
    
    
        final static ArrayList<Bullet> bullets = new ArrayList();  // class Bullet
        Ship player = new Ship();                              // class Player
    
        // main functions -----------------------------------------
    
    
        void setup () {
          size(600, 600);
        }
    
        void draw() {
          background(0);
          player.update();
          player.drawShip();
          player.shoot();
          // displayAim();
          handleBullets();
        }
    
        // other functions -----------------------------------------
    
        void handleBullets() {
          //for (Bullet b: bullets)  b.run(); //unremovable bullets!
          for ( int b = bullets.size(); b != 0; )
            if ( bullets.get(--b).run() )  
              bullets.remove(b);
          print(bullets.size() + "\t");
        }
    
        // ==============================
    
        class Ship {
    
          //Global Variables 
          PVector position; 
          PVector accel; 
          PVector velocity; 
          PVector rotation; 
          float drag = .9; 
    
          //Ship image 
          PImage ship = loadImage("ship.png");
    
          //How much the ship accelerates 
          float acc = 0;
    
          //rotation angle
          float a = 0;
    
          final byte bulletVel = 5;
    
          //CONSTRUCTOR  --------------------------------
          public Ship() {
            position = new PVector(330, 445);
            accel = new PVector(0, 0);
            velocity = new PVector(0, 0);
          }
    
          //FUNCTIONS -------------------------------------
          void update() {
            drawShip();
            drawFinished();
            // velocity.add(accel);
            // velocity.mult(drag);
            // position.add(velocity);
    
            acc *= 0.95;
    
            //Stops the ship when it reaches the surface
            if (position.y > height - 20 - 10 ) {
              position.y = height - 20 - 10;
            }
          }
    
          void drawShip() { 
            pushMatrix(); 
            //method to use the new pos variable 
            translate(position.x, position.y); 
            //rotate(a); 
            stroke(255, 50, 0); 
            fill(255, 50, 20);
            for ( int i=4; i >= 0; i--) {
              stroke(255, i*50, 0);
              fill(255, i*50, 20);
              ellipse( 0, 40, min(1, acc*10) *i*4, min(1, acc*10)* i*10);
            }
            //  image(ship, -ship.width/2, -ship.height/2, ship.width, ship.height);
            fill(222, 2, 2);
            //  rect ( width/2, height/2, 70, 20);
            rect ( 0, 0, 70, 20);
            popMatrix();
          }
    
          void drawFinished() {
            textAlign(CENTER);
            fill(0);
            if (position.y > height - 50 - 10 ) {
              text("you have crashed!", width/2, height/2);
            }
          }
    
          void shoot() {
            final byte bulletGap = 3;
            if ((mousePressed && frameCount % bulletGap == 0) || 
              (keyPressed && key ==' ' && frameCount % bulletGap == 0)) 
            {
              player.addBullet();
            }
          }
    
          void addBullet() {
            final PVector bulletSpd = new PVector();
            bulletSpd.set(3, 0);
            //bulletSpd.sub(position);
            bulletSpd.normalize();
            bulletSpd.mult(bulletVel);
            bullets.add( new Bullet(position, bulletSpd) );
          }
    
          //
        } // class 
    
        // ==========================================================
    
        class Bullet extends PVector {
    
          PVector vel;
          final static color bulletColor = #0000FF;
    
          Bullet(PVector loc, PVector vel) {
            super(loc.x, loc.y);
            this.vel = vel.get();
          }
    
          void display() {
            final byte bulletSize = 4;
            fill(bulletColor);
            ellipse(x, y, bulletSize, bulletSize);
          }
    
          boolean update() {
            add(vel);
            return x > width || x < 0 || y > height || y < 0;
          }
    
          boolean run() {
            display();
            return update();
          }
          //
        } // class Bullet
    
        // ==========================================================
    
This discussion has been closed.