Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  multiple shots  (Read 369 times)
0 Members and 1 Guest are viewing this topic.
Offline Samdy

Junior Newbie





« Posted 2013-11-07 16:17:07 »

hi, i'm new to game programming in java, and i made game like space invader before, but now, i'm trying to remake game of Raiden(which is more difficult) and encountered problems like how to do multiple shots.

mainPanel or Board class:
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  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class mainPanel extends JPanel implements Runnable, KeyListener {
   
   public final int NO_MOVE = 0;
   public final int LEFT = 1;
   public final int RIGHT = 2;
   public final int UP = 3;
   public final int DOWN = 4;
   public final int SPEED = 3;
   
   private int[] keys = new int[5];
   
   ImageIcon i = new ImageIcon(getClass().getResource("res/map1.png"));
   Image BG = i.getImage();
   
   ImageIcon p = new ImageIcon(getClass().getResource("res/plane1.gif"));
   Image player = p.getImage();
   
   
   background map = new background(0, 2400-640);
   plane p1;
   
   boolean ingame = true;
   
   public mainPanel(){
     
      p1 = new plane(225, 600);
     
      for(int x = 0;x<5;x++)
         keys[x] = 0;
     
      setFocusable(true);
      addKeyListener(this);
     
      Thread thread = new Thread(this);
      thread.start();
   }
   
   public void paintComponent(Graphics g){
      super.paintComponent(g);
      map.draw(g, BG);
      p1.draw(g, player);
   }
     
   public void keyUpdate() {
       if(keys[LEFT]>0){
           p1.setDX(p1.getDX()-(1*SPEED));
       }

       if(keys[RIGHT]>0){
          p1.setDX(p1.getDX()+(1*SPEED));
       }

       if(keys[UP]>0){
          p1.setDY(p1.getDY()-(1*SPEED));
       }

       if(keys[DOWN]>0){
          p1.setDY(p1.getDY()+(1*SPEED));
       }
   }

   @Override
   public void run() {
     
      while(ingame){
         p1.update();
         keyUpdate();
         map.update();
         repaint();
         try {
            Thread.sleep(20);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

   @Override
   public void keyTyped(KeyEvent arg0) {
      // TODO Auto-generated method stub
     
   }
   
   public void keyPressed(KeyEvent e){
      if (e.getKeyCode() == KeyEvent.VK_LEFT){
         keys[LEFT]++;
      } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         keys[RIGHT]++;
      } else if (e.getKeyCode() == KeyEvent.VK_UP) {
         keys[UP]++;
      } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
         keys[DOWN]++;
      }
      if(e.getKeyCode() == KeyEvent.VK_SPACE){
         System.out.println("SPACE");
         p1.shot();
      }
         
   }
   
   public void keyReleased(KeyEvent e){
      if (e.getKeyCode() == KeyEvent.VK_LEFT){
         keys[LEFT]=0;
      } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         keys[RIGHT]=0;
      } else if (e.getKeyCode() == KeyEvent.VK_UP) {
         keys[UP]=0;
      } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
         keys[DOWN]=0;
      }
      if(e.getKeyCode() == KeyEvent.VK_SPACE){
      }
   }
   
}


plane class
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  
import java.awt.Graphics;
import java.awt.Image;



public class plane {
   
   private final int MAX_SHOT = 20;
   
   private int dX;
   private int dY;
   private int sX=5;
   private int sY=7;
   private int count=-1;
   bullet shot1= new bullet();
   bullet shot2[];
   
   private boolean isAlive = true;
   
   public plane(int x, int y){
      dX = x;
      dY = y;
      isAlive = true;
     
      shot2 = new bullet[MAX_SHOT];
      for(int i=0; i<MAX_SHOT;i++)
         shot2[i] = new bullet();
   }
   
   public void setDX(int x){dX = x;}
   public void setDY(int y){dY = y;}
   public void setSX(int x){sX = x;}
   public void setSY(int y){sY = y;}
   public int getDX(){return dX;}
   public int getDY(){return dY;}
   public int getSX(){return sX;}
   public int getSY(){return sY;}
   
   public void setAlive(boolean t){
      isAlive = t;
   }
   public boolean getAlive(){
      return isAlive;
   }
   
   public void setShot(){
      for(int j=0;j<MAX_SHOT;j++){
         if(shot2[j].getDY() < 0){
            shot2[j].setShot(false);
         }
      }
      for(int i=0;i<MAX_SHOT;i++){
         if(shot2[i].isShot() == false){
            shot2[i].setDX(dX+20);
            shot2[i].setDY(dY+20);
         }
      }
   }
   
   public void shot(){
      count++;
   }
   
   public void update(){
     
      if(count > MAX_SHOT)
         count=-1;
     
      if(count>=0 && count <MAX_SHOT){
         if(shot2[count].isShot() == false)
            shot2[count].setShot(true);
      }
      for(int i = 0; i<MAX_SHOT;i++)
         shot2[i].update();
      setShot();
   }
   
   public void draw(Graphics g, Image img){
      if(isAlive){
         g.drawImage(img, dX, dY, dX+40, dY+40, sX, sY, sX+50, sY+42, null);
         for(int i=0; i<MAX_SHOT;i++)
            shot2[i].draw(g);
      }
   }
   

}


bullet class
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  
import java.awt.Color;
import java.awt.Graphics;


public class bullet {
   
   private int dX;
   private int dY;
   private int sX;
   private int sY;
   
   private boolean isShot = false;
   public boolean isShot() {return isShot;}
   public void setShot(boolean isShot) {this.isShot = isShot;}

   private int speed=4;
   public void setSpeed(int s){speed = s;}
   
   public void setDX(int x){dX = x;}
   public void setDY(int y){dY = y;}
   public void setSX(int x){sX = x;}
   public void setSY(int y){sY = y;}
   public int getDX(){return dX;}
   public int getDY(){return dY;}
   public int getSX(){return sX;}
   public int getSY(){return sY;}
   
   public void update(){
      if(isShot == true)
         dY-=speed;
   }
   
   public void draw(Graphics g){
      if(isShot == true)
         g.setColor(Color.WHITE);
         g.fillRect(dX, dY, 2, 10);
   }

}
Offline lcass
« Reply #1 - Posted 2013-11-07 16:32:29 »

What do you mean multiple shots like firing multiple bullets? what is the issue when firing is it that the x and y reset?
Offline Samdy

Junior Newbie





« Reply #2 - Posted 2013-11-07 16:34:22 »

What do you mean multiple shots like firing multiple bullets? what is the issue when firing is it that the x and y reset?

yeah, multiple bullets, the issue is my bullet is staggering and in the plane class, since counter always >= 0, the bullet always loop once
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ricardo

Senior Member


Medals: 2
Projects: 3



« Reply #3 - Posted 2013-11-07 17:47:00 »

EDIT: Sorry I didn't see all your code there before writing this. I was reading other posts as well.
But you can still use some logic from this code.
Create a List of bullets. Easy to check if you have bullets to update and add or remove bullets.
Also you should pass x, y, direction, etc in the bullet constructor and let bullet class do all the calculations for you. Just update bullet and draw in your main class.
Get x and y to check if you hit some enemy or wall or bullet is off screen to remove them.
That said, I hope this can help:


Create a bullet class and call it bullet.java.

bullet.java:
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  
public class Bullet {
   // add your fields here. bullet position, bullet speed, bullet direction, etc
  int x = 0;
   int y = 0;
   // ...
 
   // constructor
  public Bullet(double angle, int x, int y, int direction_x, int direction_y) {  // every time you create a new bullet, give this fields: angle, real x, real y, direction
     this.x = x;
      this.y = y;  // save all your fields here. x, y, direction, etc.
     // ...
  }
   
   // functions go retrieve position here. to check if you hit something in your game loop.
  public int getx() { return x; }
   public int gety() { return y; }
   
   public void update() {   // call this function on your main loop. this will update bullet position

      // calculate direction here. use speed, angle, etc...

      x += direction_x;  // calculate new position
     y += direction_y;
   }
   public void draw(Graphics2D g) {   // call this on your game loop, after you draw all stuff (background, player, etc)
     // draw your bullet here. note I call grapics2d. I don't know how you draw your stuff. use graphics or whatever.
  }
}



Every time you shoot just create new bullet.

your game.java:
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  
private static List<Bullet> bullets = new ArrayList<Bullet>();   // call this before your loop.

// inside game loop:

// add bullet:
bullets.add(new Bullet(angle, x, y, direction_x, direction_y));

// update bullets
for (int i = 0; i < bullets.size(); i++) {   // check if you have bullets to update
  bullets.get(i).update();


   // use bullets.get(i).getx and gety to calculate if bullet hit something and you need to remove bullet:
  if (you_need_to_remove_your_bullet) {
      bullets.remove(i);
      i--;
   }

}


// draw bullets
for (int i = 0; i < bullets.size(); i++) {
   bullets.get(i).draw(g);
}
Offline opiop65

JGO Kernel


Medals: 130
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #4 - Posted 2013-11-07 22:49:29 »

What do you mean by staggering? That honestly makes no sense. Do you mean graphically? What specifically are you having issues with? I don't understand why throwing a wall of code at someone will get you results. Ask specific questions and explain.

@Icass I would recommend checking if the bullet needs to removed first before you update it so other bullets can't collide with the bullet that needs removing. Plus, going through an update method is more expensive than just doing a Boolean test to see if the bullet needs removing or not. Just a suggestion!

Pages: [1]
  ignore  |  Print  
 
 

 

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 (81 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (223 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!