仅在圆形边界内移动项目的逻辑

发布于 2024-09-25 23:47:36 字数 5012 浏览 1 评论 0原文

我是游戏编程的新手。我正在尝试开发一个简单的射击游戏。我正在尝试的游戏场地是圆形的。游戏有 1 名射手和 5 名机器人。射手和机器人只能在圆形区域内移动。以下是我尝试过的代码片段。

我尝试使用状态设计模式来实现游戏。 在不允许射击者跨越圆形边界的逻辑中获得结构。 请帮助完成具有更改射手逻辑的StateImpl类 位置。

public class Board extends JPanel implements ActionListener {
        private static final long serialVersionUID = -397810249729996307L;
        private final Timer timer;
        private final Shooter shooter;

    public Board(Point boardDimensions) {
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.WHITE);
        setDoubleBuffered(true);
        setSize(boardDimensions.x, boardDimensions.y);
        shooter = new Shooter(new Point(200, 225), boardDimensions);
        timer = new Timer(5, this);
        timer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        // g2d.draw(new Ellipse2D.Double(20, 10,350,350));
        Ellipse2D.Double circle1 = new Ellipse2D.Double(20, 10, 350, 350);
        g2d.draw(circle1);
        g2d.drawImage(shooter.getImage(), shooter.getShooterPosition().x,
                shooter.getShooterPosition().y, this);

        g2d.setColor(Color.BLUE);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    /**
     * Called by the AWT just after the user informs the listened-to component
     * that an action should occur.
     */
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            // Method to enable use of keys for control of the craft;
            int key = e.getKeyCode();

            if (key == KeyEvent.VK_LEFT) {
                shooter.moveLeft();
            }
            if (key == KeyEvent.VK_UP) {
                shooter.moveForward();
            }
            if (key == KeyEvent.VK_RIGHT) {
                shooter.moveRight();
            }
            if (key == KeyEvent.VK_DOWN) {
                shooter.moveBackward();
            }
        }
    }

    public void twait() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("main thread interrupted");
        }
    }
}


public class Shooter {

    private Point shooterPosition;

    private final State state;

    protected Image image;

    public Shooter(Point shooterPosition, Point boardSize) {
        super();
        this.shooterPosition = shooterPosition;
        this.state = new StateImpl(this);
        ImageIcon ii = new ImageIcon(this.getClass().getResource("bld2.png"));
        image = ii.getImage();
    }

    public Point getShooterPosition() {
        return shooterPosition;
    }

    public void setShooterPosition(Point shooterPosition) {
        this.shooterPosition = shooterPosition;
    }

    public void moveLeft() {
        this.shooterPosition = state.moveLeft();
    }

    public void moveRight() {
        this.shooterPosition = state.moveright();
    }

    public void moveForward() {
        this.shooterPosition = state.moveForward();
    }

    public void moveBackward() {
        this.shooterPosition = state.moveBackward();
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    @Override
    public String toString() {
        return "Shooter [shooterPosition=" + shooterPosition + ", state="
                + state + ", image=" + image + "]";
    }

}


public interface State {

    public Point moveForward();

    public Point moveBackward();

    public Point moveLeft();

    public Point moveright();

    public void shoot();

}

public class StateImpl implements State {

    private final Shooter shooter;
    private final int boardSize = 200;

    public StateImpl(Shooter shooter) {
        this.shooter = shooter;
    }

    public Point moveForward() {
        Point currentShooterPosition = this.shooter.getShooterPosition();
        int newY = (currentShooterPosition.y + 1) > boardSize ? boardSize
                : currentShooterPosition.y + 1;
        return new Point(currentShooterPosition.x, newY);
    }

    public Point moveBackward() {
        Point currentShooterPosition = this.shooter.getShooterPosition();
        return new Point(currentShooterPosition.x, currentShooterPosition.y - 1);
    }

    public Point moveLeft() {
        Point currentShooterPosition = this.shooter.getShooterPosition();
        return new Point(currentShooterPosition.x - 1, currentShooterPosition.y);
    }

    public Point moveright() {

        Point currentShooterPosition = this.shooter.getShooterPosition();
        int newX = (currentShooterPosition.x + 1) > boardSize ? boardSize
                : currentShooterPosition.x + 1;
        return new Point(newX, currentShooterPosition.y);
    }

    public void shoot() {

    }
}

I'm newbie to Game programming. I'm trying to develop a simple shooting game. The game ground which I'm trying is a circular one. The game has one shooter and 5 droids. The shooter and the droids have to move within the circular area only. Following is the code snippet i tried.

I tried implementing the game using the State design pattern.
Got struct in the logic where the Shooter is not allowed to cross the circular boundary.
Please help in completing the StateImpl class which has the logic of changing the shooter
position.

public class Board extends JPanel implements ActionListener {
        private static final long serialVersionUID = -397810249729996307L;
        private final Timer timer;
        private final Shooter shooter;

    public Board(Point boardDimensions) {
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.WHITE);
        setDoubleBuffered(true);
        setSize(boardDimensions.x, boardDimensions.y);
        shooter = new Shooter(new Point(200, 225), boardDimensions);
        timer = new Timer(5, this);
        timer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        // g2d.draw(new Ellipse2D.Double(20, 10,350,350));
        Ellipse2D.Double circle1 = new Ellipse2D.Double(20, 10, 350, 350);
        g2d.draw(circle1);
        g2d.drawImage(shooter.getImage(), shooter.getShooterPosition().x,
                shooter.getShooterPosition().y, this);

        g2d.setColor(Color.BLUE);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    /**
     * Called by the AWT just after the user informs the listened-to component
     * that an action should occur.
     */
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            // Method to enable use of keys for control of the craft;
            int key = e.getKeyCode();

            if (key == KeyEvent.VK_LEFT) {
                shooter.moveLeft();
            }
            if (key == KeyEvent.VK_UP) {
                shooter.moveForward();
            }
            if (key == KeyEvent.VK_RIGHT) {
                shooter.moveRight();
            }
            if (key == KeyEvent.VK_DOWN) {
                shooter.moveBackward();
            }
        }
    }

    public void twait() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("main thread interrupted");
        }
    }
}


public class Shooter {

    private Point shooterPosition;

    private final State state;

    protected Image image;

    public Shooter(Point shooterPosition, Point boardSize) {
        super();
        this.shooterPosition = shooterPosition;
        this.state = new StateImpl(this);
        ImageIcon ii = new ImageIcon(this.getClass().getResource("bld2.png"));
        image = ii.getImage();
    }

    public Point getShooterPosition() {
        return shooterPosition;
    }

    public void setShooterPosition(Point shooterPosition) {
        this.shooterPosition = shooterPosition;
    }

    public void moveLeft() {
        this.shooterPosition = state.moveLeft();
    }

    public void moveRight() {
        this.shooterPosition = state.moveright();
    }

    public void moveForward() {
        this.shooterPosition = state.moveForward();
    }

    public void moveBackward() {
        this.shooterPosition = state.moveBackward();
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    @Override
    public String toString() {
        return "Shooter [shooterPosition=" + shooterPosition + ", state="
                + state + ", image=" + image + "]";
    }

}


public interface State {

    public Point moveForward();

    public Point moveBackward();

    public Point moveLeft();

    public Point moveright();

    public void shoot();

}

public class StateImpl implements State {

    private final Shooter shooter;
    private final int boardSize = 200;

    public StateImpl(Shooter shooter) {
        this.shooter = shooter;
    }

    public Point moveForward() {
        Point currentShooterPosition = this.shooter.getShooterPosition();
        int newY = (currentShooterPosition.y + 1) > boardSize ? boardSize
                : currentShooterPosition.y + 1;
        return new Point(currentShooterPosition.x, newY);
    }

    public Point moveBackward() {
        Point currentShooterPosition = this.shooter.getShooterPosition();
        return new Point(currentShooterPosition.x, currentShooterPosition.y - 1);
    }

    public Point moveLeft() {
        Point currentShooterPosition = this.shooter.getShooterPosition();
        return new Point(currentShooterPosition.x - 1, currentShooterPosition.y);
    }

    public Point moveright() {

        Point currentShooterPosition = this.shooter.getShooterPosition();
        int newX = (currentShooterPosition.x + 1) > boardSize ? boardSize
                : currentShooterPosition.x + 1;
        return new Point(newX, currentShooterPosition.y);
    }

    public void shoot() {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧街凉风 2024-10-02 23:47:36

不要在 Paint() 方法中初始化圆,而是将其(更重要的是其中心)保留在类变量中。然后,每当射手移动时,测量圆心与射手新位置之间的距离。如果该距离大于圆的半径,则阻止射击者的移动。

Instead of initializing the circle in the paint() method, keep it (and more importantly its centre) in a class variable. Then, whenever the shooter moves, measure the distance between the centre of the circle and the shooter's new location. If that distance is larger than the radius of the circle, block the shooter's movement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文