How to show the bullets

Hi, Can i ask some help,this is the first time that i make my simple game…it’s like the spaceship flying i am just using png file for this,I already know how to move my spaceship using keybindings, my problem is that i don’t have idea how to draw simple bullet when i pressed the forward key in the keyboard,and also i don’t know the logic how the bullet will go forward when fired up…I apologize for this silly question but i am just beginner in developing simple game…I hope someone will help me to achieve this…

I really appreciated those will help, Thank you in advance.

Hello :slight_smile:
I’m quite a beginner myself so the way I show you might not be perfect.
You should make a Bullet class that holds the bullet information, like speed, position,image etc. and then in the player/spaceship you have a list of those bullets (List bulList) that you will add a new bullet to every time you fire using the bulList.add(new Bullet(param)). Now if I were you I’d make the class something like this:



class Bullet{
    final double SPEED = 2;
    double x,y;

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

    public void move(){
        //bullet goes up
        y -= SPEED;
    }

}

Now that is assuming that you are making a game like space invaders where the bullet goes up. Also you will need to draw the elements in that list every frame. Hope that helps ;D

Hello!

Read this post:

Then read this for example on good and bad Game Loops in java:

http://www.java-gaming.org/index.php?topic=24220.0

You will want to start out with a fixed timestep loops since they make more sense :))

Hi thank you for your quick reply and giving some points,okay i will let you know if this will solve my problem. :slight_smile:

@JonJova, Thank you for this links this might help me to improve my game development skill.

I think every character in the game as an object and they all derive from the [icode]GObject[/icode] class found here in my game engine. To make a bullet, I’ll make a [icode]Bullet[/icode] class like this.


public class Bullet extends GObject
{

    public Bullet (float x, float y)
    {
        super (IMAGE_OF_BULLET, x, y);
        setVelocityY(-4);  // moves up
    }

}

And in the space ship object, I check for input and if [icode]SPACE[/icode] is pressed, I’ll fire a bullet.


public void update (long elapsedTime)
{
    if (GKeyBoard.isPressed(KeyEvent.VK_SPACE))
    {
        // Create a new bullet at the current position
        Bullet b = new Bullet(getX(), getY());
        // Add it to the map to display it and it get's the events
        Map.addObject (b);
    }
}

The function [icode]Map.addObject()[/icode] adds the bullet to the [icode]ArrayList[/icode] containing the objects and the map automatically does redrawing it every frame and checks for collisions between them.

If you like, please try my engine here. Also there’s a Space Invaders game tutorial here.

It looks like this once completed.

Hi, This is my code i am confuse how to draw the bullet image, can you please help me on this.I get trouble on this.

Thank you :slight_smile:



import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.*;
import java.util.List;


public class Mygame{
    private Image ship;
    private boolean up;
    private boolean down;
    private boolean right;
    private boolean left;
    private int posX = 300;
    private int posY = 560;
    Graphics2D g2d;

    public Mygame(){

        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Mypanel panel = new Mypanel();
        panel.setPreferredSize(new Dimension(600,600));
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);




    }

    class Mypanel extends JPanel{

        private List <Bullet> bullets;
        public Mypanel() {

            try {
                ship = ImageIO.read(getClass().getResource("/images/spaceship.png"));
            } catch (IOException exc) {
                exc.printStackTrace();
            }

            bullets = new ArrayList <Bullet>();

            bullets.add(new Bullet(posX,posY));

            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false),
                    "arrowUP");
            getActionMap().put("arrowUP",
                    arrowUP);


            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true),
                    "arrowUPrelease");
            getActionMap().put("arrowUPrelease",
                    arrowUPrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false),
                    "arrowRight");
            getActionMap().put("arrowRight",
                    arrowRight);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true),
                    "arrowRightrelease");
            getActionMap().put("arrowRightrelease",
                    arrowRightrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false),
                    "arrowLeft");
            getActionMap().put("arrowLeft",
                    arrowLeft);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true),
                    "arrowLeftrelease");
            getActionMap().put("arrowLeftrelease",
                    arrowLeftrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false),
                    "arrowDown");
            getActionMap().put("arrowDown",
                    arrowDown);

            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true),
                    "arrowDownrelease");
            getActionMap().put("arrowDownrelease",
                    arrowDownrelease);

        }


        Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = true;
                posY-=5;
                repaint();

            }
        };

        Action arrowUPrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = false;

            }
        };


        Action arrowDown = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = true;
                posY+=5;
                repaint();

            }
        };

        Action arrowDownrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = false;
            }
        };

        Action arrowRight = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = true;
                posX+=5;
                repaint();

            }
        };

        Action arrowRightrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = false;



            }
        };


        Action arrowLeft = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = true;
                posX-=5;
                repaint();

            }
        };


        Action arrowLeftrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = false;



            }
        };



        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.fillRect(0,0,600,600);
            g2d.setColor(Color.BLACK);
            g2d.drawImage(ship, posX, posY, null);

        }


    

    } //End of Class Mypanel



    class Bullet{
        final double speed = 2;
        int x,y;
         Bullet(int x, int y){
           this.x = x;
           this.y = y;

         }


        public void setX(int x){
            this.x = x;
        }


        public void setY(int y){
            this.y=y;
        }

        public int getX(){
            return x;
        }

       public int getY(){
           return y;
       }



       public void moveForward(){
           y-=speed;

       }

    }


    public static void main(String []args) {
            new Mygame();

    }
}


You should have a game loop that is calling the repaint() method every frame.

See here: http://www.java-gaming.org/topics/game-loops/24220/view.html

Okay but i have not yet draw my bullet, or how do i make my bullet color orange or red?just like my ship i draw in the paintComponent…can you please help me.I am confuse.

Read the links on game loops for a start. It’s been linked twice now.

Bad example:


class Game {
bulletsArray = new ArrayList <Bullet>();

public void gameLoop() {
   while (true) {

      update();
      render();

      // sleep
      try {
         Thread.sleep(1);
      } catch() {}
   }
}

public void update() {
   for (Bullet b : bulletsArray) {
      b.update();
   }
}

public void render(Graphics g) {
   // draw things
   for (Bullet b : bulletsArray) {
       b.draw(g);
   }
}
}
class Bullet {
   int x, y, xspeed, yspeed;
   update() {
      x += xspeed;
      y += yspeed;
   }
   draw(Graphics g) {
      g.drawImage(bulletImage, x, y, null);
   }
}

Hi i think i am confuse on this, can you please tell why is not firing the bullet,sorry… i am still learning on this game,
i hope you can help me.

Thank you in advance.


package mygame.airforce;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.*;
import java.util.List;


public class Mygame implements Runnable{
    private Image ship;
    private boolean up;
    private boolean down;
    private boolean right;
    private boolean left;
    private int posX = 300;
    private int posY = 560;
    Graphics2D g2d;
    Thread gameloop;
    Bullet bb = new Bullet();
    Mypanel p = new Mypanel();
    public Mygame(){

        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Mypanel panel = new Mypanel();

        panel.setPreferredSize(new Dimension(600,600));
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);




    }

    class Mypanel extends JPanel{

        private List <Bullet> bullets = new ArrayList <Bullet>();

        public Mypanel() {

            try {
                ship = ImageIO.read(getClass().getResource("/mygame/airforce/images/spaceship.png"));
            } catch (IOException exc) {
                exc.printStackTrace();
            }

            bullets = new ArrayList <Bullet>();

            bullets.add(new Bullet(posX,posY));

            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false),
                    "arrowUP");
            getActionMap().put("arrowUP",
                    arrowUP);


            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true),
                    "arrowUPrelease");
            getActionMap().put("arrowUPrelease",
                    arrowUPrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false),
                    "arrowRight");
            getActionMap().put("arrowRight",
                    arrowRight);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true),
                    "arrowRightrelease");
            getActionMap().put("arrowRightrelease",
                    arrowRightrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false),
                    "arrowLeft");
            getActionMap().put("arrowLeft",
                    arrowLeft);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true),
                    "arrowLeftrelease");
            getActionMap().put("arrowLeftrelease",
                    arrowLeftrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false),
                    "arrowDown");
            getActionMap().put("arrowDown",
                    arrowDown);

            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true),
                    "arrowDownrelease");
            getActionMap().put("arrowDownrelease",
                    arrowDownrelease);

        }


        Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = true;
                posY-=5;
                repaint();

            }
        };

        Action arrowUPrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = false;

            }
        };


        Action arrowDown = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = true;
                posY+=5;
                repaint();

            }
        };

        Action arrowDownrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = false;
            }
        };

        Action arrowRight = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = true;
                posX+=5;
                repaint();

            }
        };

        Action arrowRightrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = false;



            }
        };


        Action arrowLeft = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = true;
                posX-=5;
                repaint();

            }
        };


        Action arrowLeftrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = false;



            }
        };



        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.fillRect(0,0,600,600);
            g2d.setColor(Color.BLACK);
            g2d.drawImage(ship, posX, posY, null);

        }


          public void gameUpdate(){
               for(Bullet b:bullets){
                  new Bullet(getX(),getY());
               }


          }






    }


    public void start(){
        gameloop = new Thread(this);
        gameloop.start();
    }


    public void run(){
        Thread t = Thread.currentThread();

        while(t==gameloop){
            try{
                Thread.sleep(5);
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
            p.gameUpdate();
            bb.draw(g2d);
            p.repaint();
        }

    }

    class Bullet{
        final double speed = 2;
        int x,y;
        public Bullet(){}
         Bullet(int x, int y){
           this.x = x;
           this.y = y;

         }


        public void setX(int x){
            this.x = x;
        }


        public void setY(int y){
            this.y=y;
        }

        public int getX(){
            return x;
        }

       public int getY(){
           return y;
       }



       public void moveForward(){
           y-=speed;

       }

       public void draw(Graphics g){
           Graphics2D gg = (Graphics2D)g;
           gg.drawRect(0,0,1,1);
           gg.setColor(Color.orange);

       }

    }



    public static void main(String []args) {

          new Mygame();


    }
}