如何射击"子弹"在java swing动画游戏中

时间:2014-12-16 02:19:37

标签: java swing animation graphics game-physics

我创建了一个简单的动画游戏,用户可以使用箭头键移动圆圈。我画了一个跟随''圆周的中心。我的目标是在圆圈的中心(跟随它)有无限的正方形(子弹)供应,可以在移动时使用asdf向左上方射击。我试图找出如何键入代码,将子弹从一次射击周围的圆圈分离。提前致谢。我是初学者,所以我提前为这个凌乱的代码道歉:)

public class Game extends JPanel implements ActionListener, KeyListener
{

    Timer t = new Timer(5, this);

    int x, y, velx, vely, movx, movy = 0; 

    int shootx, shooty;

    int xaim = 0;

    int yaim = 0;

    int xpts[], ypts[];

    int aim;

    Polygon bullet;

    public Game(){

        t.start();

        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    public void paintComponent(Graphics g){

        Graphics2D g2d = (Graphics2D) g;
        //Background
        g2d.setColor(Color.BLUE);
        g2d.fillRect(0, 0, 600, 600);
        g2d.fillRect(x, y, 30, 30);
        //Borders
        g2d.setColor(Color.GREEN);
        g2d.fillRect(0, 0, 20, 600);
        g2d.fillRect(0, 0, 600, 20);
        g2d.fillRect(580, 0, 20, 600);
        g2d.fillRect(0,560,600,20);
        //Player
        g2d.setColor(Color.RED);
        g2d.fillOval(x, y, 30, 30);
        //Bullet
        g2d.setColor(Color.BLACK);
        g2d.fillRect(xaim+28,yaim+28,15,15);    
    }
    public void actionPerformed(ActionEvent e) {
          checkbounds();
          x += movx;
          y += movy;
          xaim += velx;
          yaim += vely;

        repaint();
    }
    public void keyPressed(KeyEvent e) {
          int code = e.getKeyCode();
          if (code == KeyEvent.VK_UP)
          {
              up();
          }
          if (code == KeyEvent.VK_DOWN)
          {
              down();
          }
          if (code == KeyEvent.VK_LEFT)
          {
              left();
          }
          if (code == KeyEvent.VK_RIGHT)
          {
              right();
          }
          if (code == KeyEvent.VK_A)
          {
             //code to shoot left
          }
          if (code == KeyEvent.VK_W)
          {
              //code to shoot up
          }
          if (code == KeyEvent.VK_D)
          {
              //code to shoot right
          }
          if (code == KeyEvent.VK_S)
          {
              //code to shoot down
          }

    }
    public void up(){
        movy = -1;
        movx = 0;
        velx = 0;
        vely = -1;
    }
    public void down(){
        movy = 1;
        movx = 0;
        velx = 0;
        vely = 1;

    }
    public void right(){
        movx = 1;
        movy = 0;
        velx = 1;
        vely= 0;

    }
    public void left(){
        movx = -1;
        movy = 0;
        velx = -1;
        vely = 0;
    }
    public void checkbounds(){
        if(x < 20){
            x = 20;
        }
        if(x > 550){
            x = 550;
        }
        if(y < 20){
            y=20;
        }
        if(y > 530){
            y = 530;
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}

}

1 个答案:

答案 0 :(得分:1)

基本上,您要做的是为Bullet创建一个新类。该类将保存每个子弹的数据值(x位置,y位置,x速度,y速度)。然后,在主类中创建项目符号列表。每当您想要添加项目符号时,请将其添加到列表中。每当您更新游戏时,也要更新列表。 This是一个很好的游戏教程,可以帮助您入门。