Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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 shoot bullets ??  (Read 1117 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-15 21:54:06 »

Hello everyone
am trying to make the player able to shoot bullets, and i thought using arrayList is the best way to do that (please correct me if am wrong)
so here is what i did
after initializing everything,
i check if the "shoot" boolean is true,
then variable called "numBullet" increment by 1
then a for loop go from 0 to the "numBullet" that draw and move all the bullets in the ArrayList
the problem is :
i only able to shoot one time (i press space and a number of bullets get created, i press again, nothing happens)
the speed is not right (every time a new bullet is created the speed increase)
wrong positions for the new bullets

i don't think that there is any need to post the code cause am looking for a theory or a solution more than a fix for my code

thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline UprightPath
« Reply #1 - Posted 2013-04-15 21:57:59 »

Using the pastebin (Linked beneath the text box) please show us the code you're using for this? Because it sounds like there are multiple problems going on here.

Offline alaslipknot
« Reply #2 - Posted 2013-04-15 22:05:20 »

Codes :
(this is my first OOP program so don't judge the mess you're gonna see  Roll Eyes )

Control.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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Control implements KeyListener{
   public static boolean right,left,shoot;
     
   @Override
   public void keyPressed(KeyEvent e) {
     
      //moving the player
      if(e.getKeyCode()==e.VK_RIGHT){
         
         right=true;
      }else if(e.getKeyCode()==e.VK_LEFT){
         
         left=true;
      }
     
      //shooting bullets
      if(e.getKeyCode()==e.VK_SPACE){
         shoot=true;
      }
     
   }

   @Override
   public void keyReleased(KeyEvent e) {
      if(e.getKeyCode()==e.VK_RIGHT){
         
         right=false;
      }else if(e.getKeyCode()==e.VK_LEFT){
     
         left=false;
      }
      //shooting bullets
      if(e.getKeyCode()==e.VK_SPACE){
         shoot=false;
      }  
           
   }

   @Override
   public void keyTyped(KeyEvent e) {
     
   }

}


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  
import java.awt.Graphics;
import java.awt.Rectangle;

public class Bullet extends Rectangle {
   public static int speed = 1,
         x = Player.x + Player.width / 2,
         y = Player.y;

   public Bullet(int x, int y, int width, int height) {

      x = this.x;
      y = this.y;
      this.setBounds(x, y, width, height);

   }

   public void draw(Graphics g) {
      g.drawRect(x, y, width, height);
   }

   public void move(int speed) {

      speed = this.speed;
      this.y -= speed;
   }
}


Screen.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  
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  
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable {
   Thread thread = new Thread(this);

   public static Ball ball;
   public static Player player;
   public static Control control;
   public static Bullet bullet;
   public static ArrayList<Bullet> bulletArray;

   public static boolean isFirst = true;

   public static int screenWidth, screenHeight, screenX, screenY, numBullet;

   public Screen() {
      define();
      thread.start();
      this.addKeyListener(control);
      this.setFocusable(true);
   }

   public void define() {
      control = new Control();
      ball = new Ball(ball.x, ball.y, ball.height, ball.width);
      player = new Player(player.x, player.y, player.width, player.height,
            player.speed);

      screenWidth = this.getWidth();
      screenHeight = this.getHeight();

      bulletArray = new ArrayList<Bullet>();

      bullet = new Bullet(0, 0, 50, 50);

   }

   public void paintComponent(Graphics g) {

      if (isFirst) {
         define();
      }
      // drawing the background
      g.setColor(new Color(0, 0, 0));
      g.fillRect(screenX, 0, screenWidth, screenHeight);

      // drawing the ball
      g.setColor(new Color(250, 50, 0));
      ball.move(ball.x, ball.y);
      ball.draw(g);

      // player
      g.setColor(new Color(255, 255, 255));
      player.draw(g);
      player.move();

      // creating bullets
      if (control.shoot) {
         numBullet++;
      }

      for (int i = 0; i < numBullet; i++) {
         bullet = new Bullet(player.x, bullet.y, 5, 5);
         bulletArray.add(bullet);

         bulletArray.get(i).draw(g);
         bulletArray.get(i).move(bullet.speed);
      }

   }

   public void run() {

      while (true) {

         if (!isFirst) {

         }

         repaint();

         try {

            Thread.sleep(5);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

}



i don't think you need the other classes

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-04-15 22:26:30 »

You really need to fix up your game in general, before you try debugging anything else.

1. You need a game loop (read this) instead of just stuffing everything in paintComponent() and calling repaint().

2. Why are your bullet x/y ints static? It makes sense to have your speed static if you aren't doing any acceleration, but different bullets will have different x/y coordinates. Also, is it really necessary to have that much stuff static in your Screen and Control classes?

3.
1  
bullet = new Bullet(player.x, bullet.y, 5, 5);
should not be in your loop, because then you are creating/resetting every bullet in your ArrayList every time through the loop. Put it in the constructor, or something.
Offline alaslipknot
« Reply #4 - Posted 2013-04-15 22:43:16 »

i knew it  Undecided
those codes are worthless  Cry


"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2013-04-15 22:44:54 »

i knew it  Undecided
those codes are worthless  Cry


No, just needs to be improved. A good base of code will make future development 100% easier.
Offline alaslipknot
« Reply #6 - Posted 2013-04-15 22:48:59 »

No, just needs to be improved. A good base of code will make future development 100% easier.

yes but i don't know how to reach that level ...
i start learning Java 2 months ago and still can't do anything 100% by myself  Undecided

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline ReBirth
« Reply #7 - Posted 2013-04-16 02:16:25 »

Also don't forget to remove the bullet after it finishes its job, going out off screen or explode.

Offline SHC
« Reply #8 - Posted 2013-04-16 07:31:39 »

@alaslipknot

I think you need a tutorial to get started. Here's a list of tutorials from this site.


These should get you started well. I also recommend to see the Zetcode Java2D Games Tutorial

Offline alaslipknot
« Reply #9 - Posted 2013-04-16 10:21:03 »

@SHC

i started with the zetCode tutorials but then i thought that video tutorials might be better, easier and faster to learn, but i seems i was wrong  Tongue
it's the same mistake i made when i started to learn ActionScript  Roll Eyes
anyway ...
thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!