Java帮助:使图像在屏幕上移动

时间:2014-09-22 21:22:59

标签: java image

我已经有很长一段时间没遇到这个问题了 我试图制造一个太空射击但无济于事,我试图让子弹像太空入侵者一样在屏幕上移动 当玩家按下空格键时,子弹应该出现在玩家的X位置,并在屏幕上向右移动。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class game extends JFrame{

boolean run = true;
boolean fired = false;

Image player;
Image bullet;

int playerX = 100;
int playerY = 200;

int bulletX;
int bulletY;


public game(){
    //Load Images:
    ImageIcon playerI = new     ImageIcon("C:/Users/Dan/workspace/shooterProject/bin/shooterProject/ship.png");
    player = playerI.getImage();
    ImageIcon bulletI = new     ImageIcon("C:/Users/Dan/workspace/shooterProject/bin/shooterProject/bullet.png");
    bullet = bulletI.getImage();
    //Set up game

      addKeyListener(new AL());

       addMouseListener(new Mouse());

       init();

}

private Image dbImage;
private Graphics dbg;


public static void main(String[] args) {
  new game();
}


//When the program runs, thins are initialised here

public void init(){
    windowManager();
}
public void paintComponent(Graphics g){

    if(run == true){
        g.drawImage(player, playerX, playerY, this);
    }
    if(fired == true){
        g.drawImage(bullet, bulletX, bulletY, this);
    }

    repaint();

}

public void paint(Graphics g){
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage,0,0,this);
}


public void bullet(){
    bulletX = playerX;
    bulletY = playerY;
    while(fired == true){
        bulletX = bulletX + 10;
        if(bulletX == 800){
            bullet = null;
            fired = false;
        }
    }

}


public void windowManager(){

      JFrame f = new JFrame();

      setTitle("Engine");
      setVisible(true);
      setResizable(false);
      setSize(800,400);
      setBackground(Color.BLACK);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class AL extends KeyAdapter{
    public void keyPressed(KeyEvent e){

        int keyCode = e.getKeyCode();
        if((keyCode == KeyEvent.VK_UP) && (run == true) && (playerY - 20 > 0)){
               playerY = playerY - 10;
        }else if((keyCode == KeyEvent.VK_DOWN) && (run == true) && (playerY + 20 < 400)){
            playerY = playerY + 10;
        }
        if((keyCode == KeyEvent.VK_SPACE) && (fired == false)){
            fired = true;
            if(fired == true){
                bullet();
            }
        } 
    }

    public void keyReleased(KeyEvent e){
    }
}

public class Mouse extends MouseAdapter {
public void mousePressed(MouseEvent e) {

    double x = e.getX();
    double y = e.getY();

}
}
}

无论其

当我在没有while循环的情况下运行代码时,子弹出现在玩家的X位置 但 当玩家按下X按钮时,while循环没有任何反应,子弹甚至不出现!

任何人都可以帮助我如何让子弹出现并在屏幕上移动? 感谢

1 个答案:

答案 0 :(得分:1)

这是因为你没有在子弹超出范围之前吸引子弹,你不应该以这种方式使用while循环,你可能需要google for&#39; Game Loop&#39;但是直到你在这里做一个可能有帮助的狙击手,请注意非常糟糕但应该有效:

public void paintComponent(Graphics g){

    if(run == true){
        g.drawImage(player, playerX, playerY, this);

        if(fired == true) {
            bulletX = bulletX + 10;
            if(bulletX > 800 || bulletX < 0){
                fired = false;
            }

            g.drawImage(bullet, bulletX, bulletY, this);
        }

        repaint();
    }

}

public void bullet(){
    bulletX = playerX;
    bulletY = playerY;
}

最后一个注释,将这个代码在paint方法dbImage = createImage(getWidth(),getHeight())中移动到构造函数或init(),因为你每帧都在创建一个新图像。