如何让它绘制子弹?

时间:2015-05-24 04:06:52

标签: java graphics draw

我是Java新手,正在尝试构建我的第一个游戏。在我的游戏中,玩家 - 在屏幕底部左右移动 - 射击子弹,直接从点击空格按钮时玩家的x位置直接上升。

我无法弄清楚如何画出每个生成的子弹。在此之前,我将所有内容都绘制在主Game类中的draw方法中,但由于keyListener位于Player类中,因此无法执行此操作。

问题是:每次点击空格键时如何画一个子弹?

以下是玩家类:

package Sprites;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import Graphics.*;


public class Player implements KeyListener{

private int x, y;
private BufferedImage image;
private int dir;
private int limX, limY;
private ArrayList<Integer> keysDown;
private ArrayList<Bullets> bullCount;
private Bullets b;


public Player(int x1) {
    x = x1; //initial spawns of player
    image = Assets.player;
    limX = 800;
    limY = 600;
    keysDown = new ArrayList<Integer>();
    bullCount = new ArrayList<Bullets>();
}

public void update() {
    move();
}


public void move() {

    if(keysDown.contains(KeyEvent.VK_A)) {
        if (x > 0 + 10) {
        x = x - 3;
        }
    }
    if(keysDown.contains(KeyEvent.VK_D)){
        if (x < 800 - 42) {
        x = x + 3;
        }
    }
}

public ArrayList<Bullets> getBullCount() {
    return bullCount;
}

public int getPX() {
    return x;
}

public void keyPressed(KeyEvent e) {
    if (!keysDown.contains(e.getKeyCode()))
        keysDown.add(new Integer(e.getKeyCode()));
    if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            b = new Bullets(x);
            bullCount.add(b);
            System.out.println ("There are " + bullCount.size() + "bullets alive.");
        }
    }

public void keyReleased(KeyEvent e) {
    keysDown.remove(new Integer(e.getKeyCode()));

}

public void keyTyped(KeyEvent e) {

}
}

子弹类:

package Sprites;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import Graphics.*;


public class Bullets{

private int x, y;
private BufferedImage image;
private Graphics2D g;


public Bullets(int c) {
    image = Assets.bullet;
    x = c;
    y = 600 - 42;
    g = (Graphics2D)image.getGraphics();
}

public int getY() {
    return y;
}

public void update() {

move();
draw(g);

}


public void move() {y = y + 50;}

public void draw(Graphics g2d) {
    g.drawImage(Assets.bullet, x, y, null);
}
}

和游戏课程(但我认为不需要这个)

package Main;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import Graphics.*;
import Sprites.Bullets;
import Sprites.Player;

@SuppressWarnings("serial")
public class Game extends JPanel
implements Runnable{

//dimensions
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    public static final int SCALE = 2;

private int intX = WIDTH - 16; //initial x location of player
Player a = new Player(intX);

//game thread
private Thread thread;
private boolean running;
private int FPS = 60;
private long targetTime = 1000 / FPS;

//image
private BufferedImage image;

private Graphics2D g;

//Constructor
public Game () {
    super();
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setFocusable(true);
    requestFocus();
}

public void addNotify() {
    super.addNotify();
    if(thread == null) {
        thread = new Thread(this);
        addKeyListener(a);
        thread.start();
    }
}

private void init () {
    image = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
    g = (Graphics2D)image.getGraphics();

    running = true;
    Assets.init();
}

public void run() {

    init();

    long start;
    long elapsed;
    long wait;

    //game loop
    while(running){
    start = System.nanoTime();

    update();
    draw(g);
    drawToScreen();

    elapsed = System.nanoTime() - start;

    wait = targetTime - elapsed / 1000000;
    if(wait < 0) wait= 5;

    try {
        Thread.sleep(wait);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    }
}

private void update() {
    a.update();
}
private void draw(Graphics g2d) {
    g.setColor(Color.black);
    g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);
    g.drawImage(Assets.player, a.getPX(), HEIGHT * SCALE - 42, null); //draws player
}

private void drawToScreen() {
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
    g2.dispose();
}

}

对不起这篇文章的篇幅!

0 个答案:

没有答案