Search code examples
javamove

HOw to move my bullet slowly in java?


Hi I am developing a game that a fighter moves right and left and shoots. For the shooting part, I tried to use a for loop to slow the speed down and user can see the bullet. But it wasn't enough. I used sleep too but not a good answer. Now I have no idea what to do. Here is my paintComponent calss:

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;



public class PaintComponent extends JPanel implements KeyListener {
    int dx = 200-15;
    int dy = 450;
    int my = 450;

    ArrayList<Bullet> bullets = new ArrayList<>();
    public Rectangle2D rec =new Rectangle2D.Double(dx , dy, 30, 10);
    Rectangle2D recB = new Rectangle2D.Double(dx+13 , my, 6, 6);

//    public Polygon pol = new Polygon
    private BufferedImage imageBg, imageFi, imageBu;


    public PaintComponent() {
        this.addKeyListener(this);
        this.setFocusable(true);
        this.setBackground(Color.white);

        try {                
          imageBg = ImageIO.read(new File("C:\\Java\\NetbeansProjects\\Game\\bg.jpg"));
          imageBu = ImageIO.read(new File("C:\\Java\\NetbeansProjects\\Game\\bul.png"));
          imageFi = ImageIO.read(new File("C:\\Java\\NetbeansProjects\\Game\\fi.png"));
       } catch (IOException ex) {
            System.out.println("No background image is available!");
       }
    }

    public void shoot(){
        if(bullets != null){
            for(int i=0; i<bullets.size(); i++){
                for(int j=0; j<200; j++){
                    bullets.get(i).setdy(my-j);
                }
                System.out.println(bullets.get(i).getdy());
            }
        }
    }
    public void moveRec(KeyEvent evt){
        switch(evt.getKeyCode()){
            case KeyEvent.VK_LEFT:
            dx -= 5;
            rec.setRect(dx, dy, 30, 10);
            recB.setRect(dx+13, dy, 6, 6);
            repaint();
            break;
        case KeyEvent.VK_RIGHT:
            dx += 5;
            rec.setRect(dx, dy, 30, 10);
            recB.setRect(dx+13, dy, 6, 6);
            repaint();
            break;
        case KeyEvent.VK_SPACE:
            Bullet b = new Bullet(dx, dy);
            bullets.add(b);
            shoot();
            break;

    }
}

        @Override
        public void paintComponent(Graphics grphcs)
            {super.paintComponent(grphcs);
            Graphics2D gr = (Graphics2D) grphcs;
                int X = (int) rec.getCenterX();
                int Y = (int) rec.getCenterY();
                gr.drawImage(imageBg, 0, 0, null);
                gr.drawImage(imageFi, X-50, Y-75, null);
                gr.setColor(Color.GRAY);
                if(bullets != null){
                    for(int i=0;i<bullets.size();i++){
                   gr.drawImage(imageBu, null, bullets.get(i).getdx(), bullets.get(i).getdy());
                   repaint();
                    }
                }
                gr.draw(recB);
            }

    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyPressed(KeyEvent e) 
    {moveRec(e);}
    @Override
    public void keyReleased(KeyEvent e) 
    {}
}

and this is my bullet calss:

package game;

public class Bullet {
    private int x, y, newy;

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

    public int getdy(){
        return y;
    }
    public void setdy(int newy){
        y = newy;
    }
    public int getdx(){
        return x;
    }
}

Solution

  • Finally I did it with adding a timer in my bullet class and repaint() in my paintcomponent method!

        package game;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    
    public class Bullet {
        private int x, y;
        private int speed, ttl;
        public final Timer timer1;
    
        public Bullet(int x, int y, int speed){
    
            this.x = x;
            this.y = y;
            this.speed= speed;
            this.ttl = 250;
    
            ActionListener actListener = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Move();
                }
            };
            this.timer1 = new Timer(50, actListener);
            timer1.start();
        }
    
        public int getdy(){
            return y;
        }
        public void setdy(int newy){
            y = newy;
        }
        public int getdx(){
            return x;
        }
        public int Move(){
        //Do some calculation to perform loss of velocity within a reasonable range. Because these number might be overkill
        this.speed -= (ttl / 100);
        y += this.speed;
        ttl--;
        return y;
        }
    }