-1

A 和 C 图像距离很远。此时,从A的坐标值创建B图像,每次迭代,都必须移动x,y,才能到达C图像的坐标值。

比如A(100,100),C(300,300)
从B(100,100)开始,
每次重复,x,y都要移动到B(300,300)。

这是输入当前移动源的方法。

 Public void Attack () {
    int x1 = a.getX ();
    int y1 = a.getY ();

    int x2 = c.getX ();
    int y2 = c.getY ();
    if(b.getX ()==c.getX&&b.getY () == c.getY())'
    {
        system.out.println ("ok");'
    }else {
        b.setbounds (help1,help2,100,50)
    }
 }

在这里,我想知道输入help1 help2的代码。

毕达哥拉斯公式

在 A 和 C 图像之间我想知道如何使 B 图像沿着虚拟直线移动。

就像一个塔防游戏。A 图像是塔 B 图像是子弹 C 图像是敌人。

我希望从塔发射的子弹移动到敌人的位置。

我是韩国人。我用了翻译器

4

1 回答 1

0

下面的代码是使用直线方程y = mx + c沿着这条线绘制移动物体的mre 。 要测试代码,请将整个代码复制到并运行,或在线运行
MoveAlongStraightLine.java

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class MoveAlongStraightLine extends JFrame {

    public MoveAlongStraightLine(){
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        ShootinBoard shootingBoard = new ShootinBoard();
        JButton fire = new JButton("Fire");
        fire.addActionListener(e->shootingBoard.fire());
        add(shootingBoard);
        add(fire, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

    public static void main(String[]args){
        SwingUtilities.invokeLater(()->new MoveAlongStraightLine());
    }
}

class ShootinBoard extends JPanel{

    //use constants for better readability
    private static final double W = 600, H = 400;
    private static final int DOT_SIZE = 10, SPEED = 2;
    private final int dX = 1; //x increment

    private final Timer timer;
    private final Point2D.Double shooter, target;
    private Point2D.Double bullet;

    public ShootinBoard() {
        setPreferredSize(new Dimension((int)W, (int)H));
        shooter = new Point2D.Double(50,350);
        bullet = shooter; //place bullet at start point
        target = new Point2D.Double(550,50);
        timer = new Timer(SPEED, e->moveBullet());
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(new BasicStroke(3));
        //draw source
        g2.setColor(Color.blue);
        g2.draw(new Ellipse2D.Double(shooter.getX(), shooter.getY(),DOT_SIZE , DOT_SIZE));
        //draw bullet
        g2.setColor(Color.black);
        g2.draw(new Ellipse2D.Double(bullet.getX(), bullet.getY(),DOT_SIZE , DOT_SIZE));
        //draw target
        g2.setColor(Color.red);
        g2.draw(new Ellipse2D.Double(target.getX(), target.getY(),DOT_SIZE , DOT_SIZE));
    }

    void fire(){
        timer.stop();
        bullet = shooter; //place bullet at start point
        timer.start();
    }

    void moveBullet() {

        if(target.x == bullet.x && target.y == bullet.y) {
            timer.stop();
        }
        //y = mx + c  for more details see https://www.usingmaths.com/senior_secondary/java/straightline.php
        double m = (target.y - bullet.y)/ (target.x - bullet.x);//slope
        double c = (target.x * bullet.y - bullet.x * target.y)/(target.x - bullet.x);

        double newBulletX = bullet.x+dX; //increment x
        double newBulletY = m * newBulletX + c; //calculate new y
        bullet =  new Point2D.Double(newBulletX,newBulletY);

        repaint();
    }
}

在此处输入图像描述

于 2020-04-23T04:51:21.193 回答