多个相同类型的移动对象

时间:2013-12-14 19:47:42

标签: java object

我开始在java中做一个小游戏。像迷宫一样。但我对“敌人”有一点问题。

我有一个类“Ball”创建了玩家和一个类“Enemy”,我想用它来创建不止一个类型。敌人是正方形,我需要很多特定的坐标,所以当方块向左侧移动时,我将不得不上下移动球。我不知道这些指示有多具体。但我只设法制造了一个移动的方形矿石,但那些不移动。

任何可能知道并需要更多信息的人请向我索要,我会将代码或您需要的内容发送给您。 谢谢!

ENEMY班级:

public class Enemy {

int Y = 20;
private static final int WIDTH = 60;
private static final int HEIGHT = 50;
int x = 1000;
private Game game;

public Enemy(Game game) {
    this.game = game;

}
public void paint(Graphics2D g) {
    g.fillRect(x, Y, WIDTH, HEIGHT);

}
public void move() {
        x = x - 1;
}

public Rectangle getBounds() {
    return new Rectangle(x, Y, WIDTH, HEIGHT);
}

public int getTopY() {
    return Y;}

BALL类:

public class Ball {
    private static final int DIAMETRU = 30;
    int x = 200;
    int y = 0;
    private Game game;

    public Ball(Game game) {
        this.game= game;
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP)
            y = y - 5;
        if (e.getKeyCode() == KeyEvent.VK_DOWN)
            y = y + 5;
    }

    public void keyReleased(KeyEvent e) {

    }

    void move(){

        if (collision()){
            x = x - 5;
        }
        if (x == 25)
            game.gameOver();
    }
    private boolean collision() {
        return game.enemy.getBounds().intersects(getBounds());
    }

    public void paint(Graphics2D g) {
        g.fillOval(x, y, DIAMETRU, DIAMETRU);
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, DIAMETRU, DIAMETRU);
    }
}

游戏类

public class Game extends JPanel {

    Ball ball = new Ball(this);
    GameOver go = new GameOver(this);
    Enemy enemy = new Enemy(this);


    public Game() {
        addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {
            }
            public void keyReleased(KeyEvent e) {
                ball.keyReleased(e);
            }
            public void keyPressed(KeyEvent e) {
                ball.keyPressed(e);
            }
        });
        setFocusable(true);
    }

    private void move() {
        enemy.move();
        ball.move();


    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        ball.paint(g2d);
        enemy.paint(g2d);
        go.paint(g2d);
    }

    public void gameOver() {
        JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Maze");
        Game game = new Game();
        frame.add(game);
        frame.setSize(1000,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            game.move();
            game.repaint();
            Thread.sleep(5);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,对于任何阅读此问题的人来说,目前还不清楚如何呈现基本理解问题所需的所有内容。

以下是 3D 解决方案: 您可以使用j3d-package,可以找到一个很好的教程here。读到最后,也许它可以帮助你独自一人。

要移动对象,请在某处保留它们的列表(可能是'ArrayList')并经常更新它们以让它们移动。

如果您阅读了教程,尤其是部分positioning,那么您应该知道如何在世界中定位对象。如果您理解了TransformGroups的概念,那么您可以意识到,为了操纵对象的位置,您必须将TransformGroup保持在某处。这意味着你的敌人类似乎是这样的:

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;
class Enemy {
    private TransformGroup tg; //Reference to move the object and also 
                               // has to be added to the world, see tutorial
    private ColorCube cube; //Reference to the cube, I don't exactly know why, could come in handy
    public Enemy() {
        Transform t = new Transform3D();
        this.tg = new TransformGroup();
        this.cube = new ColorCube(0.3); //Light red
        t.setTranslation(Vector3f(0.0f, 0.0f, 0.0f)); //Init with starting position
        this.tg.setTransform(t);
    }

    public void reposition() {
        //Call this for reposition and do your things
        // manipulate the TransformGroup for example
    }
    public TransformGroup getTg() { //So you can add it to the world after instanciating a new object
        return this.tg;
    }
}

你的角色的类看起来有点相同也许你可以添加一些多态性,如果你知道什么是简化如何同时处理敌人和你自己的类(它们都需要经常更新) )。