简体   繁体   English

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

[英]Java help: make image move across the screen

I've been having trouble with this for quite a while now I am trying to make a space shooter but to no avail, I'm trying to make the bullet move across the screen like in space invaders etc when the player presses the space bar a bullet should appear where the player's X position is and move right across the screen. 我已经为此困扰了一段时间了,现在我正在尝试制作太空射击游戏,但无济于事,我试图让子弹在屏幕上移动,就像在玩家按下空格键时在太空侵略者中一样项目符号应出现在玩家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();

}
}
}

HOWEVER 然而

When I run the code without the while loop the bullet appears at the player's X position but When the while loop is there when the player presses the X button nothing happens, the bullet doesnt even appear! 当我在不使用while循环的情况下运行代码时,项目符号会出现在玩家的X位置,但是当while循环存在时,如果玩家按下X按钮,则什么也不会发生,项目符号甚至都不会出现!

would anybody be able to assist me in how i can make the bullet appear and move across the screen? 谁能协助我使子弹出现并在屏幕上移动? thanks 谢谢

This is because you are not drawing the bullet until it's out of range, you should not use the while loop this way, you probably need to google for 'Game Loop' but until you do here is a snipet that may help, Note that very bad but should work: 这是因为您不会在子弹超出范围之前绘制子弹,您不应该以这种方式使用while循环,您可能需要在Google上搜索“游戏循环”,但是直到您在此处做一个摘录,这可能会有所帮助,请注意不好但是应该工作:

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;
}

one final note, move this code in the paint methode dbImage = createImage(getWidth(), getHeight()) to the constructor or the init() because you are creating a new image every frame. 最后一点,将这段代码在绘制方法中移动dbImage = createImage(getWidth(),getHeight())到构造函数或init(),因为您要在每帧创建一个新图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM