Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to show the bullets  (Read 898 times)
0 Members and 1 Guest are viewing this topic.
Offline Xuang Zang

Senior Newbie





« Posted 2013-06-16 18:16:17 »

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.
Offline Saiqal

Senior Newbie





« Reply #1 - Posted 2013-06-16 19:01:50 »

Hello  Smiley
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<Bullet> 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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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  Grin
Offline jonjava
« Reply #2 - Posted 2013-06-16 19:19:05 »

Hello!

Read this post:

http://www.java-gaming.org/topics/hello/24411/msg/205121/view.html#msg205121

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 :))

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xuang Zang

Senior Newbie





« Reply #3 - Posted 2013-06-16 19:30:26 »

Hello  Smiley
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<Bullet> 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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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  Grin

Hi thank you for your quick reply and giving some points,okay i will let you know if this will solve my problem. Smiley
Offline Xuang Zang

Senior Newbie





« Reply #4 - Posted 2013-06-16 19:32:13 »

@JonJova, Thank you for this links this might help me to improve my game development skill.
Offline SHC
« Reply #5 - Posted 2013-06-18 05:42:03 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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
SPACE
is pressed, I'll fire a bullet.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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
Map.addObject()
adds the bullet to the
ArrayList
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.


Offline Xuang Zang

Senior Newbie





« Reply #6 - Posted 2013-06-18 16:02:23 »

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 Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
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();

    }
}
Offline Longarmx
« Reply #7 - Posted 2013-06-18 17:02:16 »

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

Offline Xuang Zang

Senior Newbie





« Reply #8 - Posted 2013-06-18 20:41:30 »

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.
Offline jonjava
« Reply #9 - Posted 2013-06-18 21:40:43 »

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

Bad example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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);
   }
}
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
class Bullet {
   int x, y, xspeed, yspeed;
   update() {
      x += xspeed;
      y += yspeed;
   }
   draw(Graphics g) {
      g.drawImage(bulletImage, x, y, null);
   }
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xuang Zang

Senior Newbie





« Reply #10 - Posted 2013-06-19 16:17:24 »

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

Bad example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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);
   }
}
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
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();


    }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!